JTP
Class Screen

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Canvas
          extended by JTP.Screen
All Implemented Interfaces:
MouseListener, MouseMotionListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, MouseInputListener

public class Screen
extends Canvas
implements MouseInputListener

Controls all other game components and performs double-buffered rendering.

Author:
V.Termanis
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.Canvas
Canvas.AccessibleAWTCanvas
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
 
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
Screen(double scaleFactor)
          Creates a new Screen instance.
 
Method Summary
protected  boolean automaticMovesLeft()
          Used by KeyHandler class to decide whether another move should be executed.
protected  Images getImageData()
          Provides access to global image resources
protected  KeyHandler getKeyHandler()
          Allows parent class (JFrame) to forward key events to handler of this class.
 Point getMousePosition()
          Overloaded from Component class.
 Dimension getSize()
          Returns size of whole (scaled) game screen.
protected  void handleDeactivation()
          If window loses focus do not preserve mouse/keyboard status
 void keyPressedGame(int keyCode1, int keyCode2, long eventTime)
          Handler for all keyboard events whilst in-game.
 void keyPressedNormal(KeyEvent e)
          Handler for all keyboard events (except in-game).
 void keyReleased(KeyEvent e)
          Handler for key releases.
 void keyTyped(KeyEvent e)
          Handler for highscore name input.
 void mouseClicked(MouseEvent e)
          Not used (defined by MouseInputListener)
 void mouseDragged(MouseEvent e)
          Handles mouse movement while key is depressed.
 void mouseEntered(MouseEvent e)
          Not used (defined by MouseInputListener)
 void mouseExited(MouseEvent e)
          Prevents button or grid tile to be active when mouse moved off screen
 void mouseMoved(MouseEvent e)
          Handles mouse movement events.
 void mousePressed(MouseEvent e)
          Handles mouse-down events.
 void mouseReleased(MouseEvent e)
          Handles mouse click events (once button has been released)
 void paint(Graphics g)
          Draws screen updated (requirement by Canvas class)
protected  void returnToMenu()
          Used by GameTimer to return focus to menu when time runs out.
 boolean scaled()
          Returns whether the game is scaled up/down.
 void start(SplashControl splash)
          Initialises various game components.
 void update(Graphics g)
          Draws screen update, automatically choosing right component to render.
protected  void updateScreen()
          Forces screen update.
 
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, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, 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, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, 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, setMaximumSize, setMinimumSize, setName, setPreferredSize, 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
 

Constructor Detail

Screen

public Screen(double scaleFactor)
       throws NumberFormatException
Creates a new Screen instance.

Parameters:
scaleFactor - how much to scale output by (a value of 1 results in no scaling)
Throws:
NumberFormatException - if scaleFactor <= 0
Method Detail

getSize

public Dimension getSize()
Returns size of whole (scaled) game screen.

Overrides:
getSize in class Component
Returns:
size in pixels

update

public void update(Graphics g)
Draws screen update, automatically choosing right component to render.

Overrides:
update in class Canvas
Parameters:
g - graphics context for rendering

paint

public void paint(Graphics g)
Draws screen updated (requirement by Canvas class)

Overrides:
paint in class Canvas

updateScreen

protected void updateScreen()
Forces screen update.


handleDeactivation

protected void handleDeactivation()
If window loses focus do not preserve mouse/keyboard status


getImageData

protected Images getImageData()
Provides access to global image resources

Returns:
the image resources

scaled

public boolean scaled()
Returns whether the game is scaled up/down.

Returns:
true if the game is being scaled.

mouseMoved

public void mouseMoved(MouseEvent e)
Handles mouse movement events.

Specified by:
mouseMoved in interface MouseMotionListener
Parameters:
e - the incoming mouse event

mouseReleased

public void mouseReleased(MouseEvent e)
Handles mouse click events (once button has been released)

Specified by:
mouseReleased in interface MouseListener
Parameters:
e - the incoming mouse event.

mousePressed

public void mousePressed(MouseEvent e)
Handles mouse-down events.

Specified by:
mousePressed in interface MouseListener
Parameters:
e - the incoming mouse event.

mouseDragged

public void mouseDragged(MouseEvent e)
Handles mouse movement while key is depressed. Note: Forwards for processing to mouseMoved method.

Specified by:
mouseDragged in interface MouseMotionListener
Parameters:
e - the incoming mouse event.

mouseExited

public void mouseExited(MouseEvent e)
Prevents button or grid tile to be active when mouse moved off screen

Specified by:
mouseExited in interface MouseListener
Parameters:
e - the incoming mouse event.

keyReleased

public void keyReleased(KeyEvent e)
Handler for key releases. Note: This serves only to inform the class that now keys are held down.

Parameters:
e - the incoming key release event.

keyTyped

public void keyTyped(KeyEvent e)
Handler for highscore name input.

Parameters:
e - the incoming key press event.

keyPressedNormal

public void keyPressedNormal(KeyEvent e)
Handler for all keyboard events (except in-game). This method basically bypasses KeyHandler.

Parameters:
e - the incoming key press event.

keyPressedGame

public void keyPressedGame(int keyCode1,
                           int keyCode2,
                           long eventTime)
Handler for all keyboard events whilst in-game.

Parameters:
keyCode1 - player 1 key or control key (e.g. F9)
keyCode2 - player 2 key
eventTime - so that really old keyboard events can be discarded

start

public void start(SplashControl splash)
           throws IOException
Initialises various game components. Note: This method must be called before trying to show this component on screen.

Parameters:
splash - instance of SplashControl in use.
Throws:
IOException - if any of the (non-optional) game resources could not be loaded.

automaticMovesLeft

protected boolean automaticMovesLeft()
Used by KeyHandler class to decide whether another move should be executed.


returnToMenu

protected void returnToMenu()
Used by GameTimer to return focus to menu when time runs out.


getKeyHandler

protected KeyHandler getKeyHandler()
Allows parent class (JFrame) to forward key events to handler of this class.


getMousePosition

public Point getMousePosition()
                       throws HeadlessException
Overloaded from Component class. This method will not return null but instead a Point instance with coordinates [-1,-1]. (In line with what e.g. mouseExited returns in the case where the mouse has left the screen.)

Overrides:
getMousePosition in class Component
Returns:
absolute coordinates of the mouse on this Canvas or [-1,-1] if it is not over this component.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true

mouseClicked

public void mouseClicked(MouseEvent e)
Not used (defined by MouseInputListener)

Specified by:
mouseClicked in interface MouseListener

mouseEntered

public void mouseEntered(MouseEvent e)
Not used (defined by MouseInputListener)

Specified by:
mouseEntered in interface MouseListener