# Fitt's Law Applet

• 11-13-2011, 02:45 AM
Garry218
Fitt's Law Applet
I am trying to build an applet similar to this.
A Web-based Test of Fitts' Law - Applet1

This is So far I have done

Code:

import java.awt.*;
import java.applet.*;
// import an extra class for the MouseListener
import java.awt.event.*;
import java.util.Random;

// Tells the applet we will be using the MouseListener methods.

public class Tester extends Applet implements MouseListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
// The X-coordinate and Y-coordinate of the last click.
int xpos;
int ypos;
int x = 0;
int y = 0;
//int k = 0;

// The coordinates of the rectangle we will draw.
// It is easier to specify this here so that we can later
// use it to see if the mouse is in that area.
int rect1xco;
int rect1yco;
int rect1width;
int rect1height;
int rect2xco,rect2yco,rect2width,rect2height;

// The variable that will tell whether or not the mouse
// is in the Applet area.
boolean mouseEntered;

// variable that will be true when the user clicked i the rectangle
// the we will draw.
boolean rect1Clicked;
boolean rect2Clicked;

public void init()
{
// Assign values to the rectangle coordinates.
Random rand = new Random();
rect1xco = rand.nextInt(20)+40;
rect1yco = 70;
rect1width = rand.nextInt(50%30)+10;
rect1height = 80;

rect2xco = rand.nextInt(20)+150;
rect2yco = 70;
rect2width = rand.nextInt(50%20)+10;
rect2height = 80;

repaint();

}

public void paint(Graphics g)
{

// Rectangle's color

g.setColor(Color.DARK_GRAY);
g.setFont(new Font("sansserif", Font.BOLD, 22));
g.drawString("Fitt's Law",60,20);

g.setColor(Color.DARK_GRAY);
g.setFont(new Font("sansserif", Font.BOLD, 14));
g.drawString(" Click Green Bar 40 times",10,40);

g.setFont(new Font("serif",Font.PLAIN, 12));

//if(k==0){
g.setColor(Color.green);
g.fillRect(rect1xco,rect1yco,rect1width,rect1heigh t);

g.setColor(Color.blue);
g.fillRect(rect2xco,rect2yco,rect2width,rect2heigh t);
// k++;
//}

// When the user clicks this will show the coordinates of the click
// at the place of the click.

// If the click was in the rectangle show this message
if (rect1Clicked){
//g.drawString("Rectangle1",20,120);
g.setColor(Color.blue);
g.fillRect(rect1xco,rect1yco,rect1width,rect1heigh t);
g.setColor(Color.green);
g.fillRect(rect2xco,rect2yco,rect2width,rect2heigh t);
Random rand = new Random();
rect1xco = rand.nextInt(40);
rect1yco = 70;
rect1width = rand.nextInt(30)+5;
rect1height = rand.nextInt(80)+5;;

}

if(rect2Clicked){
//g.drawString("Rectangle2", 160, 120);
g.setColor(Color.green);
g.fillRect(rect1xco,rect1yco,rect1width,rect1heigh t);
g.setColor(Color.blue);
g.fillRect(rect2xco,rect2yco,rect2width,rect2heigh t);

Random rand = new Random();

rect2xco = rand.nextInt(40)+110;
rect2yco = 70;
rect2width = rand.nextInt(20)+5;
rect2height = rand.nextInt(80)+5;;

}
g.setColor(Color.black);
g.setColor(Color.black);
String my = Integer.toString(x);
g.drawString(my,160,180);

if(x == 40){
System.exit(0);

}
}

// This method will be called when the mouse has been clicked.
public void mouseClicked (MouseEvent me) {

// Save the coordinates of the click like this.

xpos = me.getX();
ypos = me.getY();

// Check if the click was inside the rectangle area.

if (xpos > rect1xco && xpos < rect1xco+rect1width && ypos >rect1yco &&
ypos < rect1yco+rect1height && (y%2)== 0){
x++;
y++;

rect1Clicked = true;

// if it was not then rect1Clicked is false;
} else {
rect1Clicked = false;
} //show the results of the click

if (xpos > rect2xco && xpos < rect2xco+rect2width && ypos >rect2yco &&
ypos < rect2yco+rect2height && (y%2)== 1) {
x++;
y++;
rect2Clicked = true;

// if it was not then rect1Clicked is false;
}else {
rect2Clicked = false;
}

repaint();

}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}

}

• 11-13-2011, 06:19 PM
Norm
Re: Fitt's Law Applet
Please explain your problem. Are you getting errors? Please copy and paste here the full text of the errors.

Please edit you post and replace the unformatted code with formatted code. Your unformatted code is hard to read with all statements starting in column 1