Skip to content

Instantly share code, notes, and snippets.

Created October 14, 2013 03:33
Show Gist options
  • Save anonymous/6970299 to your computer and use it in GitHub Desktop.
Save anonymous/6970299 to your computer and use it in GitHub Desktop.
//********************************************************************
// Direction.java Java Foundations
//
// Demonstrates key events.
//********************************************************************
import javax.swing.JFrame;
public class Direction
{
//-----------------------------------------------------------------
// Creates and displays the application frame.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Direction");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new DirectionPanel());
frame.pack();
frame.setVisible(true);
}
}
//********************************************************************
// DirectionPanel.java Java Foundations
//
// Represents the primary display panel for the Direction program.
//********************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DirectionPanel extends JPanel
{
private final int WIDTH = 300, HEIGHT = 200;
private final int JUMP = 10; // increment for image movement
private final int IMAGE_SIZE = 31;
private ImageIcon up, down, right, left, currentImage;
private int x, y;
//-----------------------------------------------------------------
// Constructor: Sets up this panel and loads the images.
//-----------------------------------------------------------------
public DirectionPanel()
{
addKeyListener (new DirectionListener());
x = WIDTH / 2;
y = HEIGHT / 2;
up = new ImageIcon ("arrowUp.gif");
down = new ImageIcon ("arrowDown.gif");
left = new ImageIcon ("arrowLeft.gif");
right = new ImageIcon ("arrowRight.gif");
currentImage = right;
setBackground (Color.black);
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setFocusable(true);
}
//-----------------------------------------------------------------
// Draws the image in the current location.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
currentImage.paintIcon (this, page, x, y);
}
//*****************************************************************
// Represents the listener for keyboard activity.
//*****************************************************************
private class DirectionListener implements KeyListener
{
//--------------------------------------------------------------
// Responds to the user pressing arrow keys by adjusting the
// image and image location accordingly.
//--------------------------------------------------------------
public void keyPressed (KeyEvent event)
{
switch (event.getKeyCode())
{
case KeyEvent.VK_UP:
currentImage = up;
y -= JUMP;
break;
case KeyEvent.VK_DOWN:
currentImage = down;
y += JUMP;
break;
case KeyEvent.VK_LEFT:
currentImage = left;
x -= JUMP;
break;
case KeyEvent.VK_RIGHT:
currentImage = right;
x += JUMP;
break;
}
repaint();
}
//--------------------------------------------------------------
// Provide empty definitions for unused event methods.
//--------------------------------------------------------------
public void keyTyped (KeyEvent event) {}
public void keyReleased (KeyEvent event) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment