Why is my JPanel not repainting on my JFrame?

50 views Asked by At

I am in the process of learning GUIs in Java. Here is my custom JFrame:

package ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

import management.GameManager;

@SuppressWarnings("serial")
public class TaylorIsHungryGUI extends JFrame {

    private GameManager gameMgr;

    private GamePanel gp;

    public TaylorIsHungryGUI() {
        gameMgr = new GameManager("taylor_head.jpg", "sushi.jpg");
        setUpGUI();
    }

    private void setUpGUI() {
        setTitle("Taylor is Hungry");
        setSize(1000, 750);
        setLayout(new GridBagLayout());
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        gp = new GamePanel(gameMgr);
        gp.setPreferredSize(new Dimension(800, 650));// hardCoded sizing
        gp.setMaximumSize(new Dimension(800, 650));  // hardCoded sizing
        gp.setMinimumSize(new Dimension(800, 650));  // hardCoded sizing
        gp.setBackground(Color.GRAY);
        gp.requestFocus();
        addListeners();


        add(gp);
        setVisible(true);
    }

    private void addListeners() {
        gp.addKeyListener( new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyPressed(KeyEvent e) {
                char keyChar = e.getKeyChar();
                int keyCode = e.getKeyCode();

                if (keyCode == KeyEvent.VK_RIGHT) {
                    gameMgr.getPlayer().setX(gameMgr.getPlayer().getX() + 100);
                    gp.repaint();
                    gp.revalidate();
                }

            }

            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub

            }

        });
    }

    public static void main(String[] args) {
        new TaylorIsHungryGUI();
    }
}

And here is my custom panel:

package ui;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import management.GameManager;

@SuppressWarnings("serial")
public class GamePanel extends JPanel {

    private GameManager gameMgr;

    public GamePanel(GameManager gameMgr) {
        super();
        this.gameMgr = gameMgr;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(gameMgr.getPlayer().getFace(), gameMgr.getPlayer().getX(), 500, this);
    }
}

Question: When I hit the key for the KeyPressed Event, the image in my GamePanel does not move. Why is this? Here are some other notes:

  1. The Player object's method getFace() returns it's BufferedImage field.
  2. I was trying to use a MVC design, which is why you see things like a GameManager object.
  3. In the GameManager instantiation you see a sushi.jpg parameter. It is not being used at the moment. The GameManager object has a Player object field, which is where the taylor_head.jpg parameter is going.

Also, let me know your thoughts on how I organized the code (as in a custom JFrame and JPanel), and if you know of a different way to approach it.

0

There are 0 answers