Package a6

Class GameComponent.Target

java.lang.Object
a6.GameComponent.Target
Enclosing class:
GameComponent

private static class GameComponent.Target extends Object
Represents a dot to be clicked. Maintains current position and size and whether it has already been "hit" in its current location. Able to check clicks for hits, move to a new location, and paint itself.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
    Whether this has been "hit" in its current location.
    (package private) int
    Radius of circular dot representing the target [px].
    private Random
    Generate random numbers to use when choosing new location.
    (package private) int
    x-coordinate of current center position [px].
    (package private) int
    y-coordinate of current center position [px].
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) boolean
    checkHit(int cx, int cy)
    Return true and set state to "hit" if coordinates (cx,cy) are within this target's circular area and it was not already "hit"; return false otherwise.
    (package private) int
    clip(int coord, int max)
    Clip `coord` to be within `[radius, max - radius]`, if possible.
    (package private) void
    Paint dot on provided Graphics `g`.
    (package private) void
    respawn(int xMax, int yMax)
    Move target to a random location and reset "hit" state.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • x

      int x
      x-coordinate of current center position [px].
    • y

      int y
      y-coordinate of current center position [px].
    • radius

      int radius
      Radius of circular dot representing the target [px].
    • isHit

      private boolean isHit
      Whether this has been "hit" in its current location.
    • rng

      private Random rng
      Generate random numbers to use when choosing new location.
  • Constructor Details

    • Target

      private Target()
  • Method Details

    • paintDot

      void paintDot(Graphics g)
      Paint dot on provided Graphics `g`. Dot is a circle with our current radius, centered at our current position. Circle is filled red if we have been hit, otherwise blue.
    • clip

      int clip(int coord, int max)
      Clip `coord` to be within `[radius, max - radius]`, if possible. Otherwise, return `coord` unchanged.
    • respawn

      void respawn(int xMax, int yMax)
      Move target to a random location and reset "hit" state. xMax and yMax are the (inclusive) upper bounds of the x- and y-coordinates of the bounding box of the new position [px]; lower bound is 0 (inclusive).
    • checkHit

      boolean checkHit(int cx, int cy)
      Return true and set state to "hit" if coordinates (cx,cy) are within this target's circular area and it was not already "hit"; return false otherwise.