examples/moveongrid/MoveOnGrid

From FANG

Jump to: navigation, search

01 package examples.moveongrid;
02 
03 import java.awt.Point;
04 
05 import fang.*;
06 
07 /**This example shows how to move
08  * a circle smoothly on a 5 x 5 grid.
09  @author Jam Jenkins
10  */
11 public class MoveOnGrid
12          extends Game
13 {
14    /**this is the circle which moves
15     * around with the mouse*/
16    private Sprite piece;
17 
18    private GridTracker gridTracker;
19 
20    /**adds the grid to the background
21     * and the circle in front*/
22    public void setup()
23    {
24       Grid grid=new Grid(550.1);
25       grid.setScale(1);
26       grid.setLocation(0.50.5);
27       addSprite(grid);
28 
29       piece=new OvalSprite(0.10.1);
30       piece.setLocation(0.10.1);
31       piece.setColor(getColor("yellow"));
32       addSprite(piece);
33 
34       gridTracker=new GridTracker(55);
35       gridTracker.setMoveTime(1);
36       piece.addTransformer(gridTracker);
37 
38       setHelp("resources/MoveOnGridHelp.txt");
39    }
40 
41    /**gets the snapped to grid location
42     @param point the horizontal and vertical
43     * screen location
44     @return the snapped to grid column
45     * and row.  These are numbered 0...4.
46     */
47    public Point getCell(Location2D point)
48    {
49       int x=(int)Math.min(4, point.x*5);
50       int y=(int)Math.min(4, point.y*5);
51       return new Point(x, y);
52    }
53 
54    /**moves the circle with the mouse.  When
55     * the user clicks, a circle is deposited
56     * in the nearest cell.
57     */
58    public void advance()
59    {
60       //gets the position of a click
61       Location2D click=getClick2D();
62 
63       //a null click position means the
64       //user did not click
65       if(click!=null)
66       {
67          Point target=getCell(click);
68          Point source=getCell(piece.getLocation());
69 
70          gridTracker.move(target.y-source.y,
71                           target.x-source.x);
72       }
73    }
74 
75    /**runs SnapToGrid as an application
76     @param args not used
77     */
78    public static void main(String[] args)
79    {
80       new MoveOnGrid().runAsApplication();
81    }
82 
83 }


Download/View examples/moveongrid/MoveOnGrid.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