File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Graphics without paint() method 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 "Graphics without paint() method" Watch "Graphics without paint() method" New topic
Author

Graphics without paint() method

Adam Wilkins
Greenhorn

Joined: Oct 19, 2005
Posts: 24
I have a mortgage calculation application (not applet) that is all set up with a JFrame, JPanels, and all other JCompenents. Now, I would like to display graphics within this JFrame. From my main method, I call the instance method of my class to create the window, buttons, fields, etc and add actionListener(s) to my buttons. Now, I only know how to add graphics with the paint() method, but if I move my window creation code to my paint() method and subsequently end it with repaint(), the results are unacceptable with every refresh.

If I keep my instance method in their to create the window and try to add a paint method for the graphics only, it overrides my complete window.

Is there a way to add graphics and animation to a JFrame without using the paint() method?

Thanks in advance....
Adam Wilkins
Greenhorn

Joined: Oct 19, 2005
Posts: 24
I found my answer. Disregard this post...

Thanks
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
For the sake of other posters ... if you want to display some graphics,
never override paint in JFrame, JApplet, etc... Instead, subclass JComponent
or JPanel and override its paintComponent method:

Create an instance of your class and add that component to your UI, like
any other component.
Here is the 2D graphics tutorial: http://java.sun.com/docs/books/tutorial/2d/index.html


There is no emoticon for what I am feeling!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Graphics without paint() method