Reputation: 4391
I need to create some simple graphics using java. Basically what I need to do is draw a car using the Graphics class and make it move across a frame. I think that the way I should do this is:
Paint the graphics on a JPanel
Put the jpanel on the jframe
Then move the jpanel across the jframe
To give this illusion of movement I'm thinking that I should dynamically change the layoutmanager. Does this seem like a good or bad design?
Upvotes: 2
Views: 758
Reputation: 168815
When overriding paintComponent(Graphics)
, it is often best done in a JComponent
rather than a JPanel
.
As for moving the car, I would tend to draw it to a BufferedImage
then draw the BufferedImage
at different positions to simulate movement. The repaints can be controlled by a javax.swing.Timer
.
Upvotes: 3
Reputation: 4818
I think it would be a much better idea to make an object with a paint method that is a car. Then you can move this car around the jFrame and just call paint on it :D
Upvotes: 2