makeLabel = (String name)-> {
29 | Text label = new Text(name);
30 | label.setFill(Color.LIGHTBLUE);
31 | label.setFont(Font.font("Verdana", FontWeight.BOLD, 40));
32 | return label;
33 | };
34 |
35 | Text label = makeLabel.apply("JavaFX Bloom");
36 | label.setEffect(new Bloom(0.8));
37 |
38 | Text label2 = makeLabel.apply("JavaFX Glow");
39 | label2.setEffect(new Glow(0.8));
40 |
41 | Text label3 = makeLabel.apply("JavaFX Text");
42 |
43 | VBox root = new VBox(5, label, label2, label3);
44 | root.setPadding(new Insets(20));
45 | root.setBackground(Background.EMPTY);
46 |
47 | Scene scene = new Scene(root, 400, 250, Color.DARKGRAY);
48 |
49 | primaryStage.setTitle("Bloom or Glow, I don't know...");
50 | primaryStage.setScene(scene);
51 | primaryStage.show();
52 | }
53 |
54 | /**
55 | * @param args the command line arguments
56 | */
57 | public static void main(String[] args) {
58 | launch(args);
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/Chapter08/src/chapter8/colors/ColorAdjustDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter8.colors;
5 |
6 | import javafx.application.Application;
7 | import javafx.beans.property.DoubleProperty;
8 | import javafx.geometry.Insets;
9 | import javafx.geometry.Pos;
10 | import javafx.scene.Scene;
11 | import javafx.scene.control.Label;
12 | import javafx.scene.control.Slider;
13 | import javafx.scene.effect.ColorAdjust;
14 | import javafx.scene.image.ImageView;
15 | import javafx.scene.layout.HBox;
16 | import javafx.scene.layout.VBox;
17 | import javafx.stage.Stage;
18 |
19 | /**
20 | *
21 | * @author sgrinev
22 | */
23 | public class ColorAdjustDemo extends Application {
24 |
25 | @Override
26 | public void start(Stage primaryStage) {
27 | ImageView iv = new ImageView("https://github.com/sgrinev/mastering-javafx-9-book/blob/master/resources/sample.jpg?raw=true");
28 | iv.setFitHeight(240);
29 | iv.setFitWidth(240);
30 | ColorAdjust ca = new ColorAdjust();
31 | iv.setEffect(ca);
32 |
33 | VBox root = new VBox(10);
34 | for (DoubleProperty prop : new DoubleProperty[] {
35 | ca.hueProperty(), ca.contrastProperty(), ca.brightnessProperty(), ca.saturationProperty()
36 | }) {
37 | Slider slider = new Slider(-1, 1, 0.);
38 | prop.bind(slider.valueProperty());
39 | root.getChildren().add(new HBox(5, slider, new Label(prop.getName())));
40 | }
41 | root.getChildren().add(iv);
42 | root.setAlignment(Pos.CENTER);
43 | root.setPadding(new Insets(10));
44 |
45 | primaryStage.setTitle("Color Adjust Demo");
46 | primaryStage.setScene(new Scene(root, 300, 400));
47 | primaryStage.show();
48 | }
49 |
50 | /**
51 | * @param args the command line arguments
52 | */
53 | public static void main(String[] args) {
54 | launch(args);
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/Chapter08/src/chapter8/colors/ColorInputDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter8.colors;
5 |
6 | import javafx.application.Application;
7 | import javafx.scene.Scene;
8 | import javafx.scene.control.Button;
9 | import javafx.scene.layout.StackPane;
10 | import javafx.stage.Stage;
11 |
12 | /**
13 | *
14 | * @author sgrinev
15 | */
16 | public class ColorInputDemo extends Application {
17 |
18 | @Override
19 | public void start(Stage primaryStage) {
20 | Button btn = new Button();
21 | btn.setText("Say 'Hello World'");
22 | // btn.setEffect(new ColorInput(5, 5, 50, 50, Color.RED));
23 |
24 | StackPane root = new StackPane();
25 | root.getChildren().add(btn);
26 |
27 | Scene scene = new Scene(root, 300, 250);
28 |
29 | primaryStage.setTitle("Hello World!");
30 | primaryStage.setScene(scene);
31 | primaryStage.show();
32 | }
33 |
34 | /**
35 | * @param args the command line arguments
36 | */
37 | public static void main(String[] args) {
38 | launch(args);
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/Chapter08/src/chapter8/colors/SepiaToneDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter8.colors;
5 |
6 | import javafx.application.Application;
7 | import javafx.geometry.Insets;
8 | import javafx.geometry.Pos;
9 | import javafx.scene.Scene;
10 | import javafx.scene.control.Label;
11 | import javafx.scene.control.Slider;
12 | import javafx.scene.effect.SepiaTone;
13 | import javafx.scene.image.ImageView;
14 | import javafx.scene.layout.HBox;
15 | import javafx.scene.layout.VBox;
16 | import javafx.stage.Stage;
17 |
18 | /**
19 | *
20 | * @author sgrinev
21 | */
22 | public class SepiaToneDemo extends Application {
23 |
24 | @Override
25 | public void start(Stage primaryStage) {
26 | ImageView imageView = new ImageView("https://github.com/sgrinev/mastering-javafx-9-book/blob/master/resources/sample.jpg?raw=true");
27 | imageView.setFitHeight(240);
28 | imageView.setFitWidth(240);
29 | SepiaTone st = new SepiaTone();
30 | imageView.setEffect(st);
31 |
32 | VBox root = new VBox(10);
33 | Slider slider = new Slider(0, 1, 0.5);
34 | st.levelProperty().bind(slider.valueProperty());
35 | root.getChildren().add(new HBox(5, slider, new Label("level")));
36 | root.getChildren().add(imageView);
37 | root.setAlignment(Pos.CENTER);
38 | root.setPadding(new Insets(10));
39 |
40 | primaryStage.setTitle("Color Adjust Demo");
41 | primaryStage.setScene(new Scene(root, 300, 400));
42 | primaryStage.show();
43 | }
44 |
45 | /**
46 | * @param args the command line arguments
47 | */
48 | public static void main(String[] args) {
49 | launch(args);
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/Chapter08/src/chapter8/combining/BlendDiffDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter8.combining;
5 |
6 | import javafx.application.Application;
7 | import javafx.scene.Scene;
8 | import javafx.scene.effect.Blend;
9 | import javafx.scene.effect.BlendMode;
10 | import javafx.scene.effect.ImageInput;
11 | import javafx.scene.image.Image;
12 | import javafx.scene.image.ImageView;
13 | import javafx.scene.layout.HBox;
14 | import javafx.stage.Stage;
15 |
16 | /**
17 | *
18 | * @author sgrinev
19 | */
20 | public class BlendDiffDemo extends Application {
21 |
22 | @Override
23 | public void start(Stage primaryStage) {
24 | Image image = new Image(
25 | "https://raw.githubusercontent.com/sgrinev/mastering-javafx-9-10-book/master/resources/sample.jpg",
26 | 200, 200, true, true);
27 | ImageInput ii = new ImageInput(image, 0, 0);
28 |
29 | Image image2 = new Image(
30 | "https://raw.githubusercontent.com/sgrinev/mastering-javafx-9-10-book/master/resources/sample2.jpg",
31 | 200, 200, true, true);
32 |
33 | Blend blend = new Blend();
34 | blend.setMode(BlendMode.DIFFERENCE);
35 | blend.setTopInput(ii);
36 |
37 | ImageView iv = new ImageView(image2);
38 | iv.setEffect(blend);
39 |
40 | primaryStage.setTitle("Blend Demo");
41 | primaryStage.setScene(new Scene(new HBox(20, new ImageView(image), iv, new ImageView(image2)), 640, 200));
42 | primaryStage.show();
43 | }
44 |
45 | /**
46 | * @param args the command line arguments
47 | */
48 | public static void main(String[] args) {
49 | launch(args);
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/Chapter08/src/chapter8/combining/BlendSrcDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter8.combining;
5 |
6 | import javafx.application.Application;
7 | import javafx.scene.Scene;
8 | import javafx.scene.effect.Blend;
9 | import javafx.scene.effect.BlendMode;
10 | import javafx.scene.effect.ImageInput;
11 | import javafx.scene.image.Image;
12 | import javafx.scene.layout.StackPane;
13 | import javafx.scene.text.Font;
14 | import javafx.scene.text.FontWeight;
15 | import javafx.scene.text.Text;
16 | import javafx.stage.Stage;
17 |
18 | /**
19 | *
20 | * @author sgrinev
21 | */
22 | public class BlendSrcDemo extends Application {
23 |
24 | @Override
25 | public void start(Stage primaryStage) {
26 | Image image = new Image(
27 | "https://raw.githubusercontent.com/sgrinev/mastering-javafx-9-10-book/master/resources/sample.jpg",
28 | 300, 300, true, true);
29 | ImageInput ii = new ImageInput(image, 0, 0);
30 |
31 | Blend blend = new Blend();
32 | blend.setMode(BlendMode.SRC_ATOP);
33 | blend.setTopInput(ii);
34 |
35 | Text text = new Text(0, 80, "JavaFX");
36 | text.setFont(Font.font("Verdana", FontWeight.BOLD, 70));
37 | text.setEffect(blend);
38 |
39 | primaryStage.setTitle("Blend Demo");
40 | primaryStage.setScene(new Scene(new StackPane(text), 300, 300));
41 | primaryStage.show();
42 | }
43 |
44 | /**
45 | * @param args the command line arguments
46 | */
47 | public static void main(String[] args) {
48 | launch(args);
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/Chapter08/src/chapter8/combining/LightAndDarkDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter8.combining;
5 |
6 | import javafx.application.Application;
7 | import javafx.geometry.Insets;
8 | import javafx.scene.Scene;
9 | import javafx.scene.control.Label;
10 | import javafx.scene.effect.Blend;
11 | import javafx.scene.effect.BlendMode;
12 | import javafx.scene.effect.ColorInput;
13 | import javafx.scene.image.Image;
14 | import javafx.scene.image.ImageView;
15 | import javafx.scene.layout.HBox;
16 | import javafx.scene.layout.VBox;
17 | import javafx.scene.paint.Color;
18 | import javafx.scene.paint.CycleMethod;
19 | import javafx.scene.paint.LinearGradient;
20 | import javafx.scene.paint.Stop;
21 | import javafx.stage.Stage;
22 |
23 | /**
24 | *
25 | * @author sgrinev
26 | */
27 | public class LightAndDarkDemo extends Application {
28 |
29 | @Override
30 | public void start(Stage primaryStage) {
31 | VBox root = new VBox(10);
32 | root.setPadding(new Insets(10));
33 | final Image image = new Image("https://raw.githubusercontent.com/sgrinev/mastering-javafx-9-10-book/master/resources/sample.jpg", 200, 200, true, true);
34 |
35 | Stop[] stops = new Stop[]{new Stop(0, Color.WHITE), new Stop(1, Color.BLACK)};
36 | LinearGradient gradient = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);
37 | ColorInput ci = new ColorInput(0, 0, 200, 200, gradient);
38 |
39 | for (BlendMode value : new BlendMode[] { BlendMode.DARKEN, BlendMode.LIGHTEN}) {
40 | Blend blend = new Blend();
41 | blend.setMode(value);
42 |
43 | blend.setTopInput(ci);
44 | ImageView iv = new ImageView(image);
45 | iv.setEffect(blend);
46 |
47 | root.getChildren().add(new HBox(10, new Label(value.toString()), iv));
48 | }
49 |
50 | primaryStage.setTitle("Lighten and Darken");
51 | primaryStage.setScene(new Scene(root,300, 430));
52 | primaryStage.show();
53 | }
54 |
55 | /**
56 | * @param args the command line arguments
57 | */
58 | public static void main(String[] args) {
59 | launch(args);
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/Chapter08/src/chapter8/combining/ReflectionAndShadows.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter8.combining;
5 |
6 | import javafx.application.Application;
7 | import javafx.scene.Scene;
8 | import javafx.scene.effect.DropShadow;
9 | import javafx.scene.effect.InnerShadow;
10 | import javafx.scene.effect.Reflection;
11 | import javafx.scene.layout.StackPane;
12 | import javafx.scene.paint.Color;
13 | import javafx.scene.shape.Rectangle;
14 | import javafx.scene.text.Font;
15 | import javafx.scene.text.FontWeight;
16 | import javafx.scene.text.Text;
17 | import javafx.stage.Stage;
18 |
19 | /**
20 | *
21 | * @author sgrinev
22 | */
23 | public class ReflectionAndShadows extends Application {
24 |
25 | @Override
26 | public void start(Stage stage) {
27 | // outer rectangle has a inner shadow
28 | Rectangle rect = new Rectangle(120, 120);
29 | rect.setFill(Color.WHITE);
30 | rect.setStroke(Color.BLACK);
31 | rect.setStrokeWidth(4);
32 | rect.setEffect(new InnerShadow(15, Color.BLACK));
33 |
34 | // text has a drop shadow
35 | Text text = new Text("FX");
36 | text.setFont(Font.font("Arial", FontWeight.BOLD, 80));
37 | text.setEffect(new DropShadow());
38 |
39 | // to apply reflection to both
40 | // we need to have a common node
41 | StackPane square = new StackPane(rect, text);
42 | // this way our StackPane will be small
43 | // and just wrap the rectangle
44 | square.setMaxSize(0, 0);
45 |
46 | // aplying reflection
47 | Reflection reflection = new Reflection();
48 | reflection.setFraction(0.45);
49 | square.setEffect(reflection);
50 |
51 | StackPane root = new StackPane(square);
52 | stage.setTitle("Effects Demo");
53 | stage.setScene(new Scene(root, 300, 250));
54 | stage.show();
55 | }
56 |
57 | /**
58 | * @param args the command line arguments
59 | */
60 | public static void main(String[] args) {
61 | launch(args);
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/Chapter08/src/chapter8/geometry/DisplacementMapDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter8.geometry;
5 |
6 | import javafx.application.Application;
7 | import javafx.scene.Scene;
8 | import javafx.scene.effect.DisplacementMap;
9 | import javafx.scene.effect.FloatMap;
10 | import javafx.scene.image.ImageView;
11 | import javafx.scene.layout.StackPane;
12 | import javafx.stage.Stage;
13 |
14 | /**
15 | *
16 | * @author sgrinev
17 | */
18 | public class DisplacementMapDemo extends Application {
19 |
20 | @Override
21 | public void start(Stage primaryStage) {
22 | final int SIDE = 240;
23 | ImageView iv = new ImageView("https://github.com/sgrinev/mastering-javafx-9-book/blob/master/resources/sample.jpg?raw=true");
24 | iv.setFitHeight(SIDE);
25 | iv.setFitWidth(SIDE);
26 |
27 | FloatMap floatMap = new FloatMap();
28 | floatMap.setWidth(SIDE);
29 | floatMap.setHeight(SIDE);
30 |
31 | for (int x = 0; x < SIDE; x++) {
32 | float v = ((float)Math.sin( x / 30. )) / 10f;
33 | for (int y = 0; y < SIDE; y++) {
34 | floatMap.setSamples(x, y, 0.0f, v);
35 | }
36 | }
37 |
38 | DisplacementMap displacementMap = new DisplacementMap();
39 | displacementMap.setWrap(true);
40 | displacementMap.setMapData(floatMap);
41 | iv.setEffect(displacementMap);
42 |
43 | primaryStage.setTitle("DisplacementMap Demo");
44 | primaryStage.setScene(new Scene(new StackPane(iv), 300, 300));
45 | primaryStage.show();
46 | }
47 |
48 | /**
49 | * @param args the command line arguments
50 | */
51 | public static void main(String[] args) {
52 | launch(args);
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/Chapter09/manifest.mf:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | X-COMMENT: Main-Class will be added automatically by build
3 |
4 |
--------------------------------------------------------------------------------
/Chapter09/nbproject/project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | org.netbeans.modules.java.j2seproject
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | Chapter9
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/Chapter09/src/chapter9/media/AudioClipDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter9.media;
5 |
6 | import javafx.application.Application;
7 | import javafx.scene.Scene;
8 | import javafx.scene.control.Button;
9 | import javafx.scene.layout.StackPane;
10 | import javafx.scene.media.AudioClip;
11 | import javafx.stage.Stage;
12 |
13 | /**
14 | *
15 | * @author sgrinev
16 | */
17 | public class AudioClipDemo extends Application {
18 |
19 | @Override
20 | public void start(Stage primaryStage) {
21 | AudioClip clickSound = new AudioClip("https://github.com/sgrinev/mastering-javafx-9-10-book/raw/master/resources/mouse-click.wav");
22 | Button btn = new Button("Play");
23 | btn.setOnAction((e) -> {
24 | clickSound.play();
25 | });
26 |
27 | StackPane root = new StackPane();
28 | root.getChildren().add(btn);
29 |
30 | Scene scene = new Scene(root, 300, 250);
31 |
32 | primaryStage.setTitle("Hello World!");
33 | primaryStage.setScene(scene);
34 | primaryStage.show();
35 | }
36 |
37 | /**
38 | * @param args the command line arguments
39 | */
40 | public static void main(String[] args) {
41 | launch(args);
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/Chapter09/src/chapter9/media/MediaAudioDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter9.media;
5 |
6 | import javafx.application.Application;
7 | import javafx.scene.Scene;
8 | import javafx.scene.layout.Pane;
9 | import javafx.scene.media.AudioSpectrumListener;
10 | import javafx.scene.media.Media;
11 | import javafx.scene.media.MediaPlayer;
12 | import javafx.scene.shape.Line;
13 | import javafx.stage.Stage;
14 |
15 | /**
16 | *
17 | * @author sgrinev
18 | */
19 | public class MediaAudioDemo extends Application {
20 |
21 | @Override
22 | public void start(Stage primaryStage) {
23 | final int MID = 50;
24 |
25 | Pane root = new Pane();
26 | Line[] lines = new Line[128];
27 | for (int i = 0; i < 128; i++) {
28 | Line line = new Line(5 + i*3, MID, 5 + i*3, MID);
29 | lines[i] = line;
30 | root.getChildren().add(line);
31 | }
32 |
33 | Media media = new Media("https://github.com/sgrinev/mastering-javafx-9-10-book/raw/master/resources/808-beat.mp3");
34 | MediaPlayer mp = new MediaPlayer(media);
35 | mp.setAudioSpectrumListener(new AudioSpectrumListener() {
36 | @Override
37 | public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
38 | System.out.print(timestamp + " " + magnitudes[6]);
39 | for (int i = 0; i < Math.max(128, magnitudes.length); i++) {
40 | lines[i].setEndY(MID - magnitudes[i] + mp.getAudioSpectrumThreshold());
41 | }
42 | }
43 | });
44 |
45 | primaryStage.setTitle("AudioSpectrumDemo");
46 | primaryStage.setScene(new Scene(root, 370, 100));
47 | primaryStage.show();
48 | mp.play();
49 | }
50 |
51 | public static void main(String[] args) {
52 | launch(args);
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/Chapter09/src/chapter9/media/MediaVideoDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter9.media;
5 |
6 | import javafx.application.Application;
7 | import javafx.scene.Scene;
8 | import javafx.scene.layout.Pane;
9 | import javafx.scene.media.Media;
10 | import javafx.scene.media.MediaPlayer;
11 | import javafx.scene.media.MediaView;
12 | import javafx.stage.Stage;
13 |
14 | /**
15 | *
16 | * @author sgrinev
17 | */
18 | public class MediaVideoDemo extends Application {
19 |
20 | @Override
21 | public void start(Stage primaryStage) {
22 | Media media = new Media("http://www.sample-videos.com/video/mp4/240/big_buck_bunny_240p_1mb.mp4");
23 | MediaPlayer mediaPlayer = new MediaPlayer(media);
24 | MediaView mediaView = new MediaView(mediaPlayer);
25 |
26 | primaryStage.setTitle("VideoSpectrumDemo");
27 | primaryStage.setScene(new Scene(new Pane(mediaView), 320, 240));
28 | primaryStage.show();
29 | mediaPlayer.play();
30 | }
31 |
32 | public static void main(String[] args) {
33 | launch(args);
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/Chapter09/src/chapter9/media/MediaVideoEffectsDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter9.media;
5 |
6 | import javafx.application.Application;
7 | import javafx.scene.Scene;
8 | import javafx.scene.effect.DisplacementMap;
9 | import javafx.scene.effect.FloatMap;
10 | import javafx.scene.layout.Pane;
11 | import javafx.scene.media.Media;
12 | import javafx.scene.media.MediaPlayer;
13 | import javafx.scene.media.MediaView;
14 | import javafx.stage.Stage;
15 |
16 | /**
17 | *
18 | * @author sgrinev
19 | */
20 | public class MediaVideoEffectsDemo extends Application {
21 |
22 | @Override
23 | public void start(Stage primaryStage) {
24 | Media media = new Media("http://www.sample-videos.com/video/mp4/240/big_buck_bunny_240p_1mb.mp4");
25 | MediaPlayer mediaPlayer = new MediaPlayer(media);
26 | MediaView mediaView = new MediaView(mediaPlayer);
27 |
28 | FloatMap floatMap = new FloatMap();
29 | floatMap.setWidth(320);
30 | floatMap.setHeight(240);
31 |
32 | for (int x = 0; x < 320; x++) {
33 | double v = Math.sin(x / 30.) / 10.;
34 | for (int y = 0; y < 240; y++) {
35 | floatMap.setSamples(x, y, 0.0f, (float) v);
36 | }
37 | }
38 |
39 | DisplacementMap displacementMap = new DisplacementMap();
40 | displacementMap.setWrap(true);
41 | displacementMap.setMapData(floatMap);
42 | mediaView.setEffect(displacementMap);
43 |
44 | primaryStage.setTitle("VideoSpectrumDemo");
45 | primaryStage.setScene(new Scene(new Pane(mediaView), 320, 240));
46 | primaryStage.show();
47 | mediaPlayer.play();
48 | }
49 |
50 | public static void main(String[] args) {
51 | launch(args);
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/Chapter09/src/chapter9/web/DOMModelDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter9.web;
5 |
6 | import javafx.application.Application;
7 | import javafx.concurrent.Worker;
8 | import javafx.scene.Scene;
9 | import javafx.scene.layout.StackPane;
10 | import javafx.scene.web.WebEngine;
11 | import javafx.scene.web.WebView;
12 | import javafx.stage.Stage;
13 | import org.w3c.dom.Document;
14 | import org.w3c.dom.NodeList;
15 |
16 | /**
17 | *
18 | * @author sgrinev
19 | */
20 | public class DOMModelDemo extends Application {
21 |
22 | @Override
23 | public void start(Stage stage) {
24 | WebView webView = new WebView();
25 | WebEngine webEngine = webView.getEngine();
26 | webEngine.load("https://stackoverflow.com/questions/tagged/javafx");
27 |
28 | webEngine.getLoadWorker().stateProperty().addListener(
29 | (observable, oldValue, newValue) -> {
30 | // remember, we need to get the page loaded first
31 | if (newValue == Worker.State.SUCCEEDED) {
32 | Document document = webEngine.getDocument();
33 | NodeList links = webEngine.getDocument().getElementsByTagName("a");
34 | for (int i = 0; i < links.getLength(); i++) {
35 | System.out.println(links.item(i));
36 | }
37 | } else if (newValue == Worker.State.FAILED) {
38 | System.out.println("Page loading has failed!");
39 | }
40 |
41 | });
42 |
43 | stage.setTitle("JavaFX on SO");
44 | stage.setScene(new Scene(new StackPane(webView), 400, 300));
45 | stage.show();
46 | }
47 |
48 | public static void main(String[] args) {
49 | launch();
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/Chapter09/src/chapter9/web/JS2JavaBridgeDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter9.web;
5 |
6 | import javafx.application.Application;
7 | import javafx.geometry.Insets;
8 | import javafx.scene.Scene;
9 | import javafx.scene.layout.StackPane;
10 | import javafx.scene.web.WebEngine;
11 | import javafx.scene.web.WebView;
12 | import javafx.stage.Stage;
13 | import netscape.javascript.JSObject;
14 |
15 | /**
16 | *
17 | * @author sgrinev
18 | */
19 | public class JS2JavaBridgeDemo extends Application {
20 | // declaring root as a variable to have access to it from setColor() method
21 | StackPane root;
22 |
23 | @Override
24 | public void start(Stage primaryStage) {
25 | WebView webView = new WebView();
26 | WebEngine webEngine = webView.getEngine();
27 |
28 | // here we have an HTML page with a text box and a button
29 | // pressing the button will take entered text value and
30 | // pass it to the JavaFX application
31 | webEngine.loadContent(
32 | ""
33 | + "
");
34 | JSObject window = (JSObject) webEngine.executeScript("window");
35 | window.setMember("app", this);
36 |
37 |
38 | root = new StackPane(webView);
39 | // adding padding to have visible part of the background
40 | root.setPadding(new Insets(10));
41 | primaryStage.setTitle("JavaFX JavaScript to JavaFX bridge demo");
42 | primaryStage.setScene(new Scene(root, 300, 250));
43 | primaryStage.show();
44 | }
45 |
46 | // this method we will be calling from JavaScript
47 | public void setColor(String param) {
48 | // taking parameter and apply it as a color to the background
49 | root.setStyle("-fx-background-color: " + param + ";");
50 | }
51 |
52 | public static void main(String[] args) {
53 | launch(args);
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/Chapter09/src/chapter9/web/LoadWorkerDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter9.web;
5 |
6 | import javafx.application.Application;
7 | import javafx.concurrent.Worker;
8 | import javafx.scene.Scene;
9 | import javafx.scene.control.ProgressBar;
10 | import javafx.scene.layout.VBox;
11 | import javafx.scene.web.WebEngine;
12 | import javafx.scene.web.WebView;
13 | import javafx.stage.Stage;
14 |
15 | /**
16 | *
17 | * @author sgrinev
18 | */
19 | public class LoadWorkerDemo extends Application {
20 |
21 | @Override
22 | public void start(Stage primaryStage) {
23 | WebView webView = new WebView();
24 | WebEngine webEngine = webView.getEngine();
25 | webEngine.load("https://stackoverflow.com/questions/tagged/javafx");
26 |
27 | ProgressBar loadingBar = new ProgressBar(0);
28 | loadingBar.setMinWidth(400);
29 |
30 | // using binding to easily connect the worker and the progress bar
31 | loadingBar.progressProperty().bind(
32 | webEngine.getLoadWorker().progressProperty());
33 |
34 | webEngine.getLoadWorker().stateProperty().addListener(
35 | (observable, oldValue, newValue) -> {
36 | if (newValue == Worker.State.SUCCEEDED) {
37 | System.out.println("Page was successfully loaded!");
38 | } else if (newValue == Worker.State.FAILED) {
39 | System.out.println("Page loading has failed!");
40 | }
41 |
42 | });
43 |
44 | VBox root = new VBox(5, loadingBar, webView);
45 | primaryStage.setTitle("JavaFX on SO");
46 | primaryStage.setScene(new Scene(root, 400, 300));
47 | primaryStage.show();
48 | }
49 |
50 | /**
51 | * @param args the command line arguments
52 | */
53 | public static void main(String[] args) {
54 | launch(args);
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/Chapter09/src/chapter9/web/WebEngineDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter9.web;
5 |
6 | import javafx.application.Application;
7 | import javafx.scene.Scene;
8 | import javafx.scene.control.Label;
9 | import javafx.scene.layout.StackPane;
10 | import javafx.scene.web.WebEngine;
11 | import javafx.scene.web.WebView;
12 | import javafx.stage.Stage;
13 | import javafx.stage.StageStyle;
14 |
15 | /**
16 | *
17 | * @author sgrinev
18 | */
19 | public class WebEngineDemo extends Application {
20 |
21 | @Override
22 | public void start(Stage primaryStage) {
23 | WebView webView = new WebView();
24 | WebEngine webEngine = webView.getEngine();
25 |
26 | webEngine.setOnAlert((event) -> {
27 | Stage stage = new Stage((StageStyle.UTILITY));
28 | stage.setScene(new Scene(new StackPane(new Label(event.getData())), 100, 80));
29 | stage.show();
30 | });
31 | webEngine.setCreatePopupHandler((popupFeatures) -> {
32 | Stage stage = new Stage((StageStyle.UTILITY));
33 | WebView webViewPopup = new WebView();
34 | stage.setScene(new Scene(new StackPane(webViewPopup), 300, 300));
35 | stage.show();
36 | return webViewPopup.getEngine();
37 | });
38 | webEngine.loadContent("Open StackOverflow");
39 |
40 |
41 | StackPane root = new StackPane(webView);
42 | primaryStage.setTitle("JavaFX Web Engine Demo");
43 | primaryStage.setScene(new Scene(root, 300, 250));
44 | primaryStage.show();
45 | }
46 |
47 | /**
48 | * @param args the command line arguments
49 | */
50 | public static void main(String[] args) {
51 | launch(args);
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/Chapter09/src/chapter9/web/WebViewDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter9.web;
5 |
6 | import javafx.application.Application;
7 | import javafx.scene.Scene;
8 | import javafx.scene.layout.StackPane;
9 | import javafx.scene.web.WebEngine;
10 | import javafx.scene.web.WebView;
11 | import javafx.stage.Stage;
12 |
13 | /**
14 | *
15 | * @author sgrinev
16 | */
17 | public class WebViewDemo extends Application {
18 |
19 | @Override
20 | public void start(Stage primaryStage) {
21 | WebView webView = new WebView();
22 | webView.setContextMenuEnabled(true);
23 | // webView.setZoom(1.2);
24 | // webView.setFontScale(1.5);
25 | WebEngine webEngine = webView.getEngine();
26 | webEngine.load("https://stackoverflow.com/questions/tagged/javafx");
27 |
28 | StackPane root = new StackPane(webView);
29 |
30 | Scene scene = new Scene(root, 400, 250);
31 |
32 | primaryStage.setTitle("JavaFX on SO");
33 | primaryStage.setScene(scene);
34 | primaryStage.show();
35 | }
36 |
37 | /**
38 | * @param args the command line arguments
39 | */
40 | public static void main(String[] args) {
41 | launch(args);
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/Chapter10/manifest.mf:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | X-COMMENT: Main-Class will be added automatically by build
3 |
4 |
--------------------------------------------------------------------------------
/Chapter10/nbproject/project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | org.netbeans.modules.java.j2seproject
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | Chapter10
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Chapter10/src/chapter10/chart/CrazyLineChartDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd), 2017-2018
3 | */
4 | package chapter10.chart;
5 |
6 | import javafx.application.Application;
7 | import javafx.collections.FXCollections;
8 | import javafx.scene.Scene;
9 | import javafx.scene.chart.LineChart;
10 | import javafx.scene.chart.NumberAxis;
11 | import javafx.scene.chart.XYChart;
12 | import javafx.stage.Stage;
13 |
14 | /**
15 | *
16 | * @author sgrinev
17 | */
18 | public class CrazyLineChartDemo extends Application {
19 |
20 | @Override
21 | public void start(Stage stage) {
22 | NumberAxis axisX = new NumberAxis("Something", 1, 12, 1);
23 | NumberAxis axisY = new NumberAxis();
24 | axisY.setLabel("Something else");
25 |
26 | XYChart.Series series = new XYChart.Series<>(
27 | "Nowhere",
28 | FXCollections.observableArrayList(
29 | new XYChart.Data<>(3, 10),
30 | new XYChart.Data<>(3, -5),
31 | new XYChart.Data<>(5, -10),
32 | new XYChart.Data<>(7, -10),
33 | new XYChart.Data<>(9, -5),
34 | new XYChart.Data<>(9, 10),
35 | new XYChart.Data<>(7, 15),
36 | new XYChart.Data<>(5, 15)
37 | ));
38 |
39 | LineChart chart = new LineChart<>(axisX, axisY);
40 | chart.getData().addAll(series);
41 | Scene scene = new Scene(chart, 500, 350);
42 |
43 | stage.setTitle("Crazy graph!");
44 | stage.setScene(scene);
45 | stage.show();
46 | }
47 |
48 | /**
49 | * @param args the command line arguments
50 | */
51 | public static void main(String[] args) {
52 | launch(args);
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/Chapter10/src/chapter10/chart/PieChartDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd), 2017-2018
3 | */
4 | package chapter10.chart;
5 |
6 | import javafx.application.Application;
7 | import javafx.collections.FXCollections;
8 | import javafx.collections.ObservableList;
9 | import javafx.geometry.Side;
10 | import javafx.scene.Scene;
11 | import javafx.scene.chart.PieChart;
12 | import javafx.stage.Stage;
13 |
14 | /**
15 | *
16 | * @author sgrinev
17 | */
18 | public class PieChartDemo extends Application {
19 |
20 | @Override
21 | public void start(Stage stage) {
22 |
23 | ObservableList data
24 | = FXCollections.observableArrayList(
25 | new PieChart.Data("Luck", 10),
26 | new PieChart.Data("Skill", 30),
27 | new PieChart.Data("Power of Will", 15),
28 | new PieChart.Data("Pleasure", 5),
29 | new PieChart.Data("Pain", 40));
30 |
31 | PieChart chart = new PieChart(data);
32 | chart.setTitle("Success");
33 | // chart.setLabelsVisible(false);
34 | // chart.setLegendVisible(false);
35 | chart.setLegendSide(Side.LEFT);
36 |
37 | stage.setScene(new Scene(chart, 530, 400));
38 | stage.setTitle("Pie Chart Demo");
39 | stage.show();
40 |
41 | chart.setOnMouseClicked((e) -> {
42 | data.add(new PieChart.Data("Stuff", 10));
43 | });
44 | }
45 |
46 | public static void main(String[] args) {
47 | launch(args);
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/Chapter10/src/chapter10/list/ChoiceBoxListCellDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter10.list;
5 |
6 | import javafx.application.Application;
7 | import javafx.collections.FXCollections;
8 | import javafx.collections.ListChangeListener;
9 | import javafx.collections.ObservableList;
10 | import javafx.scene.Scene;
11 | import javafx.scene.control.ListView;
12 | import javafx.scene.control.cell.ChoiceBoxListCell;
13 | import javafx.scene.layout.StackPane;
14 | import javafx.stage.Stage;
15 |
16 | /**
17 | *
18 | * @author sgrinev
19 | */
20 | public class ChoiceBoxListCellDemo extends Application {
21 |
22 | @Override
23 | public void start(Stage stage) {
24 | ObservableList values = FXCollections.observableArrayList(
25 | "Red", "Blue", "Yellow", "Green");
26 |
27 | ObservableList items = FXCollections.observableArrayList(
28 | "Gray", "Gray", "Gray", "Gray");
29 |
30 | ListView list = new ListView<>(items);
31 | list.setEditable(true);
32 | list.setCellFactory(ChoiceBoxListCell.forListView(values));
33 |
34 | items.addListener((ListChangeListener.Change extends String> change) -> {
35 | System.out.println(change);
36 | });
37 |
38 | stage.setTitle("ChoiceBoxListCell Demo");
39 | stage.setScene(new Scene(new StackPane(list), 200, 200));
40 | stage.show();
41 | }
42 |
43 | public static void main(String[] args) {
44 | launch(args);
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/Chapter10/src/chapter10/list/CustomCelfFactoryDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter10.list;
5 |
6 | import java.util.Random;
7 | import javafx.application.Application;
8 | import javafx.collections.FXCollections;
9 | import javafx.collections.ObservableList;
10 | import javafx.geometry.Insets;
11 | import javafx.scene.Scene;
12 | import javafx.scene.control.Button;
13 | import javafx.scene.control.ListCell;
14 | import javafx.scene.control.ListView;
15 | import javafx.scene.layout.VBox;
16 | import javafx.scene.paint.Color;
17 | import javafx.scene.shape.Rectangle;
18 | import javafx.stage.Stage;
19 |
20 | /**
21 | *
22 | * @author sgrinev
23 | */
24 | public class CustomCelfFactoryDemo extends Application {
25 |
26 | @Override
27 | public void start(Stage stage) {
28 | ObservableList items = FXCollections.observableArrayList(
29 | "Red", "Blue", "Yellow", "Green");
30 | ListView list = new ListView<>(items);
31 |
32 | list.setCellFactory((ListView param) -> {
33 | return new ListCell() {
34 | @Override
35 | public void updateItem(String item, boolean empty) {
36 | super.updateItem(item, empty);
37 | if (!(empty || item == null)) {
38 | setGraphic(new Rectangle(30, 30, Color.web(item)));
39 | setText(item);
40 | } else {
41 | setText(null);
42 | setGraphic(null);
43 | }
44 | }
45 | };
46 | });
47 |
48 | Button btn = new Button("Add New Item");
49 | btn.setOnAction((e) -> {
50 | items.add(
51 | // just a way to generate semirandom new items
52 | items.get(new Random().nextInt(items.size()))
53 | );
54 | System.out.println(items.size());
55 | });
56 |
57 | VBox root = new VBox(5);
58 | root.setPadding(new Insets(7));
59 | root.getChildren().addAll(list, btn);
60 | stage.setTitle("Custom List Cell");
61 | stage.setScene(new Scene(root, 200, 250));
62 | stage.show();
63 | }
64 |
65 | public static void main(String[] args) {
66 | launch(args);
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/Chapter10/src/chapter10/list/ListViewDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter10.list;
5 |
6 | import java.util.Random;
7 | import javafx.application.Application;
8 | import javafx.collections.FXCollections;
9 | import javafx.collections.ObservableList;
10 | import javafx.geometry.Insets;
11 | import javafx.scene.Scene;
12 | import javafx.scene.control.Button;
13 | import javafx.scene.control.ListView;
14 | import javafx.scene.layout.VBox;
15 | import javafx.stage.Stage;
16 |
17 | /**
18 | *
19 | * @author sgrinev
20 | */
21 | public class ListViewDemo extends Application {
22 |
23 | @Override
24 | public void start(Stage stage) {
25 | ObservableList items = FXCollections.observableArrayList(
26 | "Red", "Blue", "Yellow", "Green");
27 | ListView list = new ListView<>(items);
28 |
29 | Button btn = new Button("Add New Item");
30 | btn.setOnAction((e) -> {
31 | items.add(
32 | // just a way to generate semirandom new items
33 | items.get(new Random().nextInt(items.size()))
34 | );
35 | });
36 |
37 | VBox root = new VBox(5);
38 | root.setPadding(new Insets(7));
39 | root.getChildren().addAll(list, btn);
40 | stage.setTitle("List");
41 | stage.setScene(new Scene(root, 200, 350));
42 | stage.show();
43 | }
44 |
45 | public static void main(String[] args) {
46 | launch(args);
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/Chapter10/src/chapter10/list/MultiselectDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter10.list;
5 |
6 | import javafx.application.Application;
7 | import javafx.collections.FXCollections;
8 | import javafx.collections.ObservableList;
9 | import javafx.scene.Scene;
10 | import javafx.scene.control.ListView;
11 | import javafx.scene.control.SelectionMode;
12 | import javafx.scene.layout.StackPane;
13 | import javafx.stage.Stage;
14 |
15 | /**
16 | *
17 | * @author sgrinev
18 | */
19 | public class MultiselectDemo extends Application {
20 |
21 | @Override
22 | public void start(Stage stage) {
23 | ObservableList items = FXCollections.observableArrayList(
24 | "Red", "Blue", "Yellow", "Green",
25 | "Magenta", "Pink", "Cyan", "Gray");
26 | ListView list = new ListView<>(items);
27 | list.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
28 |
29 | list.getSelectionModel().selectedIndexProperty().addListener((obs) -> {
30 | System.out.println("Selected: " + list.getSelectionModel().getSelectedItems());
31 | System.out.println("Focused: " + list.getFocusModel().getFocusedItem());
32 | });
33 |
34 | stage.setTitle("List");
35 | stage.setScene(new Scene(new StackPane(list), 200, 350));
36 | stage.show();
37 | }
38 |
39 | public static void main(String[] args) {
40 | launch(args);
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/Chapter10/src/chapter10/list/TextFieldListCellDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter10.list;
5 |
6 | import javafx.application.Application;
7 | import javafx.collections.FXCollections;
8 | import javafx.collections.ListChangeListener;
9 | import javafx.collections.ObservableList;
10 | import javafx.scene.Scene;
11 | import javafx.scene.control.ListView;
12 | import javafx.scene.control.cell.TextFieldListCell;
13 | import javafx.scene.layout.StackPane;
14 | import javafx.stage.Stage;
15 | import javafx.util.converter.IntegerStringConverter;
16 |
17 | /**
18 | *
19 | * @author sgrinev
20 | */
21 | public class TextFieldListCellDemo extends Application {
22 |
23 | @Override
24 | public void start(Stage stage) {
25 | ObservableList items = FXCollections.observableArrayList(
26 | 100, 200, 500, 1000);
27 |
28 | ListView list = new ListView<>(items);
29 | list.setEditable(true);
30 | list.setCellFactory(TextFieldListCell.forListView(new IntegerStringConverter()));
31 |
32 | items.addListener((ListChangeListener.Change extends Integer> change) -> {
33 | System.out.println(change);
34 | });
35 |
36 | stage.setTitle("TextFieldListCell Demo");
37 | stage.setScene(new Scene(new StackPane(list), 200, 200));
38 | stage.show();
39 | }
40 |
41 | public static void main(String[] args) {
42 | launch(args);
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/Chapter10/src/chapter10/skins/ClockControl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter10.skins;
5 |
6 | import java.text.SimpleDateFormat;
7 | import java.util.Date;
8 | import javafx.animation.KeyFrame;
9 | import javafx.animation.Timeline;
10 | import javafx.beans.property.ObjectProperty;
11 | import javafx.beans.property.SimpleObjectProperty;
12 | import javafx.scene.control.Control;
13 | import javafx.scene.control.Skin;
14 | import javafx.util.Duration;
15 |
16 | /**
17 | *
18 | * @author sgrinev
19 | */
20 | // now as we are making a proper control we can base it on JavaFX Control class
21 | public class ClockControl extends Control {
22 |
23 | // enum to select between our skin types
24 | public enum SkinType { HANDS, TEXT };
25 | private final SkinType skinType;
26 |
27 | // implementing a method from Skinnable interface
28 | @Override
29 | protected Skin> createDefaultSkin() {
30 | if (skinType == SkinType.HANDS)
31 | return new ClockSkinHands(this);
32 | else
33 | return new ClockSkinText(this);
34 | }
35 |
36 | // this is our model data — time
37 | final ObjectProperty timeProp = new SimpleObjectProperty<>(new Date());
38 | public ObjectProperty timeProperty() {
39 | return timeProp;
40 | }
41 |
42 | public ClockControl(SkinType skinType) {
43 | this.skinType = skinType;
44 | // this is out "business logic" — updating time value
45 | Timeline ttimer = new Timeline(new KeyFrame(Duration.seconds(1),
46 | (event) -> {
47 | timeProp.setValue(new Date());
48 | }));
49 | ttimer.setCycleCount(Timeline.INDEFINITE);
50 | ttimer.playFrom(Duration.millis(999));
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Chapter10/src/chapter10/skins/ClockDemo.java:
--------------------------------------------------------------------------------
1 | package chapter10.skins;
2 |
3 | import javafx.application.Application;
4 | import javafx.scene.Scene;
5 | import javafx.scene.layout.HBox;
6 | import javafx.stage.Stage;
7 |
8 | /**
9 | *
10 | * @author sgrinev
11 | */
12 | public class ClockDemo extends Application {
13 |
14 |
15 | @Override
16 | public void start(Stage stage) {
17 | Scene scene = new Scene(new HBox(50,
18 | new ClockControl(ClockControl.SkinType.HANDS),
19 | new ClockControl(ClockControl.SkinType.TEXT)
20 | ), 400, 250);
21 | stage.setScene(scene);
22 | stage.setTitle("Clock, chapter 10");
23 |
24 | stage.show();
25 | }
26 |
27 | public static void main(String[] args) {
28 | launch(args);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Chapter10/src/chapter10/skins/ClockSkinText.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter10.skins;
5 |
6 | import java.text.SimpleDateFormat;
7 | import java.util.Date;
8 | import javafx.beans.binding.Bindings;
9 | import javafx.scene.Node;
10 | import javafx.scene.control.Skin;
11 | import javafx.scene.layout.StackPane;
12 | import javafx.scene.paint.Color;
13 | import javafx.scene.shape.Rectangle;
14 | import javafx.scene.text.Font;
15 | import javafx.scene.text.FontPosture;
16 | import javafx.scene.text.FontWeight;
17 | import javafx.scene.text.Text;
18 |
19 | /**
20 | *
21 | * @author sgrinev
22 | */
23 | public class ClockSkinText implements Skin {
24 |
25 | private final ClockControl control;
26 | private final StackPane root;
27 | private final Text txtTime = new Text();
28 |
29 | // skin creation
30 | public ClockSkinText(ClockControl control) {
31 | this.control = control;
32 | Rectangle border = new Rectangle(120, 50, Color.TRANSPARENT);
33 | border.setStroke(Color.BLACK);
34 | root = new StackPane(txtTime, border);
35 | txtTime.setFont(FONT);
36 | // connecting text clock with our model value
37 | txtTime.textProperty().bind(
38 | Bindings.createStringBinding(() -> {
39 | Date date = control.timeProp.get();
40 | return date == null ? "" : FORMAT.format(date);
41 | }, control.timeProp)
42 | );
43 | }
44 |
45 | // this Skin method returns the control we are "skinning"
46 | @Override
47 | public ClockControl getSkinnable() {
48 | return control;
49 | }
50 |
51 | // this Skin method returns root node of our skin
52 | @Override
53 | public Node getNode() {
54 | return root;
55 | }
56 |
57 | // this method can be used to clean any used resources
58 | @Override
59 | public void dispose() {
60 | // we have nothing to clean up
61 | }
62 |
63 | // constants
64 | private static final Font FONT = Font.font ("Courier New", FontWeight.BOLD, FontPosture.REGULAR, 20);
65 | private static final SimpleDateFormat FORMAT = new SimpleDateFormat("hh:mm:ss");
66 | }
67 |
--------------------------------------------------------------------------------
/Chapter10/src/chapter10/table/ObservableChapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter10.table;
5 |
6 | import javafx.beans.property.IntegerProperty;
7 | import javafx.beans.property.SimpleIntegerProperty;
8 | import javafx.beans.property.SimpleStringProperty;
9 | import javafx.beans.property.StringProperty;
10 |
11 | /**
12 | *
13 | * @author sgrinev
14 | */
15 | public class ObservableChapter {
16 |
17 | private final StringProperty titleProp = new SimpleStringProperty();
18 | private final IntegerProperty numberProp = new SimpleIntegerProperty();
19 |
20 | ObservableChapter(int number, String title) {
21 | titleProp.set(title);
22 | numberProp.set(number);
23 | }
24 |
25 | // Title
26 | public String getTitle() {
27 | return titleProp.get();
28 | }
29 |
30 | public void setTitle(String title) {
31 | titleProp.set(title);
32 | }
33 |
34 | // public StringProperty titleProperty() {
35 | // return titleProp;
36 | // }
37 |
38 | // Number
39 | public int getNumber() {
40 | return numberProp.get();
41 | }
42 |
43 | public void setNumber(int number) {
44 | numberProp.set(number);
45 | }
46 |
47 | public IntegerProperty numberProperty() {
48 | return numberProp;
49 | }
50 |
51 | @Override
52 | public String toString() {
53 | return "ObservableChapter{" + getNumber() + ": " + getTitle() + '}';
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/Chapter11/SimpleApp/build.sh:
--------------------------------------------------------------------------------
1 | #/bin/bash -ex
2 | # checking java version, should be at least 9
3 | "$JAVA_HOME/bin/java" -version
4 | # cleanup
5 | rm -rf build dist dist-10
6 | # compiling java files
7 | "$JAVA_HOME/bin/javac" -d build/classes src/chapterEleven/*.java src/module-info.java
8 | # adding FXML files
9 | cp src/chapterEleven/SecondStage.fxml build/classes/chapterEleven/
10 | # building jar
11 | "$JAVA_HOME/bin/javapackager" -createjar -appclass chapterEleven.SimpleApp -outdir dist -outfile SimpleApp.jar -srcdir build/classes
12 | # building stand-alone app
13 | "$JAVA_HOME/bin/jlink" --module-path "dist;$JAVA_HOME/jmods" --add-modules chapterEleven --output dist-10 --strip-debug --compress 2 --verbose --no-header-files --no-man-pages --launcher chapterEleven=chapterEleven/chapterEleven.SimpleApp
14 | # run stand-alone app
15 | ./dist-10/bin/chapterEleven
--------------------------------------------------------------------------------
/Chapter11/SimpleApp/src/chapterEleven/MyController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapterEleven;
5 |
6 | import java.net.URL;
7 | import java.util.ResourceBundle;
8 | import javafx.event.ActionEvent;
9 | import javafx.fxml.FXML;
10 | import javafx.fxml.Initializable;
11 | import javafx.scene.control.Label;
12 |
13 | /**
14 | *
15 | * @author sgrinev
16 | */
17 | public class MyController implements Initializable {
18 |
19 | @FXML
20 | private Label label;
21 |
22 | @FXML
23 | private void handleButtonAction(ActionEvent event) {
24 | label.setText("Hello World!");
25 | }
26 |
27 | @Override
28 | public void initialize(URL url, ResourceBundle rb) {
29 | System.out.println("Second Stage is being loaded.");
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Chapter11/SimpleApp/src/chapterEleven/SecondStage.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Chapter11/SimpleApp/src/chapterEleven/SimpleApp.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapterEleven;
5 |
6 | import java.io.IOException;
7 | import javafx.application.Application;
8 | import javafx.event.ActionEvent;
9 | import javafx.fxml.FXMLLoader;
10 | import javafx.scene.Parent;
11 | import javafx.scene.Scene;
12 | import javafx.scene.control.Button;
13 | import javafx.scene.layout.StackPane;
14 | import javafx.stage.Stage;
15 | import javafx.stage.StageStyle;
16 |
17 | /**
18 | *
19 | * @author sgrinev
20 | */
21 | public class SimpleApp extends Application {
22 |
23 | @Override
24 | public void start(Stage primaryStage) {
25 | Button btn = new Button();
26 | btn.setText("Open Second Window");
27 | btn.setOnAction((ActionEvent event) -> {
28 | try {
29 | Parent root = FXMLLoader.load(getClass().getResource("SecondStage.fxml"));
30 | Stage secondStage = new Stage(StageStyle.UTILITY);
31 | secondStage.setScene(new Scene(root));
32 | secondStage.show();
33 | } catch (IOException ex) {
34 | System.out.println("Failed to load FXML");
35 | ex.printStackTrace();
36 | }
37 | });
38 |
39 | StackPane root = new StackPane();
40 | root.getChildren().add(btn);
41 |
42 | Scene scene = new Scene(root, 200, 200);
43 |
44 | primaryStage.setTitle("SimpleApp");
45 | primaryStage.setScene(scene);
46 | primaryStage.show();
47 | }
48 |
49 | /**
50 | * @param args the command line arguments
51 | */
52 | public static void main(String[] args) {
53 | launch(args);
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/Chapter11/SimpleApp/src/module-info.java:
--------------------------------------------------------------------------------
1 | module chapterEleven {
2 | requires javafx.base;
3 | requires javafx.controls;
4 |
5 | requires transitive javafx.fxml;
6 | requires transitive javafx.graphics;
7 |
8 | opens chapterEleven to javafx.fxml;
9 | exports chapterEleven;
10 | }
11 |
--------------------------------------------------------------------------------
/Chapter12/manifest.mf:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | X-COMMENT: Main-Class will be added automatically by build
3 |
4 |
--------------------------------------------------------------------------------
/Chapter12/nbproject/project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | org.netbeans.modules.java.j2seproject
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | Chapter12
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/Chapter12/src/chapter12/Basic3dDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter12;
5 |
6 | import javafx.application.Application;
7 | import javafx.geometry.Point3D;
8 | import javafx.scene.Group;
9 | import javafx.scene.PerspectiveCamera;
10 | import javafx.scene.Scene;
11 | import javafx.scene.shape.Cylinder;
12 | import javafx.stage.Stage;
13 |
14 | /**
15 | *
16 | * @author sgrinev
17 | */
18 | public class Basic3dDemo extends Application {
19 |
20 | @Override
21 | public void start(Stage stage) {
22 | Cylinder cylinder = new Cylinder(40, 120);
23 | cylinder.setRotationAxis(new Point3D(50, 50, 0));
24 | cylinder.setRotate(45);
25 | cylinder.setTranslateX(150);
26 | cylinder.setTranslateY(150);
27 | cylinder.setTranslateZ(600);
28 |
29 | PerspectiveCamera camera = new PerspectiveCamera(false);
30 | camera.setTranslateX(100);
31 | camera.setTranslateY(0);
32 | camera.setTranslateZ(300);
33 |
34 | Group root = new Group(cylinder);
35 | Scene scene = new Scene(root, 300, 300, true);
36 | scene.setCamera(camera);
37 |
38 | stage.setScene(scene);
39 | stage.setTitle("3D shapes demo");
40 | stage.show();
41 |
42 | scene.setOnMouseMoved((event) -> {
43 | camera.setTranslateX(event.getSceneX()-50);
44 | camera.setTranslateY(event.getSceneY()-200);
45 | camera.setTranslateZ( 300 - event.getSceneX()/2 );
46 | });
47 | }
48 |
49 | /**
50 | * @param args the command line arguments
51 | */
52 | public static void main(String[] args) {
53 | launch(args);
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/Chapter12/src/chapter12/EarthDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter12;
5 |
6 | import javafx.animation.Interpolator;
7 | import javafx.animation.RotateTransition;
8 | import javafx.animation.Timeline;
9 | import javafx.application.Application;
10 | import javafx.geometry.Point3D;
11 | import javafx.scene.Group;
12 | import javafx.scene.PerspectiveCamera;
13 | import javafx.scene.PointLight;
14 | import javafx.scene.Scene;
15 | import javafx.scene.image.Image;
16 | import javafx.scene.paint.Color;
17 | import javafx.scene.paint.PhongMaterial;
18 | import javafx.scene.shape.Sphere;
19 | import javafx.stage.Stage;
20 | import javafx.util.Duration;
21 |
22 | /**
23 | *
24 | * @author sgrinev
25 | */
26 | public class EarthDemo extends Application {
27 |
28 | @Override
29 | public void start(Stage stage) {
30 | Sphere sphere = new Sphere(150);
31 | sphere.setTranslateX(450);
32 | sphere.setTranslateY(300);
33 | sphere.setTranslateZ(400);
34 | sphere.setRotationAxis(new Point3D(1, 1, 0));
35 |
36 | PhongMaterial mat = new PhongMaterial();
37 | Image image = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/Reversed_Earth_map_1000x500.jpg/800px-Reversed_Earth_map_1000x500.jpg");
38 | mat.setSelfIlluminationMap(image);
39 | sphere.setMaterial(mat);
40 |
41 | PointLight light = new PointLight(Color.LIGHTYELLOW);
42 | light.setTranslateX(350);
43 | light.setTranslateY(100);
44 | light.setTranslateZ(300);
45 |
46 | PerspectiveCamera camera = new PerspectiveCamera(false);
47 | camera.setTranslateX(300);
48 | camera.setTranslateY(150);
49 | camera.setTranslateZ(300);
50 |
51 | Scene scene = new Scene(new Group(sphere, light), 300, 300, true);
52 | scene.setCamera(camera);
53 |
54 | stage.setScene(scene);
55 | stage.setTitle("3D shapes demo");
56 | stage.show();
57 |
58 | RotateTransition rot = new RotateTransition(Duration.seconds(10), sphere);
59 | rot.setToAngle(360);
60 | rot.setInterpolator(Interpolator.LINEAR);
61 | rot.setCycleCount(Timeline.INDEFINITE);
62 | rot.play();
63 | }
64 |
65 | public static void main(String[] args) {
66 | launch(args);
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/Chapter12/src/chapter12/LightDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter12;
5 |
6 | import javafx.application.Application;
7 | import javafx.geometry.Point3D;
8 | import javafx.scene.Group;
9 | import javafx.scene.PerspectiveCamera;
10 | import javafx.scene.PointLight;
11 | import javafx.scene.Scene;
12 | import javafx.scene.shape.Cylinder;
13 | import javafx.stage.Stage;
14 |
15 | /**
16 | *
17 | * @author sgrinev
18 | */
19 | public class LightDemo extends Application {
20 |
21 | @Override
22 | public void start(Stage stage) {
23 |
24 | Cylinder cylinder = new Cylinder(40, 120);
25 | cylinder.setRotationAxis(new Point3D(50, 50, 0));
26 | cylinder.setRotate(45);
27 | cylinder.setTranslateX(150);
28 | cylinder.setTranslateY(150);
29 | cylinder.setTranslateZ(600);
30 |
31 | Cylinder cylinder2 = new Cylinder(40, 120);
32 | cylinder2.setRotationAxis(new Point3D(180, 0, 0));
33 | cylinder2.setRotate(45);
34 | cylinder2.setTranslateX(350);
35 | cylinder2.setTranslateY(150);
36 | cylinder2.setTranslateZ(600);
37 |
38 | PointLight light = new PointLight();
39 | light.setTranslateX(350);
40 | light.setTranslateY(100);
41 | light.setTranslateZ(300);
42 |
43 | PerspectiveCamera camera = new PerspectiveCamera(false);
44 | camera.setTranslateX(100);
45 | camera.setTranslateY(0);
46 | camera.setTranslateZ(300);
47 |
48 | Group root = new Group(cylinder, cylinder2, light);
49 | Scene scene = new Scene(root, 300, 300, true);
50 | scene.setCamera(camera);
51 |
52 | stage.setScene(scene);
53 | stage.setTitle("Light demo");
54 | stage.show();
55 |
56 | scene.setOnMouseMoved((event) -> {
57 | light.setTranslateX(event.getSceneX()-50);
58 | light.setTranslateY(event.getSceneY()-200);
59 | light.setTranslateZ( 300 - event.getSceneX()/2 );
60 | });
61 | }
62 |
63 | public static void main(String[] args) {
64 | launch(args);
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/Chapter12/src/chapter12/MaterialBumpMapDemo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Packt Publishing Ltd, 2017-2018
3 | */
4 | package chapter12;
5 |
6 | import javafx.application.Application;
7 | import javafx.geometry.Point3D;
8 | import javafx.scene.Group;
9 | import javafx.scene.PerspectiveCamera;
10 | import javafx.scene.PointLight;
11 | import javafx.scene.Scene;
12 | import javafx.scene.image.Image;
13 | import javafx.scene.paint.PhongMaterial;
14 | import javafx.scene.shape.Box;
15 | import javafx.stage.Stage;
16 |
17 | /**
18 | *
19 | * @author sgrinev
20 | */
21 | public class MaterialBumpMapDemo extends Application {
22 |
23 | @Override
24 | public void start(Stage stage) {
25 | PhongMaterial material = new PhongMaterial();
26 | material.setBumpMap(new Image("https://upload.wikimedia.org/wikipedia/commons/8/86/IntP_Brick_NormalMap.png"));
27 |
28 | Box box = new Box(100, 100, 100);
29 | box.setTranslateX(250);
30 | box.setTranslateY(100);
31 | box.setTranslateZ(400);
32 | box.setRotate(50);
33 | box.setRotationAxis(new Point3D(100, 100, 0));
34 | box.setMaterial(material);
35 |
36 | PointLight light = new PointLight();
37 | light.setTranslateX(50);
38 | light.setTranslateY(150);
39 | light.setTranslateZ(300);
40 |
41 | PerspectiveCamera camera = new PerspectiveCamera(false);
42 | camera.setTranslateX(100);
43 | camera.setTranslateY(-50);
44 | camera.setTranslateZ(300);
45 |
46 | Group root = new Group(box, light);
47 | Scene scene = new Scene(root, 300, 300, true);
48 | scene.setCamera(camera);
49 |
50 | stage.setScene(scene);
51 | stage.setTitle("Bump Map material demo");
52 | stage.show();
53 |
54 | scene.setOnMouseMoved((event) -> {
55 | light.setTranslateX(event.getSceneX()-50);
56 | light.setTranslateY(event.getSceneY()-200);
57 | light.setTranslateZ( 300 - event.getSceneX()/2 );
58 | });
59 | }
60 |
61 | /**
62 | * @param args the command line arguments
63 | */
64 | public static void main(String[] args) {
65 | launch(args);
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Packt
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------