Java Swing - Listeners
TOPIC 01 – MOUSELISTENER

 

LESSON WORK

 

 

Create a partial game where you must click on a circle that appears at different locations.  Your teacher will tell you how much detail is required.

 

Possible additions:

  • A score shows your progress.  It could be as simple as the number of clicks.

  • The circle can slowly get smaller with every click.

  • One can check the number of clicks that occur in a minute.  Because our game is fully event driven, the game will have to check the amount of time that has elapsed since the beginning till the most recent click in order to see if a minute has elapsed (which is not a great solution but it works)

 

Your application will require two different classes.  One will contain the main function that creates a JFrame and adds your specialized JPanel to it.  The other will be your specialized JPanel (that extends JPanel).

 

Let's take a closer look at your specialized JPanel class.  It will have the following form:

 

public class CoolGamePanel extends JPanel implements MouseListener

{

      //datafields go here

      //-need to keep track of circlex, circley, circleradius, goodClicks, badClicks, startTime

 

      //constructor goes here

      //-gives initial value to each datafield

      //-sets the listener object to this

 

      //paint method goes here

      //-we simply draw circle based on the datafields

      //-we stop drawing if 60 seconds have elapsed

 

      //five mandatory MouseListener methods go here

      //-all are blank except the mouseClicked() method

      //-mouseClicked method should update the circle's x, y and radius and good/bad clicks

      //-mouseClicked method needs to be able to determine if click is on circle or not.

      //-mouseClicked method calls repaint()

}