• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't draw on JFrame or its ContentPane.

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!