I am making a program where I need to display an image. I am using the ImageIcon and Image classes in order to do this. I declared the ImageIcon in the constuctor and then assigned the images value through i.getImage(). Everything but the image seems to be loading fine. Here is my code:
UPDATE: I have the image in the same directory as the code. I am using a mac and I have tried "image.png", "./image.png", "Users/myStuff/Documents/workspace/Game/src/image.png". Niether of these have worked.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Game extends JFrame {
int x = 100, y = 100;
private Image dbimage;
private Graphics dbg;
Image image;
class AL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_LEFT) {
if (x <= 20) {
x = 20;
} else {
x -= 5;
}
} else if (keyCode == e.VK_RIGHT) {
if (x >= 230) {
x = 230;
} else {
x += 5;
}
} else if (keyCode == e.VK_UP) {
if (y <= 20) {
y = 20;
} else {
y -= 5;
}
} else if (keyCode == e.VK_DOWN) {
if (y >= 230) {
y = 230;
} else {
y += 5;
}
}
}
}
public Game() {
//load up image
ImageIcon i = new ImageIcon("image.png");
image = i.getImage();
//set up properties
addKeyListener(new AL());
setTitle("Game");
setSize(250, 250);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.CYAN);
setVisible(true);
}
public void paint(Graphics g) {
dbimage = createImage(getWidth(), getHeight());
dbg = dbimage.getGraphics();
paintComponent(dbg);
g.drawImage(dbimage, 0, 0, this);
}
public void paintComponent(Graphics g) {
g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 30));
g.setColor(Color.MAGENTA);
g.drawString("Hello World!", 50, 50);
g.setColor(Color.RED);
g.drawImage(image, 100, 100, this);
repaint();
}
public static void main(String[] args) {
new Game();
}
}
Significant issues with your code above...
first and foremost, if your image is with your class files, then don't get it as a File, but rather get it as a resource as MadProg shows (and as most similar questions on this site will tell you):
Next of all, never draw directly within the JFrame and certainly not within its paint method. Instead check out the Swing drawing tutorials and follow their lead: draw within the paintComponent method of a JPanel, but only after calling the super's paintComponent method so that the painting can continue down the line of the painting chain.:
I prefer to override the JPanel's getPreferredSize when fixing the sizes of my GUI, something like:
Put all together, something like so might work:
Next look up Key Bindings to help you to do animation with key strokes with this GUI