-
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
}
}
}
}
-
I cannot find, anywhere in your code, the magic invocation of the .addKeyListener(this) method of your JFrame or what ever other container will be the Listener.
Make sure that this container has, gets, or keeps focus ...
For some help on how to write a KeyListener, take a look at:
http://java.sun.com/docs/books/tutor...ylistener.html
-
aha! Thank you very much. I deeply appreciate it.
-
You should add a focusListener to the component as well as a keyListener
Kind regards,
Noel
Efficiency is intelligent laziness
Similar Threads
-
Replies: 6
Last Post: 01-07-2010, 11:36 PM
-
By StubruFreak in forum Java
Replies: 0
Last Post: 01-25-2006, 01:41 PM
-
By Peter Mount in forum Database
Replies: 0
Last Post: 11-08-2001, 11:37 PM
-
By Marcus Nyholm in forum Web
Replies: 0
Last Post: 08-21-2000, 04:48 AM
-
Replies: 1
Last Post: 05-24-2000, 09:55 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks