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

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 36
Reputation: Robtyketto is an unknown quantity at this point 
Solved Threads: 0
Robtyketto Robtyketto is offline Offline
Light Poster

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

 
0
  #1
Mar 31st, 2009
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,625
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

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

 
0
  #2
Mar 31st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,501
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

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

 
0
  #3
Mar 31st, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 36
Reputation: Robtyketto is an unknown quantity at this point 
Solved Threads: 0
Robtyketto Robtyketto is offline Offline
Light Poster

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

 
0
  #4
Mar 31st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,501
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

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

 
0
  #5
Mar 31st, 2009
Originally Posted by Robtyketto View Post
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()
Reply With Quote Quick reply to this message  
Reply

Tags
graphics

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for graphics
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC