It's not a secret anymore!
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Graphics without paint() method" Watch "Graphics without paint() method" New topic

Graphics without paint() method

Adam Wilkins

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

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

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:

There is no emoticon for what I am feeling!
I agree. Here's the link:
subject: Graphics without paint() method
jQuery in Action, 3rd edition