Animation and Key Listener Project
Begin by creating a project called Animation and within that project create a class called Keys. What follows here is an outline of the final Keys code. Write, but do not yet compile, the code, as this will require the addition of the Figure class.
/**
* Keys.java is an intro to getting keyboard input in an applet
* @author (your name)
* @version (a version number or a date)
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Keys extends Applet implements KeyListener
{
// define instance variables
Figure stick; // stick figure
public void init()
{
// Initialize drawing colors
setBackground(Color.white);
setForeground(Color.black);
// create Figure object by sending the window size
stick= new Figure(this.getWidth(), this.getHeight());
addKeyListener(this); // register the KeyListener
// with the applet
} // end of init() method
public void paint(Graphics g)
{
stick.draw(g); // draw the figure, that’s all
} // end of paint() method
public void keyPressed(KeyEvent event)
{
int keyCode = event.getKeyCode(); // get the key hit
if (keyCode == KeyEvent.VK_D)
{
stick.moveRight();
repaint(); // signal Java to redraw the window
}
event.consume(); // keep anything else from using
// the keyrepaint();
}
public void keyReleased(KeyEvent event)
{
event.consume(); // ignore keyReleased
}
public void keyTyped(KeyEvent event)
{
event.consume(); // ignore keyTyped
}
} // end of Keys class
|
To make this project work the applet will need to implement the KeyListener interface. You can do this in the class declaration where it says implements KeyListener
Next, to make the KeyListener work in conjunction with this application, use code containing the line add(KeyListener(this);
What this means is that when running, the program will continue to “listen,” or wait, for key presses. When you implement the KeyListener interface, ensure that the code defines three methods:
public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent e);
Implement the KeyPressed routine to capture and evaluate the key that has been pressed. If that key was a d (capital or lowercase), the stick figure will move to the right. The other methods, keyReleased() and keyTyped(), will simply call KeyEvent’s consume() method and discard any actions. Within the keyPressed() method, the application will retrieve the code corresponding to the key pressed. int keyCode = event.getKeyCode(); // get the key hit
Each key has a corresponding keyCode. Here is a table indicating keys and their respective codes.
To make this application work, you will need to complete the Figure class. This is the class from which stickFigure is instantiated. The skeleton of that class is given as follows: import java.awt.*; import java.awt.geom.*; /** * A figure that can be manipulated and that draws itself on a canvas. * * @author * @version 1.0 */ public class Figure { private int xPosition; private int yPosition; private int width; // of window private int height; // of window /** * Create a new circle at default position with default * color. */ public Figure(int windowWidth, int windowHeight) { } /** * Move the figure a few pixels to the right. */ public void moveRight() { } /* * Draw the circle with current specifications on screen. */ public void draw(Graphics g) { } }
> YOUR JOB: Complete the Figure class, implementing the constructor and the moveRight()method Compile both classes, debug, run, and test the project > EXTRAS YOU CAN ADD: Add methods to moveLeft(), moveUp(), and moveDown() |