Results 1 to 2 of 2
Thread: Fitt's Law Applet
- 11-13-2011, 02:45 AM #1
Member
- Join Date
- Nov 2011
- Location
- Texas
- Posts
- 1
- Rep Power
- 0
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
XML 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(); // Add the MouseListener to your applet addMouseListener(this); } 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.drawString("Total clicks made = ",60,180); 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 #2
Similar Threads
-
Java Applet starts then the applet field goes light blue
By oki in forum Java AppletsReplies: 30Last Post: 08-26-2011, 10:05 PM -
Calling another applet on click of button in one applet
By niteshwar.bhardwaj in forum Java 2DReplies: 1Last Post: 02-19-2009, 01:54 PM -
Applet, To center text and To open I engage in a dialog in an Applet
By Marcus in forum Java AppletsReplies: 4Last Post: 06-08-2007, 07:15 AM
Bookmarks