I have a Java project that’s about traffic network simulation in a random city, I’ve managed to figure out a way to implement this project, so I divided each intersection into a section which is basically an extended JPanel class (named Carrefour)…everything works well until I got stuck with how to draw vehicles and make them pass through roads.
So my problem is how to draw an image (vehicle image) over an another image (road)?
If this is Swing, then draw the background image in a BufferedImage. Display this BufferedImage in a JComponent’s (such as a JPanel’s) paintComponent method using Graphic’s
drawImage(...)method, and then draw the changing images over this in the same paintComponent method. Don’t forget to call thesuper.paintComponent(...)method first though.Please note that this question has been asked quite a bit here and elsewhere, and as you would expect, there are lots of examples of this sort of thing that you can find here with a bit of searching.
Edit
You ask:
Again, you would create a BufferedImage for this, likely by using
ImageIO.read(...). Then you’d draw this in your JPanel’spaintComponent(Graphics g)method override usingg.drawImage(...).For example…