Results 1 to 4 of 4
- 04-20-2010, 06:40 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Help Using KeyListener to Move an Object
I have some code already, I just need help implementing a keylistener.
here it is ...
Please help!!!
public static void main(String[] args) {
Pong aPongGame = new Pong();
aPongGame.setVisible(true);
}
public Pong() {
setTitle("Pong");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
createFileMenu();
createEditMenu();
createColorMenu();
createScreenSizeMenu();
createHelpMenu();
createSettingsMenu();
JMenuBar menuBar = new JMenuBar();
/*
JMenu file = new JMenu("File");
JMenuItem newGame = new JMenuItem("New Game");
newGame.addActionListener(this);
file.add(newGame);
JMenuItem quit = new JMenuItem("Quit");
file.add(quit);
*
*/
/*
JMenu settings = new JMenu("Settings");
vsHuman = new JRadioButton("Vs Human");
vsHuman.addActionListener(this);
settings.add(vsHuman);
vsComputer = new JRadioButton("Vs Computer");
vsComputer.addActionListener(this);
vsComputer.setSelected(true);
settings.add(vsComputer);
*
*/
menuBar.add(fileMenu);
menuBar.add(settingsMenu);
menuBar.add(editMenu);
menuBar.add(colorMenu);
menuBar.add(screensizeMenu);
menuBar.add(helpMenu);
add(menuBar, BorderLayout.NORTH);
ballSpace = new JPanel();
ballSpace.setBackground(Color.BLACK);
gameBoard.add(ballSpace, BorderLayout.CENTER);
firstPlayerSpace = new JPanel();
firstPlayerSpace.setBackground(Color.BLACK);
secondPlayerSpace = new JPanel();
secondPlayerSpace.setBackground(Color.BLACK);
gameBoard.add(firstPlayerSpace, BorderLayout.WEST);
gameBoard.add(secondPlayerSpace, BorderLayout.EAST);
//opponentSpace = new JPanel();
//opponentSpace.setBackground(Color.BLACK);
//gameBoard.add(opponentSpace, BorderLayout.EAST);
add(gameBoard);
}
public static void doNothing(int milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (InterruptedException e) {
System.exit(0);
}
}
private class Ball extends Thread {//not actual code inside, just testing new thread
Graphics g = ballSpace.getGraphics();
public void run() {
while (true) {
launchBall();
}
}
public void paintBall(int xCoord, int yCoord) {
g.fillOval(xCoord, yCoord, BALL_SIZE, BALL_SIZE);
doNothing(PAUSE);
repaint();
}
public void launchBall() {
g.setColor(Color.RED);
int y = 200;
for (int x = 0; x < getWidth(); x += BALL_SIZE / 5) {
y += ballAngle;
paintBall(x, y);
}
for (int x = getWidth(); x > 0; x -= BALL_SIZE / 5) {
y -= ballAngle;
paintBall(x, y);
}
}
}
private class PlayerOne extends Thread implements KeyListener {
int width = gameBoard.getWidth();
public void run() {
while (true) {
paintPaddle();
doNothing(PAUSE);
setFocusable(true);
addKeyListener(this);
}
}
public void paintPaddle() {
Graphics g = firstPlayerSpace.getGraphics();
g.setColor(Color.RED);
g.fillRect(1, 180, 5, (width / 12));
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
private class PlayerTwo extends Thread {
int width = gameBoard.getWidth();
public void run() {
while (true) {
paintPaddle();
doNothing(PAUSE);
}
}
public void paintPaddle() {
Graphics g = secondPlayerSpace.getGraphics();
g.setColor(Color.RED);
g.fillRect(1, 180, 5, (width / 12));
}
}
private class ComputerOpponent extends Thread {
int width = gameBoard.getWidth();
public void run() {
while (true) {
paintPaddle();
doNothing(PAUSE);
}
}
public void paintPaddle() {
Graphics g = opponentSpace.getGraphics();
g.setColor(Color.RED);
g.fillRect(1, 180, 5, (width / 12));
}
}
public class MenuItemEvent implements ActionListener {
public void actionPerformed(ActionEvent event) {
String menuName;
menuName = event.getActionCommand();
if (menuName.equals("Quit")) {
System.exit(0);
} else if (menuName.equals("About")) {
JOptionPane.showMessageDialog(null, "Developers:" + "\n" + "Jerrell Jones" + "\n" + "Kyle Bero" + "\n" + "Jeremiah Stephens", "About", JOptionPane.CLOSED_OPTION);
} else if (menuName.equals("Update")) {
JOptionPane.showMessageDialog(null, "HAH! There will NEVER be updates!!!");
} else if (menuName.equals("Single Player")) {
// vsHuman.setSelected(true);
versesComputer = false;
Singleplayer = true;
// vsComputer.setSelected(false);
} else if (menuName.equals("Vs Computer")) {
// vsHuman.setSelected(false);
versesComputer = true;
// vsComputer.setSelected(true);
} else if (menuName.equals("Multiplayer")) {
versesComputer = false;
player2 = true;
//Singleplayer = true;
} else if (menuName.equals("New Game")) {
if (versesComputer) {
PlayerOne firstPlayer = new PlayerOne();
firstPlayer.start();
ComputerOpponent cpu = new ComputerOpponent();
cpu.start();
Ball theBall = new Ball();
theBall.start();
} else if (Singleplayer) {
PlayerOne firstPlayer = new PlayerOne();
firstPlayer.start();
Ball theBall = new Ball();
theBall.start();
} else if (player2) {
PlayerOne firstPlayer = new PlayerOne();
firstPlayer.start();
PlayerTwo secondPlayer = new PlayerTwo();
secondPlayer.start();
Ball theBall = new Ball();
theBall.start();
}
}
/*
else if (menuName.equals("Restart")){
if(player2){
gameBoard.remove(firstPlayerSpace);
gameBoard.remove(secondPlayerSpace);
gameBoard.remove(ballSpace);
}
else if(Singleplayer){
gameBoard.remove(opponentSpace);
}
}
*
*/
}
}
-
I'd use key binding. For help, please look here: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
Also, please check out the link in my signature regarding how to use code tags in the forum. It will allow you to post readable code. Thanks and much luck!
- 04-20-2010, 06:55 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
ok i'll try to follow the tutorial, but I admit i suck at following tutorials.
Java Code:private class PlayerOne extends Thread implements KeyListener { int width = gameBoard.getWidth(); public void run() { while (true) { paintPaddle(); doNothing(PAUSE); setFocusable(true); addKeyListener(this); } } public void paintPaddle() { Graphics g = firstPlayerSpace.getGraphics(); g.setColor(Color.RED); g.fillRect(1, 180, 5, (width / 12)); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { throw new UnsupportedOperationException("Not supported yet."); } public void keyReleased(KeyEvent e) { throw new UnsupportedOperationException("Not supported yet."); } }
-
It's like any endeavor -- the more you do it, the better you get at it.
This code that uses a while (true) loop carries a risk of either locking up your GUI or calling Swing code off of the Swing Thread (EDT). You may want to use a Swing Timer instead.Java Code:private class PlayerOne extends Thread implements KeyListener { int width = gameBoard.getWidth(); public void run() { while (true) { paintPaddle(); doNothing(PAUSE); setFocusable(true); addKeyListener(this); } } public void paintPaddle() { Graphics g = firstPlayerSpace.getGraphics(); g.setColor(Color.RED); g.fillRect(1, 180, 5, (width / 12)); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { throw new UnsupportedOperationException("Not supported yet."); } public void keyReleased(KeyEvent e) { throw new UnsupportedOperationException("Not supported yet."); } }
Similar Threads
-
KeyListener - Is this what I need?
By dbashby in forum New To JavaReplies: 26Last Post: 04-18-2009, 04:14 PM -
Unfocused keylistener
By rolfrolf in forum New To JavaReplies: 3Last Post: 11-06-2008, 09:21 AM -
KeyListener Example
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:46 PM -
how to add a KeyListener
By leonard in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:44 PM -
Help with KeyListener in applet
By mathias in forum Java AppletsReplies: 1Last Post: 08-06-2007, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks