examples/mouselocation/MouseLocation

From FANG

Jump to: navigation, search

01 package examples.mouselocation;
02 
03 import java.awt.geom.*;
04 
05 import fang.*;
06 
07 /**Tracks the x and y position of
08  * the mouse on the screen.
09  @author Jam Jenkins*/
10 public class MouseLocation extends Game
11 {
12    /**the sprite containing the
13     * mouse location string*/
14    private StringSprite locator;
15 
16    /**makes and adds the sprites to the screen*/
17    public void setup()
18    {
19       makeSprites();
20       addSprites();
21       setHelp("resources/MouseLocationHelp.txt");
22    }
23    /**makes the mouse locator sprite*/
24    private void makeSprites()
25    {
26       locator=new StringSprite("(0, 0)"true);
27       locator.setHeight(0.05);
28    }
29 
30    /**adds the mouse locator sprite to the canvas*/
31    private void addSprites()
32    {
33       addSprite(locator);
34    }
35 
36    /**positions the mouse locator at the mouse
37     * position and sets the text to the x and y
38     * location of the mouse*/
39    public void advance()
40    {
41       Location2D location=getMouse2D();
42       String text="("+truncateDecimal(location.x, 2)+
43                   ", "+truncateDecimal(location.y, 2)+")";
44       locator.setText(text);
45       locator.setLocation(location.x, location.y);
46       repositionOnScreen();
47    }
48 
49    private void repositionOnScreen()
50    {
51       if(locator.getX()-locator.getWidth()/2<0)
52          locator.setX(locator.getWidth()/2);
53       if(locator.getY()-locator.getHeight()/2<0)
54          locator.setY(locator.getHeight()/2);
55       if(locator.getX()+locator.getWidth()/2>1)
56          locator.setX(1-locator.getWidth()/2);
57       if(locator.getY()+locator.getHeight()/2>1)
58          locator.setY(1-locator.getHeight()/2);
59 
60    }
61 
62    private String truncateDecimal(double x, int places)
63    {
64       String toReturn=""+x;
65       return toReturn.substring(0,
66                                 Math.min(toReturn.length(), toReturn.indexOf('.')+places+1));
67    }
68 
69    public static void main(String[] argv)
70    {
71       new MouseLocation().runAsApplication();
72    }
73 }


Download/View examples/mouselocation/MouseLocation.java




Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games