EventTest04.java
package com.bozoid.cis370.hw04;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*; // for AffineTransform, Shape2D
import java.awt.font.*; // for Font
import java.text.*; // for NumberFormat
import java.util.Date; // for getTime()
/**
CISC370-011 homework #4: Simple Swing event handling.
@author Walt Leipold
*/
public class EventTest04
{
public static void main(String argv[])
{
Frame04 f = new Frame04();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.show();
}
}
class Frame04 extends JFrame
{
public Frame04()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension ss = kit.getScreenSize();
// Window is 3/4 of screen size, centered.
setSize(3*ss.width/4,3*ss.height/4);
setLocation(ss.width/8,ss.height/8);
setTitle("CISC370-011 homework #4");
Panel04 sp = new Panel04(this);
Container contentPane = getContentPane();
contentPane.add(sp);
}
}
class Panel04 extends JPanel
{
private Ship04 ship = null;
/**
Constructor for the lander panel. Note that the parent frame
is passed explicitly, so the panel can register to listen for
window events.
*/
public Panel04(JFrame parent)
{
// Hook up listeners with listenees.
addMouseListener(new ShipMouse());
addKeyListener(new ShipKey());
parent.addWindowListener(new ShipWindow());
// Make sure we can 'hear' keyboard events.
setFocusable(true);
// Start the simulation timer.
simTimer = new Timer(25,new ShipTimer());
simTimer.start();
// Initialize everything else.
setBackground(space_color);
fuel_font = new Font("Monospaced",Font.PLAIN,12);
ship = new Ship04(200.0,-200.0,Math.PI/2.0);
}
/**
Every tick of the timer, simulate the physics of the ship,
including fuel flow summation, translation and rotation, and
accelerations both linear and rotational.
*/
private class ShipTimer implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ship.oneTick(cmd_main,cmd_cw,cmd_ccw);
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
ship.drawShip(g,cmd_main,cmd_cw,cmd_ccw);
drawFuel(g);
}
/**
Draw the spaceship's fuel consumption in the bottom left
corner of this panel.
@param g a graphics context to draw in
*/
private void drawFuel(Graphics g)
{
String s = nf.format(ship.getFuel());
String s2 = blanks.substring(0,16-s.length()) + s;
g.setColor(text_color);
g.setFont(fuel_font);
g.drawString(s2,5,getHeight()-5);
}
/**
To control the main thruster and reaction control thrusters,
watch for presses and releases of three control keys, and
store their state in three instance variables.
*/
private class ShipKey extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_J) cmd_cw = true;
else if (e.getKeyCode() == KeyEvent.VK_L) cmd_ccw = true;
else if (e.getKeyCode() == KeyEvent.VK_K) cmd_main = true;
}
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_J) cmd_cw = false;
else if (e.getKeyCode() == KeyEvent.VK_L) cmd_ccw = false;
else if (e.getKeyCode() == KeyEvent.VK_K) cmd_main = false;
}
}
/**
To move the ship when the mouse is clicked, listen for mouse
events.
*/
private class ShipMouse extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
ship.setX((double)e.getX());
ship.setY((double)e.getY() * -1.0);
}
}
/**
To pause the simulation when the window is not active, listen
for window events.
*/
private class ShipWindow extends WindowAdapter
{
public void windowActivated(WindowEvent e)
{ ship.setActive(true); }
public void windowDeactivated(WindowEvent e)
{ ship.setActive(false); }
public void windowDeiconified(WindowEvent e)
{ ship.setActive(true); }
public void windowIconified(WindowEvent e)
{ ship.setActive(false); }
}
// Which thrusters are thrusting?
private boolean cmd_cw = false;
private boolean cmd_ccw = false;
private boolean cmd_main = false;
// Timer to trigger each frame of the animation.
private Timer simTimer = null;
// Colors of various objects.
private Color space_color = Color.BLACK;
private Color text_color = Color.CYAN;
// Formatter for fuel and (future) coordinate display.
private static NumberFormat nf = null;
static {
nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
}
// Some variables for fuel display.
private Font fuel_font;
private static final String blanks = " ";
}