2

I have been coding a game in Java for a while, and I got stuck on Image handling. I don't know why but it worked in all my previous games. Do I have it right? Thanks for advice!

public class Game extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;

    private Timer gameLoop;
    public static final int WIDTH = 800;
    public static final int HEIGHT = 500;
    public static final Dimension SCREEN_SIZE = new Dimension(WIDTH, HEIGHT);
    public static final int FRAMERATE = 60;
    public static final String TITLE = "MedievalGame";

    public BufferedImage[] images = new BufferedImage[4];

    public int x = 100;
    public int y = 100;
    public int width = 100;
    public int height = 100;

    public Game() {
        this.setPreferredSize(SCREEN_SIZE);
        this.setFocusable(true);

        try {
            images[0] = ImageIO.read(getClass().getResourceAsStream("path"));
        } catch (IOException e) {
            e.printStackTrace()
        }

        gameLoop = new Timer(1000 / FRAMERATE, this);
        gameLoop.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(images[0], x, y, width, height, null);
    }

    public void actionPerformed(ActionEvent e) {
        this.repaint();
    }

    public static void main(String[] args) {
        JFrame window = new JFrame(TITLE);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.add(new Game());
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }
}

I checked many similar questions, but none solved my problem.

tomasxboda
  • 539
  • 3
  • 15
  • `I don't know why but it worked in all my previous games.` - so look at the code in your other programs to see what you are doing differently. And post a proper [mcve] that demonstrates the problem. The context of how you use the panel with the custom painting is just as important as the painting logic itself. – camickr Jun 02 '17 at 18:28
  • @camickr I have cleaned up my entire Eclipse library for some purpose, so I can't look it up, but I know that I was doing the exact same thing. So the code provided is OK? Moreover, it was working an hour ago when I wasn't using an Array for animation but only one ImageIcon for a gif file. But suddenly, as I started to use Arrays, it doesn't seem to work. I am using a `JPanel` on a `JFrame`, everything is painted on the `JPanel`, `paintComponent(Graphics g)` method and also `super.paintComponent(g)` in it. Hope I clarified it a bit. – tomasxboda Jun 02 '17 at 18:34
  • *So the code provided is OK*, then it's working, isn't it? If it's not, then post a valid [mcve] as stated before. *"I am using a `JPanel` on a `JFrame`..." "...Hope I clarified it a bit"*. A MCVE or [SSCCE](http://sscce.org/) might better explain it. In the code snippet posted, there's not `@Override` or `super.paintComponent(g);` call. If you want real help, post a real runnable example that we can copy-paste and see the same problem as you :) – Frakcool Jun 02 '17 at 18:55
  • @Frakcool I edited the code, I will be really happy if you solve my problem :) – tomasxboda Jun 02 '17 at 19:20

3 Answers3

1

Your code works fine for me:

enter image description here

All I did was add the image to the same package of the .java file, I'm not using an IDE but you're probably doing it, so you can manage the package...

I'm more than sure it's a path issue, than a code one, however your images will become embedded-resources once you export them with your JAR file, so it's wise to treat them like if they already were and load them as shown here

Also read: Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (The general consensus says yes and to override getPreferredSize() methods)...

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • I tried to use `BufferedImage` instead, the path seems to be OK since the error isn't showing up, but when I try to print `enemyImages[0]`, it says null. I tried so many paths so far... – tomasxboda Jun 02 '17 at 19:51
  • *"...since the error isn't showing up"* Where is that `try-catch`? Post the code on how you tried using the `BufferedImage` and the embedded-resource option too. – Frakcool Jun 02 '17 at 19:54
  • Actually, I assign the image file to the array, it doesn't show any error so the file must exist, but when I check if the file exists with `file.exists()`, it returns false... So where's the problem then? I don't understand... – tomasxboda Jun 02 '17 at 20:30
  • @Erninger what's the path you're using? And your project structure? If you're using the embedded resource way, it will return false and it's ok if it returns false. Because it's not a file anymore but, well, an embedded resource – Frakcool Jun 03 '17 at 03:43
0

I run your code here and run normally

Check if the image that you're loading isn't null, so enemyImages[0] can't be null

Make sure too you are not calling the existing JFrame method "paintComponents", so change the name of your method paintComponent to another name, and run it again

Hugs

Rodrigo João Bertotti
  • 5,179
  • 2
  • 23
  • 34
  • I am sure that everything you noted is right, the file even exists, since I checked it with `file.exists()`, but still, doesn't work. – tomasxboda Jun 02 '17 at 19:32
0

I figured out what the problem was. Everything about the code was correct, but the images were somehow broken. I tried using other images, and it worked. Thank you all for your time and help.

tomasxboda
  • 539
  • 3
  • 15