I am coding a Java pong game I am stuck on how to get the player paddle to move. I for some reason cannot get the program to notice the key inputs.
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Pong {
Board board = new Board();
public void frame() {
JFrame b = new JFrame("Pong");
b.setSize(905,705);
b.setLocation(300,60);
b.setResizable(false);
b.setVisible(true);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.add(board);
}
public static void main(String[] args) {
Pong start = new Pong();
start.frame();
}
}
The board class
public class Board extends JPanel{
public int playerScore = 0;
public int opponentScore = 0;
Player player = new Player();
int test = 1;
private Timer timer;
private int time = 100;{
timer = new Timer(time, player);
timer.start();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
board(g);
g.setColor(Color.WHITE);
// player.playerGenerate();
g2d.fill(player.player);
g.fillRect(30, test, 20, 20);
player.paint(g);
control();
//repaint();
}
public void board(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.black);
g.fillRect(0, 0, 900, 900);
Stroke stroke1 = new BasicStroke(4f);
g2d.setColor(Color.white);
g2d.setStroke(stroke1);
g2d.drawRect(20, 50, 850, 600);
g2d.setColor(Color.white);
float[] dashingPattern2 = {10f, 4f};
Stroke stroke2 = new BasicStroke(4f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 1.0f, dashingPattern2, 0.0f);
g2d.setStroke(stroke2);
g2d.drawLine(435, 50, 435, 650);
g.setFont(new Font("arial",Font.PLAIN,30));
g.drawString(""+playerScore, 20, 35);
g.drawString(""+opponentScore, 855, 35);
}
public void control() {
if (player.down == true) {
player.playerYpos = player.playerYpos = player.playerYpos -10;
repaint();
}
}
}
And finally the player class.
public class Player extends JPanel implements KeyListener,ActionListener{
//Board theBoard = new Board();
public boolean down = false;
public boolean up = false;
public int playerXpos = 45;
public int playerYpos = 300;
public int playerWidth = 15;
public int playerHeight = 80;
Rectangle player = new Rectangle(playerXpos,playerYpos,playerWidth,playerHeight);
// Board theBoard = new Board();
public void playerGenerate() {
playerXpos = 45;
playerYpos = 300;
playerWidth = 15;
playerHeight = 80;
//Rectangle r = new Rectangle(playerXpos,playerYpos,playerWidth,playerHeight);
}
@Override
public void actionPerformed(ActionEvent e) {
if (down) {
down = true;
if(up != true) {
down = true;
}
else
{
up = true;
down = false;
}
}
if (up) {
up = true;
if(down != true) {
up = true;
}
else
{
up = false;
down = true;
}
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP) {
for(int r = playerYpos;r >=0;r--) {
if(r==0) {
playerYpos = playerYpos -20;
}
else {
playerYpos = playerYpos -1 ;
}
if(playerYpos < 50){
playerYpos = 50;
}
}
//repaint();
}
if(e.getKeyCode() == KeyEvent.VK_DOWN) {
for(int r = playerYpos;r >=0;r--) {
if(r==0) {
playerYpos = playerYpos +20;
}
else {
playerYpos = playerYpos -1 ;
System.out.print("down");
}
if(playerYpos > 800){
playerYpos = 800;
}
}
//repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
I tried googling around and looked at code that went with a similar approach of implementing action/key listener in the player/paddle class. I assume there is something minor and small I am just not seeing.
You have just wrote the method names. You need to register action/key listener to a particular swing component which you want to listen.
Suppose if your JComponent is JButton named btnPlayer you need to register the action performed event like :
private void btnPlayerActionPerformed(java.awt.event.ActionEvent e) {....
...}
You can add action listener from swing design. Right click on the JComponent -> Events -> ActionPerformed.
It will register click event for the component. Adding events through the properties is a better option.