| | |
Java Applet: draw graphics based on java.awt.choice
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 36
Reputation:
Solved Threads: 0
Greetings,
I am creating an applet to draw a shape based on the selected item within the awt choice object.
It currently draws the selected shape based on selection and quickly dissappears.
I believe that I should be doing all drawing elements from the paint method (which I am not).
I have created seperate methods to draw the element when the item state has changed.
Can someone please help explain how I can draw the shapes and when I need to repaint (what am I doing wrong?) .
Thanks
Rob
I am creating an applet to draw a shape based on the selected item within the awt choice object.
It currently draws the selected shape based on selection and quickly dissappears.
I believe that I should be doing all drawing elements from the paint method (which I am not).
I have created seperate methods to draw the element when the item state has changed.
Can someone please help explain how I can draw the shapes and when I need to repaint (what am I doing wrong?) .
Thanks
Rob
Java Syntax (Toggle Plain Text)
package wk8exercise3; import java.applet.Applet; import java.awt.*; import java.awt.Graphics; import java.awt.event.*; /** * * @author Rob */ public class wk8Exercise3 extends Applet implements ItemListener { Choice myChoice; int rectX; int rectY; int rectWidth ; int rectHeight; String shape; public void init() { // Create the choice and add some choices myChoice = new Choice(); myChoice.addItem("Pick a shape to draw"); myChoice.addItem("Draw a rectangle"); myChoice.addItem("Draw a Line"); myChoice.addItem("Draw an Oval"); add(myChoice); myChoice.addItemListener(this); } public void itemStateChanged (ItemEvent e) { // Declare integer for use with index of choice int Selection; Selection = myChoice.getSelectedIndex(); // Declare variables to hold paramater to be used to draw selected item if (Selection == 1) { drawARectangle(50,50,100,100); } if (Selection == 2) { drawALine(50,50,200,50); } if (Selection == 3) { drawAnOval(50,50,200,50); } } public void paint(Graphics g) { // Not sure what to do here } public void drawARectangle(int RectX, int RectY, int RectWidth, int RectHeight) { repaint(); Graphics g = getGraphics(); g.drawRect(RectX, RectY, RectWidth, RectHeight); } public void drawALine(int lineX1, int lineY1, int lineX2, int lineY2) { repaint(); Graphics g = getGraphics(); g.drawLine(lineX1,lineY1,lineX2,lineY2); } public void drawAnOval(int ovalX, int ovalY, int ovalWidth, int ovalHeight) { repaint(); Graphics g = getGraphics(); g.drawOval(ovalX, ovalY, ovalWidth, ovalHeight); } }
•
•
Join Date: Sep 2008
Posts: 1,625
Reputation:
Solved Threads: 205
You basically want to set it up so that everything gets repainted, not just your rectangles. So put your drawRectangle statements in the method where all the other graphics are drawn (paint I believe), and use an "if" statement and a boolean to determine if you actually want to draw the rectangle. I think they should be put in the paint method. I'm not completely positive about all of this advice, so wait for someone to verify it. In the meantime, Here is an example from the Java Sun Tutorials http://java.sun.com/docs/books/tutor...pesDemo2D.java
Last edited by BestJewSinceJC; Mar 31st, 2009 at 12:21 pm.
Correct. The drawing code needs to either be moved into the paint() method or called from it with a reference to the Graphics object. See comments in edited code:
java Syntax (Toggle Plain Text)
package wk8exercise3; import java.applet.Applet; import java.awt.*; import java.awt.Graphics; import java.awt.event.*; /** * * @author Rob */ public class wk8Exercise3 extends Applet implements ItemListener { Choice myChoice; int rectX; int rectY; int rectWidth ; int rectHeight; String shape; int Selection; // moved this up here public void init() { // Create the choice and add some choices myChoice = new Choice(); myChoice.addItem("Pick a shape to draw"); myChoice.addItem("Draw a rectangle"); myChoice.addItem("Draw a Line"); myChoice.addItem("Draw an Oval"); add(myChoice); myChoice.addItemListener(this); } public void itemStateChanged (ItemEvent e) { // set new selection index Selection = myChoice.getSelectedIndex(); repaint(); } public void paint(Graphics g) { super.paint(g); if (Selection == 1) { // you can call these methods directly here g.drawRect(50,50,100,100); } if (Selection == 2) { g.drawLine(50,50,200,50); } if (Selection == 3) { // or you can pass the Graphics reference as a parameter drawAnOval(50,50,200,50,g); } } // added Graphics as a parameter here public void drawAnOval(int ovalX, int ovalY, int ovalWidth, int ovalHeight, Graphics g) { g.drawOval(ovalX, ovalY, ovalWidth, ovalHeight); } }
Last edited by Ezzaral; Mar 31st, 2009 at 12:37 pm.
•
•
Join Date: Mar 2007
Posts: 36
Reputation:
Solved Threads: 0
Thanks, I see now how making the selection global I can used it within other methods.
I will now go away and read up on a few things, very interesting (especially the part of passing the graphics reference as a parameter).
Cheers, much appreciated.
Oh one last thing for clearing the on-screen display for an applet is their another method commonly used rather that getting the dimension size and background colour and simply drawing an applet sized rectangle over the top?
Cheers
I will now go away and read up on a few things, very interesting (especially the part of passing the graphics reference as a parameter).
Cheers, much appreciated.
Oh one last thing for clearing the on-screen display for an applet is their another method commonly used rather that getting the dimension size and background colour and simply drawing an applet sized rectangle over the top?
Cheers
•
•
•
•
Oh one last thing for clearing the on-screen display for an applet is their another method commonly used rather that getting the dimension size and background colour and simply drawing an applet sized rectangle over the top?
![]() |
Similar Threads
- application to applet (Java)
Other Threads in the Java Forum
- Previous Thread: java difficulty
- Next Thread: open and read all files from folder
| Thread Tools | Search this Thread |
Tag cloud for graphics







