KeyListener not responding
Well I am working on a pong game for my computer science class but keylistener just won't work.
If someone could help me out it would be greatly appreciated.
Code:
/* Program: Pong
*
* Version: 0.1
* By: Jordan Hill
*/
/*Pong Notes
* player Paddle - drawRect(playerX-(.5*playerWidth), playerY-(.5*playerHeight), playerWidth, playerHeight);
*Study setVisible method
*/
// Imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class Pong extends JFrame implements KeyListener {
// Delay Characteristics
// Graphics Characteristics
public static int leftLimit = 50; //Left Wall
public static int rightLimit = 450; //Right Wall
public static int topLimit = 50; //Top Wall
public static int bottomLimit = 250; //Bottom Wall
public static int redNumber = 0; //Cannot exceed 255
public static int greenNumber = 0; //Cannot exceed 255
public static int blueNumber = 0; //Cannot exceed 255
public static boolean colorChange = false;
public static Color arenaColor = new Color(100,100,100);
// Ball Characteristics
public static int ballRed = 0; // Cannot exceed 255
public static int ballGreen = 0; // Cannot exceed 255
public static int ballBlue = 0; // Cannot exceed 255
public static Color ballColor = new Color(ballRed,ballGreen,ballBlue);
public static Color eraseColor = new Color(255,255,255);
public static int xDirection = 7;
public static int yDirection = 2;
public static int ballX = 270;
public static int ballY = 150;
public static int lastBallX= ballX;
public static int lastBallY = ballY;
public static int ballHeight = 7;
public static int ballWidth = 7;
public static int ballSpeed1 = 1; //In miliseconds
public static int ballSpeed2 = 0; //nano seconds
// Player One Characteristics
public static int player1Red = 0;
public static int player1Green = 0;
public static int player1Blue = 0;
public static Color player1Color = new Color(player1Red, player1Green, player1Blue);
public static int player1X = 60;
public static int player1Y = 125;
public static int lastPlayer1X;
public static int lastPlayer1Y;
public static int player1Height = 75;
public static int player1Width = 10;
public static int player1Score = 0;
public static String player1Name;
// Player Two Characteristics
public static int player2Red = 0;
public static int player2Green = 0;
public static int player2Blue = 0;
public static Color player2Color = new Color(player2Red, player2Green, player2Blue);
public static int player2X = 440;
public static int player2Y = 125;
public static int lastplayer2X;
public static int lastplayer2Y;
public static int player2Height = 75;
public static int player2Width = 10;
public static int player2Score = 0;
public static String player2Name = "Computer";
// Extra Feature Variables
public static boolean scored = false;
public static int paddleSpeed = 4;
// Containers and Components
public static CoolPane arena;
public static JPanel titlePane;
public static JPanel pane1;
public static JPanel pane2;
public static JPanel mainPane;
public static JButton startGame;
public static JDialog goalScored;
public static JTextField inputRed;
public static JTextField inputGreen;
public static JTextField inputBlue;
public static JLabel titleLabel;
public void keyPressed(KeyEvent e){
String keyname= e.getKeyText(e.getKeyCode());
System.out.println(keyname);
if (keyname.equals("Up")) {
if (player1Y > topLimit){
player1Y-=10;
}
arena.repaint();
}
else if (keyname.equals("Down")) {
if (player1Y < bottomLimit){
player1Y+=10;
}
arena.repaint();
}
}
public void keyReleased(KeyEvent e) {
//Ignore
}
public void keyTyped(KeyEvent e) {
//Ignore
}
public Pong() {
super("Jordan Hill's Pong");
//JButtons
startGame = new JButton("Start");
ButtonListener startGameListener = new ButtonListener("start");
startGame.setActionCommand("startGame");
startGame.addActionListener(startGameListener);
startGame.setForeground(Color.red);
//JLabels
titleLabel = new JLabel("Welcome to Jordan Hill's Pong!");
titleLabel.setForeground(Color.black);
//JPanes
mainPane = new JPanel();
titlePane = new JPanel();
pane1 = new JPanel();
arena = new CoolPane();
mainPane.setLayout(new BorderLayout());
mainPane.add(startGame, BorderLayout.SOUTH);
mainPane.add(arena, BorderLayout.CENTER);
setContentPane(mainPane);
}
class ButtonListener extends AbstractAction {
public ButtonListener(String text) {
super(text);
}
public void actionPerformed(ActionEvent e) {
String sourceButton = e.getActionCommand();
if (sourceButton.equals("startGame")) {
repaint();
}
}
}
class CoolPane extends JPanel{
public CoolPane() {
super();
}
public void paint (Graphics g)
{
// Draw the ball
g.setColor(arenaColor);
g.fillRect(leftLimit,topLimit,ballWidth + 8 + rightLimit-leftLimit,ballHeight + 8 + bottomLimit-topLimit);
g.setColor(eraseColor);
g.fillOval(lastBallX,lastBallY,ballWidth,ballHeight);
g.setColor(ballColor);
g.fillOval(ballX,ballY,ballWidth,ballHeight);
// Draw player1 paddle
g.setColor(player1Color);
g.fillRect(player1X, player1Y, player1Width, player1Height);
g.setColor(eraseColor);
g.fillRect(player1X, player1Y, player1Width, player1Height);
}
}
public static void main(String args[]) {
JFrame frame = new Pong();
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
while(!(scored)) {
lastBallX = ballX - (xDirection);
lastBallY = ballY - (yDirection);
// Move X and check for limits
if ((ballX > leftLimit) && (ballX < rightLimit)) {
ballX = ballX + xDirection;
}
else {
xDirection = xDirection*-1;
ballX = ballX + xDirection;
}
// Move Y and check for limits
if ((ballY < bottomLimit) && (ballY > topLimit)) {
ballY = ballY + yDirection;
}
else {
yDirection = yDirection*-1;
ballY = ballY + yDirection;
}
arena.repaint();
// Delay loop.
Thread gotoSleep = new Thread();
gotoSleep.start();
try {
Thread.sleep(ballSpeed1,ballSpeed2);
} catch (InterruptedException e) {
;//Ignore
}
}
}
}