Skip to content

Commit 8c4e563

Browse files
author
Michael James Williams
committed
Initial files from Lee Stemkoski
1 parent 7d37243 commit 8c4e563

29 files changed

+739
-0
lines changed

AnimatedImage.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import javafx.scene.image.Image;
2+
3+
public class AnimatedImage
4+
{
5+
// assumes animation loops,
6+
// each image displays for equal time
7+
public Image[] frames;
8+
public double duration;
9+
10+
public Image getFrame(double time)
11+
{
12+
int index = (int)((time % (frames.length * duration)) / duration);
13+
return frames[index];
14+
}
15+
}

Circle.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import javafx.geometry.Point2D;
2+
3+
public class Circle
4+
{
5+
private Point2D center;
6+
private double radius;
7+
8+
public Circle(double x, double y, double r)
9+
{
10+
setCenter(x,y);
11+
setRadius(r);
12+
}
13+
14+
public void setCenter(double x, double y)
15+
{
16+
center = new Point2D(x,y);
17+
}
18+
19+
public void setRadius(double r)
20+
{
21+
radius = r;
22+
}
23+
24+
public double getX()
25+
{
26+
return center.getX();
27+
}
28+
29+
public double getY()
30+
{
31+
return center.getY();
32+
}
33+
34+
public double getRadius()
35+
{
36+
return radius;
37+
}
38+
39+
public boolean containsPoint(double x, double y)
40+
{
41+
return (center.distance(x,y) < radius);
42+
}
43+
}

Example1.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import javafx.application.Application;
2+
import javafx.stage.Stage;
3+
4+
public class Example1 extends Application
5+
{
6+
public static void main(String[] args)
7+
{
8+
launch(args);
9+
}
10+
11+
@Override
12+
public void start(Stage theStage)
13+
{
14+
theStage.setTitle("Hello, World!");
15+
theStage.show();
16+
}
17+
}

Example2.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import javafx.application.Application;
2+
import javafx.stage.Stage;
3+
import javafx.scene.Scene;
4+
import javafx.scene.Group;
5+
import javafx.scene.canvas.Canvas;
6+
import javafx.scene.canvas.GraphicsContext;
7+
import javafx.scene.paint.Color;
8+
import javafx.scene.text.Font;
9+
import javafx.scene.text.FontWeight;
10+
import javafx.scene.text.FontPosture;
11+
import javafx.scene.image.Image;
12+
13+
public class Example2 extends Application
14+
{
15+
public static void main(String[] args)
16+
{
17+
launch(args);
18+
}
19+
20+
@Override
21+
public void start(Stage theStage)
22+
{
23+
theStage.setTitle( "Canvas Example" );
24+
25+
Group root = new Group();
26+
Scene theScene = new Scene( root );
27+
theStage.setScene( theScene );
28+
29+
Canvas canvas = new Canvas( 400, 200 );
30+
root.getChildren().add( canvas );
31+
32+
GraphicsContext gc = canvas.getGraphicsContext2D();
33+
34+
gc.setFill( Color.RED );
35+
gc.setStroke( Color.BLACK );
36+
gc.setLineWidth(2);
37+
Font theFont = Font.font( "Times New Roman", FontWeight.BOLD, 48 );
38+
gc.setFont( theFont );
39+
gc.fillText( "Hello, World!", 60, 50 );
40+
gc.strokeText( "Hello, World!", 60, 50 );
41+
42+
Image earth = new Image( "earth.png" );
43+
gc.drawImage( earth, 180, 100 );
44+
45+
theStage.show();
46+
}
47+
}

Example3.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import javafx.application.Application;
2+
import javafx.stage.Stage;
3+
import javafx.scene.Scene;
4+
import javafx.scene.Group;
5+
import javafx.scene.canvas.Canvas;
6+
import javafx.scene.canvas.GraphicsContext;
7+
import javafx.scene.image.Image;
8+
9+
import javafx.animation.AnimationTimer;
10+
11+
// Animation of Earth rotating around the sun. (Hello, world!)
12+
public class Example3 extends Application
13+
{
14+
public static void main(String[] args)
15+
{
16+
launch(args);
17+
}
18+
19+
@Override
20+
public void start(Stage theStage)
21+
{
22+
theStage.setTitle( "AnimationTimer Example" );
23+
24+
Group root = new Group();
25+
Scene theScene = new Scene( root );
26+
theStage.setScene( theScene );
27+
28+
Canvas canvas = new Canvas( 512, 512 );
29+
root.getChildren().add( canvas );
30+
31+
GraphicsContext gc = canvas.getGraphicsContext2D();
32+
33+
Image earth = new Image( "earth.png" );
34+
Image sun = new Image( "sun.png" );
35+
Image space = new Image( "space.png" );
36+
37+
final long startNanoTime = System.nanoTime();
38+
39+
new AnimationTimer()
40+
{
41+
public void handle(long currentNanoTime)
42+
{
43+
double t = (currentNanoTime - startNanoTime) / 1000000000.0;
44+
45+
double x = 232 + 128 * Math.cos(t);
46+
double y = 232 + 128 * Math.sin(t);
47+
48+
// Clear the canvas
49+
gc.clearRect(0, 0, 512,512);
50+
51+
// background image clears canvas
52+
gc.drawImage( space, 0, 0 );
53+
gc.drawImage( earth, x, y );
54+
gc.drawImage( sun, 196, 196 );
55+
}
56+
}.start();
57+
58+
theStage.show();
59+
}
60+
}

Example3AI.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import javafx.application.Application;
2+
import javafx.stage.Stage;
3+
import javafx.scene.Scene;
4+
import javafx.scene.Group;
5+
import javafx.scene.canvas.Canvas;
6+
import javafx.scene.canvas.GraphicsContext;
7+
import javafx.scene.image.Image;
8+
9+
import javafx.animation.AnimationTimer;
10+
import javafx.animation.KeyFrame;
11+
import javafx.util.Duration;
12+
import javafx.event.EventHandler;
13+
import javafx.event.ActionEvent;
14+
15+
// Animation of Earth rotating around the sun. (Hello, world!)
16+
public class Example3AI extends Application
17+
{
18+
public static void main(String[] args)
19+
{
20+
launch(args);
21+
}
22+
23+
@Override
24+
public void start(Stage theStage)
25+
{
26+
theStage.setTitle( "AnimatedImage Example" );
27+
28+
Group root = new Group();
29+
Scene theScene = new Scene( root );
30+
theStage.setScene( theScene );
31+
32+
Canvas canvas = new Canvas( 512, 512 );
33+
root.getChildren().add( canvas );
34+
35+
GraphicsContext gc = canvas.getGraphicsContext2D();
36+
37+
Image earth = new Image( "earth.png" );
38+
Image sun = new Image( "sun.png" );
39+
Image space = new Image( "space.png" );
40+
41+
AnimatedImage ufo = new AnimatedImage();
42+
Image[] imageArray = new Image[6];
43+
for (int i = 0; i < 6; i++)
44+
imageArray[i] = new Image( "ufo_" + i + ".png" );
45+
ufo.frames = imageArray;
46+
ufo.duration = 0.100;
47+
48+
final long startNanoTime = System.nanoTime();
49+
50+
new AnimationTimer()
51+
{
52+
public void handle(long currentNanoTime)
53+
{
54+
double t = (currentNanoTime - startNanoTime) / 1000000000.0;
55+
56+
double x = 232 + 128 * Math.cos(t);
57+
double y = 232 + 128 * Math.sin(t);
58+
59+
gc.drawImage( space, 0, 0 );
60+
gc.drawImage( earth, x, y );
61+
gc.drawImage( sun, 196, 196 );
62+
gc.drawImage( ufo.getFrame(t), 450, 25 );
63+
}
64+
}.start();
65+
66+
theStage.show();
67+
}
68+
}

Example3T.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import javafx.application.Application;
2+
import javafx.stage.Stage;
3+
import javafx.scene.Scene;
4+
import javafx.scene.Group;
5+
import javafx.scene.canvas.Canvas;
6+
import javafx.scene.canvas.GraphicsContext;
7+
import javafx.scene.image.Image;
8+
9+
import javafx.animation.Timeline;
10+
import javafx.animation.KeyFrame;
11+
import javafx.util.Duration;
12+
import javafx.event.EventHandler;
13+
import javafx.event.ActionEvent;
14+
15+
// An alternative implementation of Example 3,
16+
// using the Timeline, KeyFrame, and Duration classes.
17+
18+
// Animation of Earth rotating around the sun. (Hello, world!)
19+
public class Example3T extends Application
20+
{
21+
public static void main(String[] args)
22+
{
23+
launch(args);
24+
}
25+
26+
@Override
27+
public void start(Stage theStage)
28+
{
29+
theStage.setTitle( "Timeline Example" );
30+
31+
Group root = new Group();
32+
Scene theScene = new Scene( root );
33+
theStage.setScene( theScene );
34+
35+
Canvas canvas = new Canvas( 512, 512 );
36+
root.getChildren().add( canvas );
37+
38+
GraphicsContext gc = canvas.getGraphicsContext2D();
39+
40+
Image earth = new Image( "earth.png" );
41+
Image sun = new Image( "sun.png" );
42+
Image space = new Image( "space.png" );
43+
44+
Timeline gameLoop = new Timeline();
45+
gameLoop.setCycleCount( Timeline.INDEFINITE );
46+
47+
final long timeStart = System.currentTimeMillis();
48+
49+
KeyFrame kf = new KeyFrame(
50+
Duration.seconds(0.017), // 60 FPS
51+
new EventHandler<ActionEvent>()
52+
{
53+
public void handle(ActionEvent ae)
54+
{
55+
double t = (System.currentTimeMillis() - timeStart) / 1000.0;
56+
57+
double x = 232 + 128 * Math.cos(t);
58+
double y = 232 + 128 * Math.sin(t);
59+
60+
// Clear the canvas
61+
gc.clearRect(0, 0, 512,512);
62+
63+
// background image clears canvas
64+
gc.drawImage( space, 0, 0 );
65+
gc.drawImage( earth, x, y );
66+
gc.drawImage( sun, 196, 196 );
67+
}
68+
});
69+
70+
gameLoop.getKeyFrames().add( kf );
71+
gameLoop.play();
72+
73+
theStage.show();
74+
}
75+
}

0 commit comments

Comments
 (0)