简体   繁体   中英

Simple Java Graphics

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?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM