I'm trying to make a simple gam开发者_StackOverflowe. I can't figure out how to add an image to a JFrame
.
Some info: I'm on a mac OS X. I am programing in Java.
The question: how do I add an image to a JFrame
in a way to be able to move it easily?
- create JFrame
- create JPanel and add it to JFrame
- in paint method from your JPanel add Graphics.drawImage method to display image in x, y position
edited:
Panel example:
public class ImageInPanel extends JPanel {
private BufferedImage image;
private int x;
private int y;
public void paint(Graphics g) {
g.drawImage(image, x, y, this);
}
// getters and setters
}
Create a JLabel with an ImageIcon. Then you can move the image around the frame by using:
label.setLocation(...);
精彩评论