There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen boot bsod computer connection crash css dell display driver drivers email error ethernet excel explorer firefox firefox 3 game hard drive internet internet explorer itunes laptop linux malware monitor network networking nvidia outlook outlook 2003 outlook express partition password printer problem router slow software sound trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Depeserate for help! (Java)


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
aaaman's Avatar
Junior Member with 3 posts.
 
Join Date: May 2006
Experience: Intermediate
29-May-2006, 04:45 PM #1
Depeserate for help! (Java)
Hi, im trying to create a cheap version of a tetris game where i wanna use the keyboard instead of having buttons to do all the functions (left, right,roate,drop) and i need help as do how can i implement keyListener as it is really confusing.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
* A ButtonsPanel contains "left", "right", "rotate" and "drop" buttons for
* controlling a Game.
*
* @author Ryan Heise
*/
public class ButtonsPanel extends JPanel implements ActionListener //TextListener

{
/**
* The Game to control.
*/
private Game game;

/**
* The left button.
*/
private JButton leftButton;

/**
* The right button.
*/
private JButton rightButton;

/**
* The rotate button.
*/
private JButton rotateButton;

/**
* The drop button.
*/
private JButton dropButton;

/**
* Constructs a new ButtonsPanel for controlling the given Game.
*
* @param game the Game to control.
*/
public ButtonsPanel(Game game)
{
this.game = game;

setLayout(new GridLayout(0,2));

leftButton = new JButton("Left");
leftButton.addActionListener(this);
// leftButton.addKeyListener(this);
add(leftButton);

rightButton = new JButton("Right");
rightButton.addActionListener(this);
//rightButton.addKeyListener(this);
add(rightButton);

rotateButton = new JButton("Rotate");
rotateButton.addActionListener(this);
// rotateButton.addActionListener(this);
add(rotateButton);

dropButton = new JButton("Drop");
// dropButton.addKeyListener(this);
dropButton.addActionListener(this);
add(dropButton);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == leftButton)
game.moveLeft();

if (event.getSource() == rightButton)
game.moveRight();

if (event.getSource() == rotateButton)
game.rotate();

if (event.getSource() == dropButton)
game.drop();
}
// public interface keyPressed(Keyevent event)
// {

// if (event.getInputMap().put(KeyStroke.getKeyStroke("VK_LEFT"),"pressed"))
// game.moveLeft();

// if (event.getInputMap().put(keyStroke.getKeyStroke("VK_RIGHT","pressed"))
// game.moveRight();

// if (event.getInputMap().put(keyStroke.getKeyStroke("VK_DOWN","pressed"))
// game.moveDrop();

// if (event.getInputMap().put(keyStroke.getKeyStroke("VK_UP","pressed"))
// game.moveRotate();

// }
}

Anyone got any ideas to change it?
bpmurray's Avatar
Senior Member with 103 posts.
 
Join Date: Jun 2003
Location: Ireland
Experience: Advanced
29-May-2006, 06:29 PM #2
First, you must make your class able to receive keyboard messages. This can only happen when it has focus, so you need to add this.setFocusable(true); to your constructor.

Now, you also need to be a keylistener, so you need to "implement" KeyListener. I'm unclear as to why you need the buttons on the panel, but you can make the entire panel react to the keyboard. Of course, this means you have to have the Keypressed/released/typed methods. Something like:
Code:
public class ButtonsPanel extends JPanel implements ActionListener, KeyListener

{
	/**
	 * The Game to control.
	 */
	private Game game;

	/**
	 * The left button.
	 */
	private JButton leftButton;

	/**
	 * The right button.
	 */
	private JButton rightButton;

	/**
	 * The rotate button.
	 */
	private JButton rotateButton;

	/**
	 * The drop button.
	 */
	private JButton dropButton;

	/**
	 * Constructs a new ButtonsPanel for controlling the given Game.
	 * 
	 * @param game
	 *            the Game to control.
	 */
	public ButtonsPanel(Game game) {
		this.game = game;
		this.setFocusable(true);

		setLayout(new GridLayout(0, 2));
		this.addKeyListener(this);
		
		leftButton = new JButton("Left");
		leftButton.addActionListener(this);
		leftButton.addKeyListener(this);
		add(leftButton);

		rightButton = new JButton("Right");
		rightButton.addActionListener(this);
		rightButton.addKeyListener(this);
		add(rightButton);

		rotateButton = new JButton("Rotate");
		rotateButton.addActionListener(this);
		rotateButton.addActionListener(this);
		add(rotateButton);

		dropButton = new JButton("Drop");
		dropButton.addKeyListener(this);
		dropButton.addActionListener(this);
		add(dropButton);
	}

	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == leftButton)
			game.moveLeft();

		if (event.getSource() == rightButton)
			game.moveRight();

		if (event.getSource() == rotateButton)
			game.rotate();

		if (event.getSource() == dropButton)
			game.drop();
	}
	
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		if(key == KeyEvent.VK_LEFT)
			game.moveLeft();
		if(key == KeyEvent.VK_RIGHT)
			game.moveRight();
		if(key == KeyEvent.VK_UP)
			game.rotate();
		if(key == KeyEvent.VK_DOWN)
			game.drop();
	}
	public void keyTyped(KeyEvent e) {}
	
	
	// public interface keyPressed(Keyevent event)
	// {

	// if (event.getInputMap().put(KeyStroke.getKeyStroke("VK_LEFT"),"pressed"))
	// game.moveLeft();

	// if (event.getInputMap().put(keyStroke.getKeyStroke("VK_RIGHT","pressed"))
	// game.moveRight();

	// if (event.getInputMap().put(keyStroke.getKeyStroke("VK_DOWN","pressed"))
	// game.moveDrop();

	// if (event.getInputMap().put(keyStroke.getKeyStroke("VK_UP","pressed"))
	// game.moveRotate();

	// }
}
In the appropriate key method you should process the event, something like
__________________
There are 10 kinds of people: those that understand binary and those that don't.
aaaman's Avatar
Junior Member with 3 posts.
 
Join Date: May 2006
Experience: Intermediate
29-May-2006, 09:04 PM #3
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
* A ButtonsPanel contains "left", "right", "rotate" and "drop" buttons for
* controlling a Game.
*
* @author Ryan Heise
*/
public class ButtonsPanel extends JPanel implements ActionListener, KeyListener
{
/**
* The Game to control.
*/
private Game game;

/**
* The left button.
*/
private JButton leftButton;

/**
* The right button.
*/
private JButton rightButton;

/**
* The rotate button.
*/
private JButton rotateButton;

/**
* The drop button.
*/
private JButton dropButton;

/**
* Constructs a new ButtonsPanel for controlling the given Game.
*
* @param game the Game to control.
*/
public ButtonsPanel(Game game)
{
this.game = game;
this.addKeyListener(this);


setLayout(new GridLayout(0,2));

leftButton = new JButton("Left");
leftButton.addActionListener(this);
leftButton.addKeyListener(this);
add(leftButton);

rightButton = new JButton("Right");
rightButton.addActionListener(this);
rightButton.addKeyListener(this);
add(rightButton);

rotateButton = new JButton("Rotate");
rotateButton.addActionListener(this);
rotateButton.addKeyListener(this);
add(rotateButton);

dropButton = new JButton("Drop");
dropButton.addActionListener(this);
dropButton.addKeyListener(this);
add(dropButton);
}


public void actionPerformed(ActionEvent event)
{
if (event.getSource() == leftButton)
game.moveLeft();

if (event.getSource() == rightButton)
game.moveRight();

if (event.getSource() == rotateButton)
game.rotate();

if (event.getSource() == dropButton)
game.drop();
}

public void keyPressed(KeyEvent event) {
int key = event.getKeyCode();

if(key == KeyEvent.VK_LEFT)
game.moveLeft();

if(key == KeyEvent.VK_RIGHT)
game.moveRight();

if(key == KeyEvent.VK_UP)
game.rotate();

if(key == KeyEvent.VK_DOWN)
game.drop();

public void keyTyped(KeyEvent event){}

}
}


ok ive changed it however public void keyTyped(KeyEvent event){} is a illegal expression and i cant get it to be working

Last edited by aaaman : 29-May-2006 11:08 PM.
bpmurray's Avatar
Senior Member with 103 posts.
 
Join Date: Jun 2003
Location: Ireland
Experience: Advanced
30-May-2006, 05:18 AM #4
What development system are you using? Do you see any other error marks before the illegal expression error? I can see the typo straight away: there's a misplaced closing brace "}" - if you can't find this, you shouldn't be trying to write this app!
aaaman's Avatar
Junior Member with 3 posts.
 
Join Date: May 2006
Experience: Intermediate
30-May-2006, 05:20 AM #5
ah i use blueJ its the ****test thing ever and i cbf getting netbeans, i fixed to problem i forgot to place all three methods, thanks alot pal for ya help
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:21 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.