Okay, so I’ve been looking over this for about 2 hours and I simply cannot find out what’s wrong. Here’s the code for both of my class files:
Skeleton.class
import javax.swing.*;
public class Skeleton {
public static void main(String[] args) {
JFrame frame = new JFrame("Map Editor");
frame.setVisible(true);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
Draw object = new Draw();
frame.add(object);
object.drawing();
}
}
Draw.class
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Draw extends JPanel {
private BufferedImage walkable;
public void drawing() {
System.out.println("good");
BufferedImage walkable = null;
try {
walkable = ImageIO.read(new File("Sprites/Animated.png"));
} catch(IOException e) {
System.out.println("failed");
}
System.out.println(walkable.getWidth());
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(walkable, 0, 0, this);
g.setColor(Color.red);
g.drawRect(10, 10, 10, 10);
}
}
As you can see, I have some System.out.println()'s scattered throughout the code. No exception is thrown when the image is loaded with ImageIO.read(), and when I query the width of the image after loading it is 160 which is correct. The red rectangle also draws, but the image just will not. I’m sure I’m missing something silly, but after looking over it so many times, I’m at a dead end. Any suggestions?
Edit: If it helps, here is a screenshot of the program running with the command prompt and debug outputs.