Class Sector

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Canvas
          extended bySector
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, java.lang.Runnable, java.io.Serializable

public class Sector
extends java.awt.Canvas
implements java.lang.Runnable

Author:
bachmaer Double buffered drawing canvas for displaying and storing game state.
See Also:
Serialized Form

Nested Class Summary
(package private)  class Sector.keyboardHandler
           
(package private)  class Sector.sizeChangeListener
           
 
Nested classes inherited from class java.awt.Canvas
java.awt.Canvas.AccessibleAWTCanvas
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
(package private)  java.lang.Thread animatorThread
           
(package private)  int delay
           
(package private)  java.util.ArrayList inSector
           
(package private)  java.util.ArrayList obstacles
           
(package private)  java.awt.Dimension offDimension
           
(package private)  java.awt.Graphics offGraphics
           
(package private)  java.awt.Image offImage
           
private  int ownPort
           
(package private)  OwnSpaceCraft ownShip
           
(package private) static java.util.Random rand
           
(package private)  int sizeX
           
(package private)  int sizeY
           
(package private)  java.awt.Point[] starPositions
           
(package private)  Obstacle x
           
 
Fields inherited from class java.awt.Canvas
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
Sector(int ownPort)
          Default constructor.
Sector(int ownPort, int fps)
          Initializes the drawing canvas.
 
Method Summary
 void addObstacle(Obstacle obs)
          Add a single obstacle to those in the sector.
 SpaceCraft collisionCheck()
          Checks for a collision between the users spacecraft and all aliens in the sector.
 OwnSpaceCraft createOwnSpaceCraft()
          Creates a space craft for the user in a random position with a random heading.
 void createStars()
          Creates stars in random positions for the display
 void drawAllSpaceCraft(java.awt.Graphics g)
          Draws all alien space craft in the sector.
 void drawObstacles(java.awt.Graphics g)
          Draws all obstacles in the sector.
 void drawStars(java.awt.Graphics g)
          Renders all the stars in the sector.
static void main(java.lang.String[] args)
          main for testing the Sector class.
 boolean obstacleClear()
          Check to see if an obstacle is in front of ownShip.
 void paint(java.awt.Graphics g)
          Override of the paint method for double buffering.
 void removeSpaceCraft(SpaceCraft craft)
          Remove a SpaceCraft from the sector.
 void run()
          Animation thread.
 void update(java.awt.Graphics g)
          Renders all object in the sector to the back buffer and then swaps buffers.
 void updateAndAddSpaceCraft(SpaceCraft craft)
          If the input parameter SpaceCraft is already in the sector its heading and/or postion are updated.
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

inSector

java.util.ArrayList inSector

obstacles

java.util.ArrayList obstacles

ownShip

OwnSpaceCraft ownShip

offImage

java.awt.Image offImage

offGraphics

java.awt.Graphics offGraphics

offDimension

java.awt.Dimension offDimension

delay

int delay

animatorThread

java.lang.Thread animatorThread

sizeX

int sizeX

sizeY

int sizeY

rand

static java.util.Random rand

starPositions

java.awt.Point[] starPositions

x

Obstacle x

ownPort

private int ownPort
Constructor Detail

Sector

public Sector(int ownPort)
Default constructor. Sets rendering speed for 20 fps.


Sector

public Sector(int ownPort,
              int fps)
Initializes the drawing canvas. Sets the size, frame rate, and background color. Creates event handlers and start the animation thread.

Parameters:
fps - number of frames to be rendered per second
Method Detail

collisionCheck

public SpaceCraft collisionCheck()
Checks for a collision between the users spacecraft and all aliens in the sector. Returns the subject of the collision if one occured. If the collision was headon. Ownship is set to null.

Returns:
ship collided with if a collision occured. null otherwise.

updateAndAddSpaceCraft

public void updateAndAddSpaceCraft(SpaceCraft craft)
If the input parameter SpaceCraft is already in the sector its heading and/or postion are updated. If the SpaceCrate is not found in the sector, it is added.

Parameters:
craft - to be undated or added

obstacleClear

public boolean obstacleClear()
Check to see if an obstacle is in front of ownShip.


addObstacle

public void addObstacle(Obstacle obs)
Add a single obstacle to those in the sector. Assumes that is will only be called once per obstacle. Does not check for duplicates.

Parameters:
obs - obstacle to be added to the scene

removeSpaceCraft

public void removeSpaceCraft(SpaceCraft craft)
Remove a SpaceCraft from the sector. The SpaceCraft to be removed could be either alien or ownShip.

Parameters:
craft - SpaceCraft to be removed.

createStars

public void createStars()
Creates stars in random positions for the display


drawStars

public void drawStars(java.awt.Graphics g)
Renders all the stars in the sector.

Parameters:
g - Graphics context for rendering

run

public void run()
Animation thread. Sleeps inbetween frames according to the frame rate.

Specified by:
run in interface java.lang.Runnable

paint

public void paint(java.awt.Graphics g)
Override of the paint method for double buffering.


update

public void update(java.awt.Graphics g)
Renders all object in the sector to the back buffer and then swaps buffers.


drawAllSpaceCraft

public void drawAllSpaceCraft(java.awt.Graphics g)
Draws all alien space craft in the sector. Draws own ship if it is present.

Parameters:
g - Graphics context for rendering

drawObstacles

public void drawObstacles(java.awt.Graphics g)
Draws all obstacles in the sector.

Parameters:
g - Graphics context for rendering

createOwnSpaceCraft

public OwnSpaceCraft createOwnSpaceCraft()
Creates a space craft for the user in a random position with a random heading.

Returns:
reference to the users space craft for updating

main

public static void main(java.lang.String[] args)
main for testing the Sector class.

Parameters:
args -