dualroids
Class SpaceArea

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--dualroids.SpaceArea
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable

final class SpaceArea
extends JComponent

Draws the actual playing field. Takes care of all actual drawing and buffering during the game.

See Also:
Serialized Form

Inner classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent, JComponent.ActionStandin, JComponent.IntVector, JComponent.KeyboardState
 
Inner classes inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Inner classes inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.AWTTreeLock
 
Field Summary
private  Arena arena
          An arena object, which is used to fetch the shapes to draw
private static String BG_GRAPHIC
          File name of the background image
private  Image bgImage
          The background image
private static Color BLEAK_GRAY
          A semi-transparent gray color for showing end texts
private  BufferedImage buffer1
          Background image buffer
private  BufferedImage buffer2
          Foreground image buffer, works best with accelerated graphics
private static Color CLEAR
          A 'clear' color, fully transparent
private  ClientModelConnector connector
          The connector to the client
private static Font GAME_OVER_FONT
          Font for displaying the 'Game Over' -text
private  boolean gameOver
          Toggles whether the game is over
private  RenderingHints hints
          Rendering hints for rendering asteroids, spaceships etc.
private  boolean otherPlayerQuit
          Toggles whether other player has quit
private static Font PLAYER_HAS_QUIT_FONT
          Font for displaying the 'Other player has quit' text
private  String player1
          Name of player 1
private  String player2
          Name of player 2
private static Font SCORE_BOARD_FONT
          Font for displaying scoreboard during the game
private static Font SCORE_FONT
          Font for displaying high scores when the game ends
private  String score1
          Score of player 1
private  String score2
          Score of player 2
private  boolean scoreboardUpdated
          Toggles whether the score board has been updated
private  HighScoreHandler scoreHandler
          A high score-handler to save/load highscores
private  String ships1
          Number of ships player 1 has left
private  String ships2
          Number of ships player 2 has left
static int WINDOW_HEIGHT
          Height of the displayable area.
static int WINDOW_WIDTH
          Width of the displayable area.
 
Fields inherited from class javax.swing.JComponent
_bounds, accessibleContext, actionMap, ACTIONMAP_CREATED, alignmentX, alignmentY, ANCESTOR_INPUTMAP_CREATED, ANCESTOR_USING_BUFFER, ancestorInputMap, ancestorNotifier, autoscroller, border, changeSupport, clientProperties, CREATED_DOUBLE_BUFFER, flags, FOCUS_INPUTMAP_CREATED, focusInputMap, HAS_FOCUS, htmlKey, htmlView, inputVerifier, IS_DOUBLE_BUFFERED, IS_OPAQUE, IS_PAINTING_TILE, IS_PRINTING, IS_PRINTING_ALL, KEY_EVENTS_ENABLED, KEYBOARD_BINDINGS_KEY, listenerList, maximumSize, minimumSize, NEXT_FOCUS, paintImmediatelyClip, paintingChild, preferredSize, readObjectCallbacks, REQUEST_FOCUS_DISABLED, tmpRect, TOOL_TIP_TEXT_KEY, ui, uiClassID, UNDEFINED_CONDITION, verifyInputWhenFocusTarget, vetoableChangeSupport, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW, WHEN_IN_FOCUSED_WINDOW_BINDINGS, WIF_INPUTMAP_CREATED, windowInputMap
 
Fields inherited from class java.awt.Container
component, containerListener, containerSerializedDataVersion, dbg, dispatcher, layoutMgr, listeningBoundsChildren, listeningChildren, maxSize, ncomponents, printing, printingThreads, serialVersionUID
 
Fields inherited from class java.awt.Component
actionListenerK, adjustmentListenerK, appContext, background, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, componentListener, componentListenerK, componentOrientation, componentSerializedDataVersion, containerListenerK, cursor, dropTarget, enabled, eventMask, focusListener, focusListenerK, font, foreground, graphicsConfig, hasFocus, height, hierarchyBoundsListener, hierarchyBoundsListenerK, hierarchyListener, hierarchyListenerK, incRate, inputMethodListener, inputMethodListenerK, isInc, isPacked, itemListenerK, keyListener, keyListenerK, LEFT_ALIGNMENT, locale, LOCK, metrics, minSize, mouseListener, mouseListenerK, mouseMotionListener, mouseMotionListenerK, name, nameExplicitlySet, newEventsOnly, ownedWindowK, parent, peer, peerFont, popups, prefSize, privateKey, RIGHT_ALIGNMENT, textListenerK, TOP_ALIGNMENT, valid, visible, width, windowClosingException, windowListenerK, x, y
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
SpaceArea(ClientModelConnector cmc)
          Constructor for this class: initializes all necessary parameters.
 
Method Summary
 void drawEndCredits(Graphics2D g2_b2)
          Draws end credits when the game ends.
 void drawScoreBoard()
          Draws the scoreboard into buffer2, so that scores can float above the asteroids (buffer2 has an alpha channel).
 void gameOver()
          Called externally when the game is over.
 void otherPlayerQuit()
          Called externally when the other player has quit.
 void paint(Graphics g)
          Draws the game screen and updates it.
 void paintScoreBoard()
          Paints scoreboard into buffer1 with the background image.
 void registerArena(Arena arena)
          Registers an Arena for painting.
 void updateScoreBoard(String player1, String player2, String score1, String score2, String ships1, String ships2)
          Updates player names, number of ships and current score.
 
Methods inherited from class javax.swing.JComponent
_paintImmediately, , addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, adjustPaintFlags, alwaysOnTop, checkIfChildObscuredBySibling, clearFocusOwners, componentInputMapChanged, computeVisibleRect, computeVisibleRect, compWriteObjectNotify, contains, createToolTip, disable, enable, enableSerialization, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getActionMap, getAlignmentX, getAlignmentY, getAutoscrolls, getBorder, getBounds, getClientProperties, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getCreatedDoubleBuffer, getDebugGraphicsOptions, getFlag, getGraphics, getHeight, getInputMap, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getUIClassID, getVerifyInputWhenFocusTarget, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, hide, isDoubleBuffered, isFocusCycleRoot, isFocusTraversable, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paintWithBuffer, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processFocusEvent, processKeyBinding, processKeyBindings, processKeyBindingsForAllComponents, processKeyEvent, processMouseMotionEvent, putClientProperty, readObject, rectangleIsObscured, rectangleIsObscuredBySibling, registerKeyboardAction, registerKeyboardAction, registerWithKeyboardManager, registerWithKeyboardManager, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setCreatedDoubleBuffer, setDebugGraphicsOptions, setDoubleBuffered, setEnabled, setFlag, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPaintingChild, setPreferredSize, setRequestFocusEnabled, setToolTipText, setUI, setVerifyInputWhenFocusTarget, setVisible, shouldDebugGraphics, superProcessMouseMotionEvent, unregisterKeyboardAction, unregisterWithKeyboardManager, unregisterWithKeyboardManager, update, updateUI, writeObject
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, adjustListeningChildren, applyOrientation, checkGD, countComponents, createChildHierarchyEvents, createHierarchyEvents, deliverEvent, dispatchEventImpl, dispatchEventToSelf, doLayout, eventEnabled, findComponentAt, findComponentAt, findComponentAt, getAccessibleAt, getAccessibleChild, getAccessibleChildrenCount, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents_NoClientCode, getComponents, getLayout, getMouseEventTarget, getWindow, initIDs, insets, invalidate, invalidateTree, isAncestorOf, layout, lightweightPaint, lightweightPrint, list, list, locate, minimumSize, nextFocus, numListening, paintComponents, paintHeavyweightComponents, postProcessKeyEvent, postsOldMouseEvents, preferredSize, preProcessKeyEvent, printComponents, printHeavyweightComponents, processContainerEvent, processEvent, proxyEnableEvents, proxyRequestFocus, remove, remove, removeAll, removeContainerListener, setFocusOwner, setLayout, transferFocus, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, areInputMethodsEnabled, bounds, checkImage, checkImage, checkWindowClosingException, coalesceEvents, constructComponentName, contains, createImage, createImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getAccessibleIndexInParent, getAccessibleStateSet, getBackground, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont_NoClientCode, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocationOnScreen_NoTreeLock, getLocationOnScreen, getName, getNativeContainer, getParent_NoClientCode, getParent, getPeer, getSize, getToolkit, getToolkitImpl, getTreeLock, getWindowForObject, gotFocus, handleEvent, imageUpdate, inside, isDisplayable, isEnabled, isEnabledImpl, isLightweight, isRecursivelyVisible, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, repaint, repaint, repaint, resetGC, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

WINDOW_WIDTH

public static final int WINDOW_WIDTH
Width of the displayable area. Must match Arena.HEIGHT

WINDOW_HEIGHT

public static final int WINDOW_HEIGHT
Height of the displayable area. Must match Arena.WIDTH

BG_GRAPHIC

private static final String BG_GRAPHIC
File name of the background image

SCORE_BOARD_FONT

private static final Font SCORE_BOARD_FONT
Font for displaying scoreboard during the game

SCORE_FONT

private static final Font SCORE_FONT
Font for displaying high scores when the game ends

PLAYER_HAS_QUIT_FONT

private static final Font PLAYER_HAS_QUIT_FONT
Font for displaying the 'Other player has quit' text

GAME_OVER_FONT

private static final Font GAME_OVER_FONT
Font for displaying the 'Game Over' -text

CLEAR

private static final Color CLEAR
A 'clear' color, fully transparent

BLEAK_GRAY

private static final Color BLEAK_GRAY
A semi-transparent gray color for showing end texts

connector

private ClientModelConnector connector
The connector to the client

player1

private String player1
Name of player 1

player2

private String player2
Name of player 2

score1

private String score1
Score of player 1

score2

private String score2
Score of player 2

ships1

private String ships1
Number of ships player 1 has left

ships2

private String ships2
Number of ships player 2 has left

bgImage

private Image bgImage
The background image

scoreboardUpdated

private boolean scoreboardUpdated
Toggles whether the score board has been updated

otherPlayerQuit

private boolean otherPlayerQuit
Toggles whether other player has quit

gameOver

private boolean gameOver
Toggles whether the game is over

hints

private final RenderingHints hints
Rendering hints for rendering asteroids, spaceships etc.

buffer1

private BufferedImage buffer1
Background image buffer

buffer2

private BufferedImage buffer2
Foreground image buffer, works best with accelerated graphics

arena

private Arena arena
An arena object, which is used to fetch the shapes to draw

scoreHandler

private HighScoreHandler scoreHandler
A high score-handler to save/load highscores
Constructor Detail

SpaceArea

public SpaceArea(ClientModelConnector cmc)
Constructor for this class: initializes all necessary parameters.
Parameters:
cmc - The ClientModelConnector for the client whose screen this is displayed on.
Method Detail

registerArena

public void registerArena(Arena arena)
Registers an Arena for painting.
Parameters:
arena - the Arena to be painted

otherPlayerQuit

public final void otherPlayerQuit()
Called externally when the other player has quit. Sets the toggles so the appropriate texts are drawn.

gameOver

public final void gameOver()
Called externally when the game is over. Sets the toggles so the appropriate texts are drawn.

updateScoreBoard

public final void updateScoreBoard(String player1,
                                   String player2,
                                   String score1,
                                   String score2,
                                   String ships1,
                                   String ships2)
Updates player names, number of ships and current score. Sets the toggles so the scoreboard is drawn.
Parameters:
player1 - The name of player 1
player2 - The name of player 2
score1 - The score of player 1
score2 - The score of player 2
ships1 - Number of ships player 1 has remaining
ships2 - Number of ships player 2 has remaining

drawEndCredits

public void drawEndCredits(Graphics2D g2_b2)
Draws end credits when the game ends. consumes memory far faster than can be freed, but as the game has ended anyway, it doesn't matter much.
Parameters:
g2_b2 - The graphics context which to paint into

paintScoreBoard

public void paintScoreBoard()
Paints scoreboard into buffer1 with the background image. The scores will thus be under the asteroids, but on slower machines it's noticeably faster than using drawScoreBoard().

drawScoreBoard

public void drawScoreBoard()
Draws the scoreboard into buffer2, so that scores can float above the asteroids (buffer2 has an alpha channel). On some machines, it slows redrawing down considerably.

paint

public void paint(Graphics g)
Draws the game screen and updates it.
Overrides:
paint in class JComponent
Parameters:
g - The graphics context which to paint into


Dualroids was written by Oskar Ojala, Yrjö Kari-Koskinen and Henri Sivonen