Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes How to properly create an applet with an Input field and some shapes. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to properly create an applet with an Input field and some shapes." Watch "How to properly create an applet with an Input field and some shapes." New topic
Author

How to properly create an applet with an Input field and some shapes.

Tochka Zapetaq
Greenhorn

Joined: Feb 17, 2010
Posts: 4
Hello!

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 :-)


Thanks in advance for all and any help :-)
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Tochka,
Welcome to the Ranch.

Some tips:
You should be using Swing instead of AWT (JApplet instead of Applet JTextField 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.

Recommended reading:http://java.sun.com/products/jfc/tsc/articles/painting/#swing

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Tochka Zapetaq
Greenhorn

Joined: Feb 17, 2010
Posts: 4
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 :-)

I changed to JApplet and JTextField.

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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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...


Define some fields in your applet/panel.


Then set these values based on user input.
Then modify your method to


Then, from your actionPerformed, simply invoke repaint like


Also check out layout managers
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
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.
Tochka Zapetaq
Greenhorn

Joined: Feb 17, 2010
Posts: 4
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!
Tochka Zapetaq
Greenhorn

Joined: Feb 17, 2010
Posts: 4
Here's my final code. Time to go to bad :-) If you have any comments please post.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to properly create an applet with an Input field and some shapes.