Code for Java MouseListener Applet with timer


// The "MouseListenerTest" class.
import java.awt.*;//Import Abstract Window Toolkit (Basic graphics)
import java.util.Random;//Import random generator
import java.applet.*;Import applet package
import java.awt.event.*;Import event listeners, including MouseListeners
import javax.swing.*; //Swing package
public class Project2 extends Applet implements MouseListener
//Opening line of a class. I extend Applet to make an Applet and implement MouseListener to tell the program to pay attention to what the mouse does. {
static int xpos;
static int ypos;
static int xtarget;
static int ytarget;
static double score=0;
static double clicks=0;
static long start = System.currentTimeMillis();
//Variable that stores the time at the initialization static Graphics g;
//Importing Graphics class, part of the awt package static Random generator = new Random ();
Initialize radom generator public void mousePressed (MouseEvent me)
//Code in this method is activated when the mouse is pressed. Even if it's empty, it has to be there {
}
public void mouseReleased (MouseEvent me)
//Code in this method is activated when the mouse is released. Even if it's empty, it has to be there {
}
public void mouseEntered (MouseEvent me)
//Code in this method is activated when the mouse enters the window. Even if it's empty, it has to be there {
xpos = me.getX ();
//assigns the mouse position to the xpos and ypos variables ypos = me.getY ();
repaint ();
}
public void mouseClicked (MouseEvent me)
//Code in this method is activated when the mouse is clicked. Even if it's empty, it has to be there {
clicks++;
xpos = me.getX ();
ypos = me.getY ();
if (xpos > xtarget-10 && xpos < xtarget+10 && ypos > ytarget-10 && ypos < ytarget+10)
//Detects if the click was inside the target {
xtarget = generator.nextInt (280);
//Generates a new target location ytarget = generator.nextInt (180);
score++;
}
repaint ();
}
public void mouseExited (MouseEvent me)
//Code in this method is activated when the mouse leaves the window. Even if it's empty, it has to be there {
xpos = me.getX ();
ypos = me.getY ();
msg = "The mouse has exited the window at " + xpos + "," + ypos;
repaint ();
}
public void paint (Graphics g)
{
double accuracy;
long elapsedTimeMillis;
if (score==10)
//If 10 targets have been shot {
elapsedTimeMillis = System.currentTimeMillis()-start;
//figures out elapsed time accuracy=score/clicks;
//Figures out accuracy accuracy=accuracy*100;
g.drawString ("Your accuracy is "+accuracy+" %",0,50);
g.drawString ("You shot "+score+" targets in "+elapsedTimeMillis+" Miliseconds.",0,65);
score=0;
clicks=0;
}
if (score<10)
{g.setColor(Color.red);
g.fillOval (xtarget, ytarget, 10, 10);
//Draws a target at the generated coordinates }
else{
try
{
Thread.sleep (2000,0);
//Pauses the program for 2 seconds. This must be in a try-catch }
catch(Exception e)
{
}
repaint();
}
g.drawString ("Your score is:"+score, 0, 12);
}
public void init ()//Init activated when the applet starts, kind of like a main method
{
addMouseListener (this);
//Tells Java to use MouseListener on this class xsquare = generator.nextInt (280);
ysquare = generator.nextInt (180);
}
} // MouseListenerTest class

Back to Applet
Home
Hosted by www.Geocities.ws

1