Rachel Carlson
Rachel Carlson

Reputation: 1

Adding jpanel to my frame wont work

I am creating a simple java game and I am pretty new to this. My problem is that i've created objects and a player for my game that i want to add to the screen. What I've done is that I've added these objects to one JPanel that I later add on the frame. What only shows up are my objects but the player does not show up. Ive tried creating two seperate jpanels that i add on the frame, but that does not work either

public GameViewer(Board b) {
        PlayerPiece player = new PlayerPiece();

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(player);
        panel.addKeyListener(player);
        panel.add(new GameComponent(b));

        frame.setLayout(new BorderLayout());
        //background image// frame.add(image, BorderLayout.CENTER); ****
        frame.setSize(WIDTH,HEIGHT);

        frame.add(panel, BorderLayout.CENTER);

more imports *

      public class PlayerPiece extends JPanel implements ActionListener, KeyListener{
     Timer playTimer = new Timer(5,this);
     int positionX = 150, positionY = 360, speedX = 0, speedY = 0;

     public PlayerPiece(){
          playTimer.start();
         addKeyListener(this);
         setFocusable(true); 
         setFocusTraversalKeysEnabled(false);
}
     public void paintComponent(Graphics g)
{
         super.paintComponent(g);
         g.setColor(Color.RED);
         g.fillRect(positionX, positionY,50,30);
     }

       Further on I only have keyevents

@Override public void actionPerformed(ActionEvent e) {

          if (positionX < 0){ //If the player is outside of the frame stop it
              speedX = 0;
              positionX = 0;
    }

          if (positionX > 300){ //Right side but outside of the screen, block it
              speedX = 0;
              positionX = 300;
    }

          if (positionY < 0){
              speedY = 0;
              positionY = 0;
    }

          if (positionY > 360){
              speedY = 0;
              positionY = 360;
    }

          positionX = positionX + speedX;
          positionY = positionY + speedY;
          repaint();
}

@Override
      public void keyPressed(KeyEvent e) {
          int keyCode = e.getKeyCode(); //Get the button pressed
              if(keyCode == KeyEvent.VK_RIGHT) { //Go Right
                  speedX = 2;
                  speedY = 0;
        }
              if(keyCode == KeyEvent.VK_DOWN) { //Go Down
                  speedX = 0;
                  speedY = 2;
        }
              if(keyCode == KeyEvent.VK_LEFT) { //Go Left
                  speedX = -2;
                  speedY = 0;
        }
              if(keyCode == KeyEvent.VK_UP) { //Go up
                  speedX = 0;
                  speedY = -2;
        }
}

@Override
            public void keyReleased(KeyEvent e) {
                speedX = 0;
                speedY = 0;
}
@Override
            public void keyTyped(KeyEvent e) {
}

  }

Upvotes: 0

Views: 218

Answers (1)

steven35
steven35

Reputation: 4017

You didn't tell us what PlayerPiece was so I replaced it with a button. The code you posted should work so the problem must be in PlayerPiece.

So to fix it, replace PlayerPiece with another object like a Button and make sure the buttons shows up. Then look at your PlayerPiece class and find the problem there.

UPDATE: I think your problem is that the PlayerPiece (which is just another JPanel) shows outside of the frame. Try the below code and you'll see it does show up. I omitted the irrelevant parts of the code, such as event listeners.

import java.awt.BorderLayout;
import java.awt.Button;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Test");

        PlayerPiece player = new PlayerPiece();

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(player);

        frame.setLayout(new BorderLayout());
        frame.setSize(400,400);

        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class PlayerPiece extends JPanel {
     int positionX = 150, positionY = 150;

     public void paintComponent(Graphics g) {
         super.paintComponent(g);
         g.setColor(Color.RED);
         g.fillRect(positionX, positionY,50,30);
     }
}

Upvotes: 0

Related Questions