aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Can't draw on JFrame or its ContentPane. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Can Watch "Can New topic
Author

Can't draw on JFrame or its ContentPane.

James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Hi all,

I'm sorry, I'm having an awful day and a really hard time figuring out why I can't paint the Car objects onto a JFrame OR its ContentPane. The JFrame appears, but no cars appear on it. Anything that can point me in the right direction? This is homework, so I don't want a spoon-fed solution, but maybe if you see something that I'm overlooking, it will help. As usual, thanks in advance!


Car class:



Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
OK, sorry, I see now (finally!!!) that paint() was wiping the slate clean, so maybe I can take it from here. Thanks!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 1808
    
    2
that paint() was wiping the slate clean, so maybe I can take it from here.


And the problem is that you can't control when Swing decides the frame needs to be repainted.

Read the section from the Swing tutorial on Custom Painting. Basically, custom painting is done by override the paintComponent() method of a JPanel or JComponent. That way when Swing decides the frame needs to be repainted, your custom component will repaint itself correctly.

You should never use the getGraphics() method to get a Graphics object. The Graphics object will be passed to you in the paintComponent() method.

Also, get rid of all the static methods and variables. That is the sign of a poorly designed program. Again the Swing tuturial has proper working examples.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Can't draw on JFrame or its ContentPane.
 
Similar Threads
Thread not visible
Help with project using graphics
Variable is not declared?
The method add(Component) in the type Container is not applicable for the arguments (Class)Why?
ActionListener: Non-static variable 'this' cannot be referenced from a non-static context????