Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a pacman image

I am a student in Ap Computer Science and the project for the class is to create a game of your choice. I am creating a pacman game. We haven't worked much with GUI so I am still very new to the whole concept, but this is what I have so far:

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;


public class GameHandler extends JFrame{


JPanel jpanel = new JPanel();
Graphics g;
BufferedImage pacman;
int frame;
int direction;
int x, y;
//final int STEP = 2; If you want to change the speed at which pacman moves accross the screen;

public void popupWindow() {
    setSize(400, 400);
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame = 0;
    direction = KeyEvent.VK_LEFT;
    x = 300;
    y = 300;

    try {
        pacman = ImageIO.read(new File("pacman.png"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();


    }
          image();
}

public void keyPressed(KeyEvent e) {
    direction = e.getKeyCode();
}


public void update() {
    frame++;
    if (frame > 2) {
        frame = 0;
    }

    switch(direction) {
        case KeyEvent.VK_LEFT:
            x--;
            break;
        case KeyEvent.VK_RIGHT:
            x++;
            break;
        case KeyEvent.VK_UP:
            y--;
            break;
        case KeyEvent.VK_DOWN:
            y++;
            break;
    }
}

public void image() {   
    super.paintComponents(g);
    g.drawImage(pacman.getSubimage(frame*10, 0, 37, 30), x, y, null);
}
}

My problem is that I am not able to output this image on to the screen (I know my g.drawImage dimensiones are a off due to the fact I couldn't tweak it without any output)

When I try to call the method image() in the popupWindow() method I get this error:

Exception in thread "main" java.lang.NullPointerException
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:70)
at java.awt.Container.paintComponents(Container.java:1890)
at GameHandler.image(GameHandler.java:69)
at GameHandler.popupWindow(GameHandler.java:38)
at Main.main(Main.java:7)

I tried calling the image method in different spots and in the try/catch as well. I am lost.

like image 655
user1881401 Avatar asked Sep 25 '26 23:09

user1881401


1 Answers

public void image() {   
    super.paintComponents(g);
    g.drawImage(pacman.getSubimage(frame*10, 0, 37, 30), x, y, null);
}

g isn't set yet for use in this method. Your class defines Graphics g; but it isn't set anywhere! So, you ask, how do I set it? Where does a Graphics object come from?


The fastest answer for your current code base could be:

JPanel jpanel = new JPanel();
Graphics g = jpanel.getGraphics();

but this is not an ideal solution at all.


Usually this code pattern is found in paintComponent.

What you should be aiming for is something like this:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(...);
}

You can override paintComponent on any JComponent, like a JPanel (which is a subclass of JComponent). So, what I recommend to you is

  • Add a new class that overrides JPanel
  • Use JFrame.setContentPane() to set it as the content pane for the main JFrame
  • Override paintComponent in that panel
  • Use that panel to make any UI changes (only top-level stuff on the JFrame, everything else on the content pane)
like image 78
Ben Avatar answered Sep 27 '26 12:09

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!