0

I'm new to the concept of BufferedImage, and have very little knowledge other than that it can allow me to animate character sprites for the pac-man replication I'm making. I've followed tutorials to get to the point I am at, but when trying to update the player's position with either a KeyListener or a rudimentary "x_coordinate++;", nothing seems to happen visually. I have noticed that the construction of my classes are a bit off, and may cause nothing to happen, but in the cases where I have managed a change in values from the console, the BufferedImage is persistent at defying my wishes.

GraphicsPanel.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GraphicsPanel extends JPanel implements KeyListener, MouseListener{
private int background_x;
private BufferedImage spriteSheet = null;
private BufferedImage player;
private Player p;
private Timer t;

public void init() {
    BufferedImageLoader loader =new BufferedImageLoader();
    try {
        spriteSheet = loader.loadImage("images/pacman.png");
    }catch(IOException e) {
        e.printStackTrace();
    }   
    this.addKeyListener(this);
    p = new Player(214,315,this);
}



public GraphicsPanel(){


    setPreferredSize(new Dimension(448,596));


    t = new Timer(70, new ClockListener(this));  

    this.setFocusable(true);                     

}
public void paintComponent(Graphics g)
{

    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.black);
    g2.fillRect(0, 0, 448, 596);

    ClassLoader cldr = this.getClass().getClassLoader();    
    String imagePath = "images/background.png";              
    URL imageURL = cldr.getResource(imagePath);             
    ImageIcon image = new ImageIcon(imageURL);              
    image.paintIcon(this, g2, 0, 50);
    init();
    t.start();
    p.draw(g2,this);
}



public void clock() {

    System.out.println(p.getX()+ " " + p.getY());

    repaint();
} 

public BufferedImage getSpriteSheet() {
    return spriteSheet;
}


@Override
public void keyPressed(KeyEvent e) {
    //simple movement to help to ensure the most clarity with errors.
    if(e.getKeyCode()==KeyEvent.VK_RIGHT)
        p.setX(p.getX()+1);
    if(e.getKeyCode()==KeyEvent.VK_LEFT)
        p.setX(p.getX()-1);
    if(e.getKeyCode()==KeyEvent.VK_UP)
        p.setY(p.getY()+1);
    if(e.getKeyCode()==KeyEvent.VK_DOWN)
        p.setY(p.getY()-1);

    repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub
}
}

Player.java

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.swing.ImageIcon;

public class Player {


private int x_coordinate;           
private int y_coordinate;   
private int xdirection;
private int ydirection;
private BufferedImage player;
private BufferedImage spriteSheet = null;




public Player(int x_coordinate, int y_coordinate,GraphicsPanel game){

    this.x_coordinate = x_coordinate;                                                       
    this.y_coordinate = y_coordinate;  


    Spritesheet ss = new Spritesheet(game.getSpriteSheet());
    player = ss.grabImage(1,1, 32, 32);
}





public void tick() {
    //to be used for smooth movement and animation
}



public void draw(Graphics g, Component c) {
    g.drawImage(player, x_coordinate, y_coordinate, null);
}

public int getX() {
    return x_coordinate;
}

public void setX(int x_coordinate) {
    this.x_coordinate = x_coordinate;
}

public int getY() {
    return y_coordinate;
}

public void setY(int y_coordinate) {
    this.y_coordinate = y_coordinate;
}   
}

BufferedImageLoader.java

 import java.awt.image.BufferedImage;
 import java.io.IOException;

 import javax.imageio.ImageIO;
 public class BufferedImageLoader {

private BufferedImage image;

public BufferedImage loadImage(String path) throws IOException {

    image = ImageIO.read(getClass().getResource(path));
    return image;
}
}

SpriteSheet.java

import java.awt.image.BufferedImage;

public class Spritesheet {

private BufferedImage image;

public Spritesheet(BufferedImage image) {
     this.image =image;


}

public BufferedImage grabImage(int col, int row, int width, int height) {
    BufferedImage img = image.getSubimage((col*32)-32, (row*32)-32, width, height);
     return img;
}
}

ClockListener.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ClockListener implements ActionListener {

GraphicsPanel f;

ClockListener(GraphicsPanel c)
{
    f = c;
}

public void actionPerformed(ActionEvent e) {
        f.clock();
}

}

GraphicsMain.java

import javax.swing.JFrame;
import javax.swing.JPanel;


public class GraphicsMain extends JFrame{

public static void main(String[] args) {
    GraphicsMain window = new GraphicsMain();
    JPanel p = new JPanel();
    p.add(new GraphicsPanel());  //  add a class that extends JPanel
    window.setTitle("PAC-MAN");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setContentPane(p);
    window.pack();
    window.setLocationRelativeTo(null);
    window.setVisible(true);

}

}
  • Are you sure you want to start the timer each time `paintComponent` is called? Also at the end of a `repaint` you will eventually get to `paintComponent`, so the only thing saving you from stackoverflow error is probably the fact that `repaint` requests are not called directly, but rather queued/coalesced (because in `paintComponent` you call `repaint`, which in turn ends up calling `paintComponent`... you get the idea). Also, file reads in the `paintComponent` - extract these somewhere else. – pafau k. May 26 '20 at 17:19
  • Does this help? [How to make an image move while listening to a keypress in Java.](https://stackoverflow.com/questions/6887296/how-to-make-an-image-move-while-listening-to-a-keypress-in-java) – Abra May 26 '20 at 17:31
  • Also, each `paintComponent` you make a call to `init`, which creates a new `Player` and sets it's starting position, so that is probably a core reason as to why the image stays in place. – pafau k. May 26 '20 at 17:32
  • Have you seen this? [How to change an image after a keyboard input in java?](https://stackoverflow.com/questions/30387154/how-to-change-an-image-after-a-keyboard-input-in-java/30387171) – Abra May 26 '20 at 17:37

0 Answers0