|
01 package examples.navigator;
02
03 import java.awt.geom.Point2D;
04
05 import fang.*;
06
07
08 /**Demonstrates scrolling by moving the
09 * mouse in the direction desired from
10 * the center of the screen.
11 * @author Jam Jenkins*/
12 public class Navigator extends Game
13 {
14 /** tiles image reflected vertically and
15 * horizontally to make it seamless.*/
16 private ImageSprite background;
17
18 /**the tracker used for the scrolling*/
19 private ScrollTracker tracker;
20
21 /** makes and adds the sprites */
22 public void setup()
23 {
24 makeSprites();
25 addSprites();
26 setHelp("resources/NavigatorHelp.txt");
27 }
28
29 /** makes the image sprites for the background
30 * and sets a vertical scrolling tracker on them*/
31 private void makeSprites()
32 {
33 background=new ImageSprite("resources/road.jpg");
34 background.setScale(1);
35 tracker=new ScrollTracker(
36 true,
37 new Point2D.Double(),
38 background,
39 3);
40 background.setTracker(tracker);
41 }
42
43 /**adds the sprites to the canvas*/
44 private void addSprites()
45 {
46 canvas.addSprite(background);
47 }
48
49 /**uses the position of the mouse in relation
50 * to the center of the screen to move the
51 * background in the direction and speed
52 * indicated. The angle from the center to the
53 * mouse if the direction the background moves
54 * and as the distance from the center of the
55 * screen increases, so too does the scroll speed*/
56 public void advance(double timeAdvanced)
57 {
58 Location2D mouse=getMouse2D();
59 Location2D center=new Location2D(0.5, 0.5);
60 double distance=mouse.distance(center);
61 double maxDistance=center.distance(1, 1);
62 /**only move if more than 10% the distance from
63 * the center to the corner*/
64 if(distance/maxDistance>0.1)
65 {
66 Location2D v=new Location2D(
67 center.x-mouse.x,
68 center.y-mouse.y);
69 tracker.setVelocity(v);
70 }
71 else
72 tracker.setVelocity(new Location2D());
73 }
74
75 public static void main(String[] argv)
76 {
77 Navigator navigator=new Navigator();
78 navigator.runAsApplication();
79 }
80 }
|