So i'm a java student in 9th grade and my code gives me an error i have never encountered before...
time is off the essence, so quick help is loved!:
it says:
Play.java:5: Play is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent)
public class Play extends JApplet implements KeyListener, FocusListener, MouseListener {
and i'll post my code...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Play extends JApplet implements KeyListener, FocusListener, MouseListener {
private JPanel main, fini, game;
private CardLayout cards;
private Container c;
private int index, max;
private String player;
public Play( ) {
index = 0;
max = 3;
}
public void init( ) {
c = this.getContentPane();
cards = new CardLayout( );
c.setLayout( cards );
main = new MainPanel();
main.addMouseListener(this);
game.addMouseListener(this);
fini.addMouseListener(this);
c.add( main, "Greeting" );
c.add( game, "Game" );
c.add( fini, "End" );
c.addFocusListener(this);
c.addKeyListener(this);
getContentPane().setBackground( Color.blue );
}
public Insets getInsets() {
return new Insets ( 0, 0, 0, 0 );
}
class MainPanel extends JPanel implements ActionListener {
private JLabel name, grade, greeting;
private JTextField gradeText, nameText;
private JButton go;
public MainPanel( ) {
setLayout(new GridLayout(6, 1));
greeting = new JLabel("Welcome to the game of Navigate!", JLabel.CENTER);
this.add(greeting);
name = new JLabel("Name");
this.add(name);
nameText = new JTextField(10);
this.add(nameText);
grade = new JLabel("Grade");
this.add(grade);
gradeText = new JTextField(10);
this.add(gradeText);
go = new JButton("GO!");
go.addActionListener ( this );
this.add(go);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
public void actionPerformed ( ActionEvent evt ) {
String command = evt.getActionCommand();
if ( command.equals( "GO!")) {
cards.next(c);
}
}
}
class Game extends JPanel implements KeyListener, FocusListener, MouseListener {
private PlayCell [][] board;
private int x = 0, y = 0;
private int numberofbad = 4;
private boolean done = false, firsttime;
private PaintPanel canvas;
public Game( ) {
canvas = new PaintPanel();
setContentPane(canvas);
canvas.setBackground(Color.gray);
canvas.addFocusListener(this);
canvas.addKeyListener(this);
canvas.addMouseListener(this);
SetUpBoard( );
}
public void SetUpBoard( ) {
x = y = 0;
numberofbad = 4;
done = false;
firsttime = true;
board = new PlayCell[6][6];
for ( int i = 0; i < 6; i++ )
for ( int k = 0; k < 6; k++ )
board[i][k] = new PlayCell();
PlaceBadCells( );
}
class PaintPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
for ( int i = 0; i < 6; i++ )
for ( int k = 0; k < 6; k++ )
board[i][k].drawCell( g, i * 40 + 117, k * 40 + 17, 36, 36 );
board[x][y].changeToOnCell( );
board[x][y].changeToCellVisited( );
board[x][y].drawCell( g, x * 40 + 117, y * 40 + 17, 36, 36 );
}
}
public void focusGained(FocusEvent evt) {
firsttime = false;
canvas.repaint();
}
public void focusLost(FocusEvent evt) {
firsttime = true;
canvas.repaint();
}
public void mousePressed(MouseEvent evt) {
canvas.requestFocus();
}
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void keyTyped( KeyEvent e ) {}
public void keyPressed( KeyEvent e ) {
Graphics g = getGraphics();
int value = e.getKeyCode();
if ( value == KeyEvent.VK_SPACE ) {
SetUpBoard ( );
firsttime = false;
canvas.repaint ( );
}
else if (( value == KeyEvent.VK_RIGHT || value == KeyEvent.VK_LEFT ||
value == KeyEvent.VK_UP || value == KeyEvent.VK_DOWN ) && !done ) {
board[x][y].changeToOffCell( );
board[x][y].drawCell( g, x * 40 + 117, y * 40 + 17, 36, 36 );
if ( value == KeyEvent.VK_RIGHT && x < 5 )
x++;
else if ( value == KeyEvent.VK_LEFT && x > 0 )
x--;
else if ( value == KeyEvent.VK_UP && y > 0 )
y--;
else if ( value == KeyEvent.VK_DOWN && y < 5 )
y++;
board[x][y].changeToOnCell( );
board[x][y].changeToCellVisited( );
board[x][y].drawCell( g, x * 40 + 117, y * 40 + 17, 36, 36 );
if ( board[x][y].checkForBadCell ( ) || (x == 5 && y == 5) ) {
done = true;
DrawFinalBoard(g);
}
}
}
public void keyReleased( KeyEvent e ) {}
public void PlaceBadCells( ) {
int badx, bady;
for ( int v = 1; v <= numberofbad; v++ ) {
badx = (int)( Math.random() * 4 );
bady = (int)( Math.random() * 4 );
if ( (badx != 0 || bady != 0) && !board[badx][bady].checkForBadCell() &&
(badx != 3 || bady != 3) )
board[badx][bady].changeToBadCell( );
else
v--;
}
}
public void DrawFinalBoard( Graphics g ) {
for ( int i = 0; i < 6; i++ )
for ( int k = 0; k < 6; k++ ) {
board[i][k].changeToCellVisited ( );
board[i][k].drawCell( g, i * 40 + 117, k * 40 + 17, 36, 36 );
WinOrLoseMessage ( g );
}
}
public void WinOrLoseMessage( Graphics g ) {
g.setColor ( Color.red );
Font letters = new Font("Serif", Font.BOLD, 20);
g.setFont(letters);
if ( x == 5 && y == 5 ) {
g.drawString ( "YOU", 20, 40 );
g.drawString ( "ARE", 20, 70 );
g.drawString ( "A", 20, 100 );
g.drawString ( "WINNER!", 20, 130 );
}
else {
g.drawString ( "Sorry,",20, 40 );
g.drawString ( "but",20, 70 );
g.drawString ( "this",20, 100 );
g.drawString ( "time",20, 130 );
g.drawString ( "you",20, 160 );
g.drawString ( "lost.",20, 190 );
}
g.drawString ( "Hit", 460, 40 );
g.drawString ( "Spacebar", 460, 70 );
g.drawString ( "to", 460, 100 );
g.drawString ( "play", 460, 130 );
g.drawString ( "again.", 460, 160 );
}
}
}