Package a6
Class GameComponent
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JPanel
a6.GameComponent
- All Implemented Interfaces:
MouseListener
,ImageObserver
,MenuContainer
,Serializable
,EventListener
,Accessible
GUI component for Click-a-Dot game board. Maintains and visualizes game state and responds to
mouse inputs. Allows programmatic control of game settings and property access to game state.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
Represents a dot to be clicked.Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
Whether a game is currently being played.private int
Number of targets to show during one game.private int
Number of targets successfully hit by player during the current game.private GameComponent.Target
Encapsulates state and controls for current target.private int
Number of targets that have been shown during current game (including the current target).private int
Duration each target is shown [ms].private Timer
Timer to trigger changing targets.Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields inherited from class java.awt.Component
accessibleContext, 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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
getScore()
Query the current score.int
Query the radius of the target dot [px].int
Query the duration each target is shown [ms].void
void
void
void
Handle a mouse button being pressed.void
void
Visualize the current game state by painting on `g`.private void
setScore
(int newScore) Change the current score to `newScore` and notify observers.void
setTargetRadius
(int r) Change the radius of the target dot to `r` px.void
setTargetTimeMillis
(int t) Change the duration each target is shown to `t` ms.void
Start a new game using current settings.void
stopGame()
Stop current game.private void
timeout()
Update the game when the timer "goes off".Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Field Details
-
maxTargets
private int maxTargetsNumber of targets to show during one game. -
targetTimeMillis
private int targetTimeMillisDuration each target is shown [ms]. -
target
Encapsulates state and controls for current target. -
timer
Timer to trigger changing targets. -
isActive
private boolean isActiveWhether a game is currently being played. -
targetCount
private int targetCountNumber of targets that have been shown during current game (including the current target). If a game is not currently being played, is the number of targets shown during the previous game (0 if no games have been played). -
score
private int scoreNumber of targets successfully hit by player during the current game. If a game is not currently being played, is the number of targets hit during the previous game (0 if no games have been played).
-
-
Constructor Details
-
GameComponent
public GameComponent()Construct a new GameComponent with default settings.
-
-
Method Details
-
startGame
public void startGame()Start a new game using current settings. Progress from any previous or ongoing game is reset. Request a repaint. -
stopGame
public void stopGame()Stop current game. Takes effect immediately. Requests a repaint. All background tasks are cancelled when game is stopped. -
timeout
private void timeout()Update the game when the timer "goes off". If the game is inactive, does nothing. If the game is active, and if the maximum number of targets has already been shown, stops the game. Otherwise, respawns the target within the current component area, increments the target count, and requests a repaint. -
getScore
public int getScore()Query the current score. -
setScore
private void setScore(int newScore) Change the current score to `newScore` and notify observers. -
getTargetRadius
public int getTargetRadius()Query the radius of the target dot [px]. -
setTargetRadius
public void setTargetRadius(int r) Change the radius of the target dot to `r` px. Takes effect immediately (triggers a repaint request).Precondition:
r > 0
-
getTargetTimeMillis
public int getTargetTimeMillis()Query the duration each target is shown [ms]. -
setTargetTimeMillis
public void setTargetTimeMillis(int t) Change the duration each target is shown to `t` ms. Does not affect the currently-shown target (if any).Precondition:
t >= 0
-
paintComponent
Visualize the current game state by painting on `g`. If the game is inactive, fills the component area with black. If the game is active, draws the target on top of the default JPanel background.- Overrides:
paintComponent
in classJComponent
-
mousePressed
Handle a mouse button being pressed. If the game is inactive, does nothing. If the game is active, and if the press was a new target hit, increments score by 1, notifies observers, and requests a repaint.- Specified by:
mousePressed
in interfaceMouseListener
-
mouseReleased
- Specified by:
mouseReleased
in interfaceMouseListener
-
mouseClicked
- Specified by:
mouseClicked
in interfaceMouseListener
-
mouseEntered
- Specified by:
mouseEntered
in interfaceMouseListener
-
mouseExited
- Specified by:
mouseExited
in interfaceMouseListener
-