Package a6

Class GameComponent

All Implemented Interfaces:
MouseListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible

public class GameComponent extends JPanel implements MouseListener
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:
  • Field Details

    • maxTargets

      private int maxTargets
      Number of targets to show during one game.
    • targetTimeMillis

      private int targetTimeMillis
      Duration each target is shown [ms].
    • target

      private GameComponent.Target target
      Encapsulates state and controls for current target.
    • timer

      private Timer timer
      Timer to trigger changing targets.
    • isActive

      private boolean isActive
      Whether a game is currently being played.
    • targetCount

      private int targetCount
      Number 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 score
      Number 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

      public void paintComponent(Graphics g)
      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 class JComponent
    • mousePressed

      public void mousePressed(MouseEvent e)
      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 interface MouseListener
    • mouseReleased

      public void mouseReleased(MouseEvent e)
      Specified by:
      mouseReleased in interface MouseListener
    • mouseClicked

      public void mouseClicked(MouseEvent e)
      Specified by:
      mouseClicked in interface MouseListener
    • mouseEntered

      public void mouseEntered(MouseEvent e)
      Specified by:
      mouseEntered in interface MouseListener
    • mouseExited

      public void mouseExited(MouseEvent e)
      Specified by:
      mouseExited in interface MouseListener