I am trying to write a program that reads the number from a TextField and draws a circle with that size and color depending on the size (e.g. if size is between 10 and 20 - make it red, otherwise green). I was able to display the input box and a button to perform the action:
But a few problems show up:
1. if I add a
everything but the input box disappears.
2. I am not sure how to "draw" from within the actionPerformed context as it is not like the paint(Graphics page) as I don't "have" a page :-)
You should be using Swing instead of AWT (JApplet instead of AppletJTextField instead of TextField)
Instead of adding components to the applet, add them to a JPanel and then add the panel instance to the applet using the JApplet#getContentPane()#add method
You should be overriding the paintComponent(Graphics g) method of the JPanel instead of paint
I am not sure how to "draw" from within the ActionPerformed
You obtain a reference to the Graphics in the paintComponent. The Graphics class has several convenient drawXXX and fillXXX methods. Since you mentioned drawing a circle, in your paintComponent you will need to (in pseudo code)
1) Obtain the size and color parameters from the user selection
2) Use these parameters to define the circle. Graphics class has a method which lets you set the color
3) Draw the actual circle using drawOval
To initiate the painting, all you need to do, is invoke repaint() from the actionPerformed.
Thanks for the quick reply. I am a complete novice (read only 3 chapters from a Java book) in this so my questions may seem weird. I know I need to read more before going into "difficult" things like this but just want to try to see how it works and then will understand it completely :-)
OK, the "Instead of adding components to the applet, add them to a JPanel and then add the panel instance to the applet using the JApplet#getContentPane()#add method" part first:
1. I added "private JPanel myPanel;" in line 5
2. I replaced lines 25, 26 and 27 with "myPanel.add(inputLabel); myPanel.add(sizeVariable); myPanel.add(doButton);
The "You should be overriding the paintComponent(Graphics g) method of the JPanel instead of paint " I kind of understand as "I should not be adding anything to the paint method of the JApplet itself but instead to the paintComponent method of the JPanel. I tried and added this at the end of my code:
Then "TestPanelClass myPanel = new TestPanelClass();" before the add() operations and "getContentPane().add(myPanel);" after the add operations of line 25-27;
This compiles and I see the red circle. But I do not know how to use all this in the actionPerformed part of the program...
edit: I think I got this :-). I declared the variable size at the begining of the class and then in actionPerformed was calling redraw() and in the declaration of my class the size was used to set the color of the circle. I don't understand exactly what I did but it looks promising.
I am a complete novice (read only 3 chapters from a Java book)
Then I would start by playing around with an application instead of an Applet. Its easier to test an application because you don't have to worry about html or a browser (and when you paste code in the forum we don't have to worry about it either.
The concepts are the same, you add components to a JFrame instead of a JPanel.
You've been given a link to the Swing tutorial, which include plenty of examples to get you started.
In this case you would probably have to panels. One panel will contain the text field and the button. The second panel will be the panel you override the paintCompnent() method on and do the custom painting. I would add a method like setOvalSize(...) to this custom panel. Then when you click on the button you take the size entered in the text field, and invoke the method. The method will then invoke repaint() on itself.
Joined: Feb 17, 2010
I just edited my post - seems I came up with the same thing somehow :-). I will put a bit more thinking into what I did (and you suggested) and will go from there :-). Thanks a lot!
Joined: Feb 17, 2010
Here's my final code. Time to go to bad :-) If you have any comments please post.