// Scott DeRuiter 12/3/2000
// TopViewTest.java
// Use the left and right keys to move the direction of a pointer, then
// use the spacebar to move that point. Also, use the up and down
// arrow keys to change the magnitude of the motion.
// 3/13/2013 Greenstein: changed applet to JFrame
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TopViewTest implements KeyListener, FocusListener, MouseListener
{
GamePiece mypiece;
private JFrame frame;
private DrawingArea canvas;
public TopViewTest ( )
{
mypiece = new GamePiece ( 350, 225, 700, 450, 20, 15, Color.blue );
}
public static void main(String[] args)
{
TopViewTest tvt = new TopViewTest();
tvt.Run();
}
public void Run ( )
{
// Create a frame to hold everything
frame = new JFrame ("TopViewTest");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setBackground(Color.black); // only needed if frame is larger than panel
// Define panel to draw on
canvas = new DrawingArea(); // create a panel to draw on
canvas.setBackground(Color.lightGray);
canvas.addFocusListener(this);
canvas.addKeyListener(this);
canvas.addMouseListener(this); // connects the MouseListerner to the panel window
// Put frame together
frame.getContentPane().add(canvas); // puts panel on frame
frame.setVisible(true);
}
class DrawingArea extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
mypiece.draw ( g );
}
}
public void focusGained(FocusEvent evt)
{
canvas.repaint();
}
public void focusLost(FocusEvent evt)
{
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 )
{
int value = e.getKeyCode();
if ( value == KeyEvent.VK_RIGHT )
{
mypiece.changedegree ( 6 );
canvas.repaint ();
}
else if ( value == KeyEvent.VK_LEFT )
{
mypiece.changedegree ( -6 );
canvas.repaint ();
}
else if ( value == KeyEvent.VK_SPACE )
{
mypiece.move();
canvas.repaint();
}
else if ( value == KeyEvent.VK_UP )
{
mypiece.changemagnitude ( 2 );
canvas.repaint ();
}
else if ( value == KeyEvent.VK_DOWN )
{
mypiece.changemagnitude ( -2 );
canvas.repaint();
}
}
public void keyReleased ( KeyEvent e ) {}
}
class GamePiece
{
private int xpos, ypos, width, height, degree, motionmag;
private Color piece;
public GamePiece ( int x, int y, int w, int h, int d, int m, Color p )
{
xpos = x;
ypos = y;
width = w;
height = h;
degree = d;
motionmag = m;
piece = p;
}
public void draw ( Graphics g )
{
int [] x = new int [5];
int [] y = new int [5];
double dx = Math.PI / 4;
double piecesize = width / 20.0;
x[0] = (int) ( xpos + piecesize * Math.cos ( degree * Math.PI / 180 + 3 * dx ));
x[1] = (int) ( xpos + piecesize * Math.cos ( degree * Math.PI / 180 + dx ));
x[2] = (int) ( xpos + 2 * piecesize * Math.cos ( degree * Math.PI / 180 ));
x[3] = (int) ( xpos + piecesize * Math.cos ( degree * Math.PI / 180 - dx ));
x[4] = (int) ( xpos + piecesize * Math.cos ( degree * Math.PI / 180 - 3 * dx ));
y[0] = (int) ( ypos + piecesize * Math.sin ( degree * Math.PI / 180 + 3 * dx ));
y[1] = (int) ( ypos + piecesize * Math.sin ( degree * Math.PI / 180 + dx ));
y[2] = (int) ( ypos + 2 * piecesize * Math.sin ( degree * Math.PI / 180 ));
y[3] = (int) ( ypos + piecesize * Math.sin ( degree * Math.PI / 180 - dx ));
y[4] = (int) ( ypos + piecesize * Math.sin ( degree * Math.PI / 180 - 3 * dx ));
g.setColor ( piece );
g.fillPolygon ( x, y, 5 );
}
public void move ()
{
xpos = (int) ( xpos + motionmag * Math.cos( degree * Math.PI / 180 ));
ypos = (int) ( ypos + motionmag * Math.sin( degree * Math.PI / 180 ));
if ( xpos > width - 20 )
xpos = width - 20;
if ( xpos < 20 )
xpos = 20;
if ( ypos > height - 20 )
ypos = height - 20;
if ( ypos < 20 )
ypos = 20;
}
public void changemagnitude ( int addto )
{
motionmag += addto;
if ( motionmag > 40 )
motionmag = 40;
if ( motionmag < 6 )
motionmag = 6;
}
public void changedegree ( int addto )
{
degree += addto;
if ( degree >= 360 )
degree -= 360;
else if ( degree <= 0 )
degree += 360;
}
}
Back to Lesson 26 Examples
Back to Java Main Page