/*** * PROVIDE THE MISSING CODE in the keyPressed() method * in order to move th
ID: 3627722 • Letter: #
Question
/***
* PROVIDE THE MISSING CODE in the keyPressed() method
* in order to move the 'snake' around the interface
* by pressing:
*
* --> cursor keys
* --> CTRL-Z to 'undo' last move
* --> CTRL-Y to 'redo' past/last move
*/
package finals;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
/**
* @(#)Snakes.java
*
*
* @author
* @version 1.00 2011/7/28
*/
public class Snakes extends JPanel implements KeyListener {
final static long serialVersionUID = 1;
final private int WIDTH = 600;
final private int HEIGHT = 400;
LinkedList snake = new LinkedList();
int x = WIDTH/2;
int y = HEIGHT/2;
/***
* KeyListener method - keyPressed()
*
* Complete the missing code in this method
*/
public void keyPressed(KeyEvent ke) {
int code = ke.getKeyCode();
switch( code ) {
default: // exit the method, i.e. do NOT repaint
return;
case KeyEvent.VK_Z:
if ( ke.isControlDown() ) {
// undo last move
}
repaint();
return; // exit the method, i.e. do NOT add new point(X,Y)
case KeyEvent.VK_Y:
if ( ke.isControlDown() ) {
// redo past/last move ...
}
break;
case KeyEvent.VK_UP:
// update x and/or y ...
break;
case KeyEvent.VK_DOWN:
// update x and/or y ...
break;
case KeyEvent.VK_LEFT:
// update x and/or y ...
break;
case KeyEvent.VK_RIGHT:
// update x and/or y ...
break;
}
// .. then add the new x,y coordinate to the list
snake.add( new Point(x,y) );
repaint();
}
/***
* just a constructor
*/
public Snakes() {
snake.add( new Point(x,y) );
this.setPreferredSize( new Dimension(600, 400) );
this.setBackground( Color.WHITE );
}
/***
* 'other' KeyListener methods
*/
public void keyTyped(KeyEvent ke) { /* NOT USED - DON'T REMOVE */ }
public void keyReleased(KeyEvent ke) { /* NOT USED - DON'T REMOVE */ }
/***
* paintComponent - overloaded JPanel method
*/
Path2D.Double path = new Path2D.Double();
public void paintComponent(Graphics g) {
super.paintComponent( g );
Graphics2D canvas = (Graphics2D)g;
Point p;
canvas.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
canvas.setStroke( new BasicStroke(5.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND) );
path.reset();
// (re)copy the points in 'snake' to create a 'path'
p = snake.get(0);
path.moveTo( p.x, p.y );
for(int i=1; ip = snake.get(i);
path.lineTo( p.x, p.y );
}
// draw the path
canvas.setColor( Color.BLUE );
canvas.draw( path );
}
/***
* main() - program execution starts here!
*/
public static void main(String[] args) {
Snakes myProgram = new Snakes();
JFrame frame = new JFrame("Snakes!");
frame.addKeyListener( myProgram );
frame.setContentPane( myProgram );
frame.pack();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLocationRelativeTo( null );
frame.setResizable( false );
frame.setVisible( true );
}
}
Explanation / Answer
See if this works.... I am not sure what you have for the main class, but given the information you had, you will have to make sure there are no syntax errors public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if ((key == KeyEvent.VK_LEFT) && (!right)) { left = true; up = false; down = false; } if ((key == KeyEvent.VK_RIGHT) && (!left)) { right = true; up = false; down = false; } if ((key == KeyEvent.VK_UP) && (!down)) { up = true; right = false; left = false; } if ((key == KeyEvent.VK_DOWN) && (!up)) { down = true; right = false; left = false; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.