How to place images on top of each other in java
Posted
by Haxed
on Stack Overflow
See other posts from Stack Overflow
or by Haxed
Published on 2010-06-18T05:41:13Z
Indexed on
2010/06/18
5:43 UTC
Read the original article
Hit count: 278
Hi,
I have a background image of a road, which I have displayed on a JFrame using an ImageIcon. I want to put a car on top of that background image at a certain (x,y) location.
I tried using another ImageIcon and the background does not appear when I run the program , however the car does.
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Gui extends JFrame {
private ImageIcon northcar = new ImageIcon("src/north.gif");
private ImageIcon usIcon = new ImageIcon("src/trafficLight.jpg");
public Gui() {
add(new JLabel(usIcon)); // background image does not appear after the i added the northcar label
add(new JLabel(northcar)); // this picture can be seen
}
public static void main(String[] args) {
Gui frame = new Gui();
frame.setTitle("TestImageIcon");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 650, 650);
frame.setVisible(true);
}
}
I heard about using a canvas but have no clue. Any ideas ?
Many Thanks
© Stack Overflow or respective owner