943,712 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4376
  • Java RSS
Mar 31st, 2009
0

Java Applet: draw graphics based on java.awt.choice

Expand Post »
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

Java Syntax (Toggle Plain Text)
  1. package wk8exercise3;
  2.  
  3. import java.applet.Applet;
  4. import java.awt.*;
  5. import java.awt.Graphics;
  6. import java.awt.event.*;
  7.  
  8. /**
  9.  *
  10.  * @author Rob
  11.  */
  12. public class wk8Exercise3 extends Applet implements ItemListener {
  13.  
  14. Choice myChoice;
  15.  
  16. int rectX;
  17. int rectY;
  18. int rectWidth ;
  19. int rectHeight;
  20. String shape;
  21.  
  22. public void init()
  23. {
  24. // Create the choice and add some choices
  25. myChoice = new Choice();
  26. myChoice.addItem("Pick a shape to draw");
  27. myChoice.addItem("Draw a rectangle");
  28. myChoice.addItem("Draw a Line");
  29. myChoice.addItem("Draw an Oval");
  30. add(myChoice);
  31. myChoice.addItemListener(this);
  32. }
  33.  
  34. public void itemStateChanged (ItemEvent e)
  35. {
  36. // Declare integer for use with index of choice
  37. int Selection;
  38. Selection = myChoice.getSelectedIndex();
  39.  
  40. // Declare variables to hold paramater to be used to draw selected item
  41.  
  42. if (Selection == 1)
  43. {
  44. drawARectangle(50,50,100,100);
  45. }
  46. if (Selection == 2)
  47. {
  48. drawALine(50,50,200,50);
  49. }
  50. if (Selection == 3)
  51. {
  52. drawAnOval(50,50,200,50);
  53. }
  54. }
  55.  
  56. public void paint(Graphics g)
  57. {
  58. // Not sure what to do here
  59. }
  60.  
  61.  
  62.  
  63. public void drawARectangle(int RectX, int RectY, int RectWidth, int RectHeight)
  64. {
  65. repaint();
  66. Graphics g = getGraphics();
  67. g.drawRect(RectX, RectY, RectWidth, RectHeight);
  68. }
  69.  
  70. public void drawALine(int lineX1, int lineY1, int lineX2, int lineY2)
  71. {
  72. repaint();
  73. Graphics g = getGraphics();
  74. g.drawLine(lineX1,lineY1,lineX2,lineY2);
  75. }
  76.  
  77. public void drawAnOval(int ovalX, int ovalY, int ovalWidth, int ovalHeight)
  78. {
  79. repaint();
  80. Graphics g = getGraphics();
  81. g.drawOval(ovalX, ovalY, ovalWidth, ovalHeight);
  82. }
  83.  
  84. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Robtyketto is offline Offline
36 posts
since Mar 2007
Mar 31st, 2009
0

Re: Java Applet: draw graphics based on java.awt.choice

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Mar 31st, 2009
0

Re: Java Applet: draw graphics based on java.awt.choice

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)
  1. package wk8exercise3;
  2.  
  3. import java.applet.Applet;
  4. import java.awt.*;
  5. import java.awt.Graphics;
  6. import java.awt.event.*;
  7.  
  8. /**
  9.  *
  10.  * @author Rob
  11.  */
  12. public class wk8Exercise3 extends Applet implements ItemListener {
  13.  
  14. Choice myChoice;
  15.  
  16. int rectX;
  17. int rectY;
  18. int rectWidth ;
  19. int rectHeight;
  20. String shape;
  21. int Selection; // moved this up here
  22.  
  23. public void init()
  24. {
  25. // Create the choice and add some choices
  26. myChoice = new Choice();
  27. myChoice.addItem("Pick a shape to draw");
  28. myChoice.addItem("Draw a rectangle");
  29. myChoice.addItem("Draw a Line");
  30. myChoice.addItem("Draw an Oval");
  31. add(myChoice);
  32. myChoice.addItemListener(this);
  33. }
  34.  
  35. public void itemStateChanged (ItemEvent e)
  36. {
  37. // set new selection index
  38. Selection = myChoice.getSelectedIndex();
  39. repaint();
  40. }
  41.  
  42. public void paint(Graphics g)
  43. {
  44. super.paint(g);
  45.  
  46. if (Selection == 1)
  47. {
  48. // you can call these methods directly here
  49. g.drawRect(50,50,100,100);
  50. }
  51. if (Selection == 2)
  52. {
  53. g.drawLine(50,50,200,50);
  54. }
  55. if (Selection == 3)
  56. {
  57. // or you can pass the Graphics reference as a parameter
  58. drawAnOval(50,50,200,50,g);
  59. }
  60. }
  61.  
  62. // added Graphics as a parameter here
  63. public void drawAnOval(int ovalX, int ovalY, int ovalWidth, int ovalHeight, Graphics g)
  64. {
  65. g.drawOval(ovalX, ovalY, ovalWidth, ovalHeight);
  66. }
  67.  
  68. }
Last edited by Ezzaral; Mar 31st, 2009 at 12:37 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,758 posts
since May 2007
Mar 31st, 2009
0

Re: Java Applet: draw graphics based on java.awt.choice

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
Reputation Points: 10
Solved Threads: 0
Light Poster
Robtyketto is offline Offline
36 posts
since Mar 2007
Mar 31st, 2009
0

Re: Java Applet: draw graphics based on java.awt.choice

Click to Expand / Collapse  Quote originally posted by Robtyketto ...
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?
Well, you can just not paint anything else after the call to super.paint()
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,758 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java difficulty
Next Thread in Java Forum Timeline: Help with array-based implemenation adt list using java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC