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
