├── .classpath
├── .gitignore
├── .project
├── .settings
├── org.eclipse.core.resources.prefs
└── org.eclipse.jdt.core.prefs
├── LICENSE.txt
├── README.md
├── bin
├── malc
│ └── layout.fxml
├── malc2
│ ├── UI.fxml
│ └── UI.fxml.bak
├── malc3
│ ├── Sample.fxml
│ └── TextArea.fxml
└── resources
│ └── icon.png
├── build.fxbuild
└── src
├── bank
├── Controller.java
├── Main.java
└── UI.fxml
├── bind
├── a.java
├── bill.java
└── j.java
├── ex
├── BackSpace.java
├── EventHandlerDemo.fxml
├── EventHandlerDemoController.java
├── KeyboardExample.java
├── MainApp.java
├── Person.java
└── ScrollBarPane.java
├── game
├── BallGame.java
├── BallGameController.java
├── GameModel.java
├── HideShowApp.java
└── SingleClassNoXmlBallGame.java
├── james
├── AvcHmi.java
├── DataSource.java
├── Indicators.java
├── Minimal.java
├── PopupExample.java
├── SecondStage.java
├── ServiceSample.java
├── s.java
└── testScheduledExecutorService.java
├── malc
├── Main.java
└── layout.fxml
├── malc2
├── Main.java
├── Methods.java
├── UI.fxml
└── UI.fxml.bak
├── malc3
├── DemoShowHide.java
├── Sample.fxml
├── Sample.java
└── TextArea.fxml
├── resources
├── GameView.fxml
├── head.png
├── icon.png
└── style.css
└── uka
├── Account.java
└── uka.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Package Files #
4 | *.jar
5 | *.war
6 | *.ear
7 |
8 | */bin
9 | *bin
10 | *.bin
11 | *bin
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | JavaFX-Exercises
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.xtext.ui.shared.xtextBuilder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.xtext.ui.shared.xtextNature
21 | org.eclipse.jdt.core.javanature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding//src/bank/Controller.java=UTF-8
3 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.7
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.7
12 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2013 John Malc
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | 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, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | JavaFX
2 | ================
3 |
4 | Java FX Exercises
5 |
6 | JavaFX is a cross platform GUI toolkit for Java, and is the successor to the Java Swing libraries.
7 |
8 | #### Installation
9 |
10 | If you already develop applications with Java, you probably don't need to download anything at all: JavaFX has been included with the standard JDK (Java Development Kit) bundle since JDK version 7u6 (August 2012). If you haven't updated your Java installation in a while, head to the Java download website for the latest version.
11 |
12 | ### Basic Framework Classes
13 |
14 | Creating a JavaFX program begins with the Application class, from which all JavaFX applications are extended. Your main class should call the launch() method, which will then call the init() method and then the start() method, wait for the application to finish, and then call the stop() method. Of these methods, only the start() method is abstract and must be overridden.
15 |
16 | The Stage class is the top level JavaFX container. When an Application is launched, an initial Stage is created and passed to the Application's start method. Stages control basic window properties such as title, icon, visibility, resizability, fullscreen mode, and decorations; the latter is configured using StageStyle. Additional Stages may be constructed as necessary. After a Stage is configured and the content is added, the show() method is called.
17 |
18 | Knowing all this, we can write a minimal example that launches a window in JavaFX:
19 |
20 | ```
21 | import javafx.application.Application;
22 | import javafx.stage.Stage;
23 |
24 | public class Example1 extends Application
25 | {
26 | public static void main(String[] args)
27 | {
28 | launch(args);
29 | }
30 |
31 | public void start(Stage theStage)
32 | {
33 | theStage.setTitle("Hello, World!");
34 | theStage.show();
35 | }
36 | }
37 | ```
38 |
39 | ### Structuring Content
40 |
41 | Content in JavaFX (such as text, images, and UI controls) is organized using a tree-like data structure known as a scene graph, which groups and arranges the elements of a graphical scene.
42 |
43 | A general element of a scene graph in JavaFX is called a Node. Every Node in a tree has a single "parent" node, with the exception of a special Node designated as the "root". A Group is a Node which can have many "child" Node elements. Graphical transformations (translation, rotation, and scale) and effects applied to a Group also apply to its children. Nodes can be styled using JavaFX Cascading Style Sheets (CSS), quite similar to the CSS used to format HTML documents.
44 |
45 | The Scene class contains all content for a scene graph, and requires a root Node to be set (in practice, this is often a Group). You can set the size of a Scene specifically; otherwise, the size of a Scene will be automatically calculated based on its content. A Scene object must be passed to the Stage (by the setScene() method) in order to be displayed.
46 |
--------------------------------------------------------------------------------
/bin/malc/layout.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/bin/malc2/UI.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/bin/malc2/UI.fxml.bak:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/bin/malc3/Sample.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/bin/malc3/TextArea.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/bin/resources/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmpe/JavaFX/2b2c87f417866c7fa703f0e06927d972fc3a32a9/bin/resources/icon.png
--------------------------------------------------------------------------------
/build.fxbuild:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/bank/Controller.java:
--------------------------------------------------------------------------------
1 | package bank;
2 |
3 | import java.net.URL;
4 | import java.util.ResourceBundle;
5 |
6 | import javafx.collections.FXCollections;
7 | import javafx.fxml.FXML;
8 | import javafx.scene.control.Button;
9 | import javafx.scene.control.ComboBox;
10 | import javafx.scene.control.RadioButton;
11 | import javafx.scene.control.TextField;
12 |
13 | /*
14 | * prepsat bez pouziti fxml-ka, nevim jak na to
15 | */
16 | public class Controller {
17 |
18 | @FXML
19 | TextField InsertPinNumber;
20 | @FXML
21 | TextField InsertAccNumber;
22 | @FXML
23 | TextField InfoTetx;
24 | @FXML
25 | TextField AmmountMoneyToWid;
26 | @FXML
27 | Button PinAccept;
28 | @FXML
29 | Button CardAccept;
30 | @FXML
31 | Button StateOfAccount;
32 | @FXML
33 | Button MoneyToWith;
34 | @FXML
35 | Button CardOut;
36 | @FXML
37 | RadioButton FreeMoney;
38 | @FXML
39 | RadioButton ChoiceMoney;
40 | @FXML
41 | ComboBox ComboBox = new ComboBox(
42 | FXCollections.observableArrayList(10, 20, 30));
43 |
44 | @FXML
45 | ResourceBundle resources;
46 | @FXML
47 | URL location;
48 |
49 | @FXML
50 | public void initialize() {
51 | // ComboBox.getItems().clear();
52 | // ComboBox.getItems().addAll(10, 20, 30);
53 |
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/src/bank/Main.java:
--------------------------------------------------------------------------------
1 | package bank;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 |
9 | public class Main extends Application {
10 | private static String UI = "UI.fxml";
11 |
12 | @Override
13 | public void start(Stage primaryStage) throws Exception {
14 | Parent root = FXMLLoader.load(getClass().getResource(UI));
15 | Scene frame = new Scene(root);
16 | primaryStage.isResizable();
17 | primaryStage.setTitle("Bank Business");
18 | primaryStage.setScene(frame);
19 | primaryStage.centerOnScreen();
20 | primaryStage.show();
21 |
22 | }
23 |
24 | public static void main(String[] args) {
25 | launch(args);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/bank/UI.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
13 |
14 |
16 |
18 |
20 |
22 |
24 |
26 |
28 |
30 |
32 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
45 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/src/bind/a.java:
--------------------------------------------------------------------------------
1 | package bind;
2 |
3 | import javafx.beans.value.*;
4 |
5 | public class a {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | bill electricBill = new bill();
10 |
11 | electricBill.amountDueProperty().addListener(new ChangeListener() {
12 | @Override
13 | public void changed(ObservableValue o, Object oldVal, Object newVal) {
14 | System.out.println("Electric bill has changed!");
15 | }
16 | });
17 |
18 | electricBill.setAmountDue(100.00);
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/bind/bill.java:
--------------------------------------------------------------------------------
1 | package bind;
2 |
3 | import javafx.beans.property.*;
4 |
5 | public class bill {
6 | // Define a variable to store the property
7 | private DoubleProperty amountDue = new SimpleDoubleProperty();
8 |
9 | // Define a getter for the property's value
10 | public final double getAmountDue() {
11 | return amountDue.get();
12 | }
13 |
14 | // Define a setter for the property's value
15 | public final void setAmountDue(double value) {
16 | amountDue.set(value);
17 | }
18 |
19 | // Define a getter for the property itself
20 | public DoubleProperty amountDueProperty() {
21 | return amountDue;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/bind/j.java:
--------------------------------------------------------------------------------
1 | package bind;
2 |
3 | import javafx.beans.*;
4 | import javafx.beans.binding.*;
5 |
6 | public class j {
7 | public static void main(String[] args) {
8 |
9 |
10 | bill bill1 = new bill();
11 | bill bill2 = new bill();
12 | bill bill3 = new bill();
13 | NumberBinding total =
14 | Bindings.add(bill1.amountDueProperty().add(bill2.amountDueProperty()),
15 | bill3.amountDueProperty());
16 | total.addListener(new InvalidationListener() {
17 |
18 | @Override public void invalidated(Observable o) {
19 | System.out.println("The binding is now invalid.");
20 | }
21 | });
22 |
23 | // First call makes the binding invalid
24 | bill1.setAmountDue(200.00);
25 |
26 | // The binding is now invalid
27 | bill2.setAmountDue(100.00);
28 | bill3.setAmountDue(75.00);
29 |
30 | // Make the binding valid...
31 | System.out.println(total.getValue());
32 |
33 | // Make invalid...
34 | bill3.setAmountDue(150.00);
35 |
36 | // Make valid...
37 | System.out.println(total.getValue());
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/ex/BackSpace.java:
--------------------------------------------------------------------------------
1 | package ex;
2 |
3 | import javafx.application.Application;
4 | import javafx.beans.InvalidationListener;
5 | import javafx.beans.Observable;
6 | import javafx.collections.FXCollections;
7 | import javafx.collections.ObservableList;
8 | import javafx.event.EventHandler;
9 | import javafx.scene.Group;
10 | import javafx.scene.Scene;
11 | import javafx.scene.layout.HBox;
12 | import javafx.scene.layout.VBox;
13 | import javafx.scene.text.Font;
14 | import javafx.stage.Stage;
15 |
16 | import javafx.scene.control.ComboBox;
17 | import javafx.scene.control.TextField;
18 | import javafx.scene.input.KeyCode;
19 | import javafx.scene.input.KeyEvent;
20 |
21 | public class BackSpace extends Application {
22 |
23 | private final ObservableList strings = FXCollections
24 | .observableArrayList("Option 1", "Option 2", "Option 3",
25 | "Option 4", "Option 5", "Option 6",
26 | "Long ComboBox item 1 2 3 4 5 6 7 8 9", "Option 7",
27 | "Option 8", "Option 9", "Option 10", "Option 12",
28 | "Option 13", "Option 14", "Option 15", "Option 16",
29 | "Option 17", "Option 18", "Option 19", "Option 20",
30 | "Option 21", "Option 22", "Option 23", "Option 24",
31 | "Option 25", "Option 26", "Option 27", "Option 28",
32 | "Option 29", "Option 30", "Option 31", "Option 32",
33 | "Option 33", "Option 34", "Option 35", "Option 36",
34 | "Option 37", "Option 38", "Option 39", "Option 40",
35 | "Option 41", "Option 42", "Option 43", "Option 44",
36 | "Option 45", "Option 46", "Option 47", "Option 48",
37 | "Option 49", "Option 50", "Option 51", "Option 52",
38 | "Option 53", "Option 54", "Option 55", "Option 56",
39 | "Option 57", "Option 58", "Option 59", "Option 60",
40 | "Option 61", "Option 62", "Option 63", "Option 64",
41 | "Option 65", "Option 66", "Option 67", "Option 68",
42 | "Option 69", "Option 70", "Option 71", "Option 72",
43 | "Option 73", "Option 74", "Option 75");
44 | private final ObservableList fonts = FXCollections
45 | .observableArrayList(Font.getFamilies());
46 |
47 | public static void main(String[] args) {
48 | launch(args);
49 | }
50 |
51 | @Override
52 | public void start(Stage stage) {
53 | stage.setTitle("ComboBox");
54 |
55 | // non-editable column
56 | VBox nonEditBox = new VBox(15);
57 |
58 | ComboBox comboBox1 = new ComboBox();
59 | comboBox1.setEditable(true);
60 | comboBox1.setItems(FXCollections.observableArrayList(strings.subList(0,
61 | 4)));
62 | comboBox1.sceneProperty().addListener(new InvalidationListener() {
63 |
64 | @Override
65 | public void invalidated(Observable arg0) {
66 | System.out.println("sceneProperty changed!");
67 | }
68 | });
69 |
70 | nonEditBox.getChildren().add(comboBox1);
71 |
72 | ComboBox comboBox2 = new ComboBox();
73 | comboBox2.setEditable(true);
74 | comboBox2.setItems(FXCollections.observableArrayList(strings.subList(0,
75 | 5)));
76 | nonEditBox.getChildren().add(comboBox2);
77 |
78 | TextField textField1 = new TextField();
79 | nonEditBox.getChildren().add(textField1);
80 |
81 | nonEditBox.addEventHandler(KeyEvent.ANY, new EventHandler() {
82 |
83 | @Override
84 | public void handle(KeyEvent keyEvent) {
85 | if (keyEvent.getCode().equals(KeyCode.BACK_SPACE)
86 | || keyEvent.getCode().equals(KeyCode.DELETE)) {
87 | System.out.println("VBox is receiving key event !");
88 | }
89 | }
90 | });
91 |
92 | HBox vbox = new HBox(20);
93 | vbox.setLayoutX(40);
94 | vbox.setLayoutY(25);
95 |
96 | vbox.getChildren().addAll(nonEditBox);
97 | Scene scene = new Scene(new Group(vbox), 620, 190);
98 |
99 | stage.setScene(scene);
100 | stage.show();
101 |
102 | // scene.impl_focusOwnerProperty().addListener(new
103 | // ChangeListener() {
104 | // public void changed(ObservableValue extends Node> ov, Node t, Node
105 | // t1) {
106 | // System.out.println("focus moved from " + t + " to " + t1);
107 | // }
108 | // });
109 | }
110 | }
--------------------------------------------------------------------------------
/src/ex/EventHandlerDemo.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/src/ex/EventHandlerDemoController.java:
--------------------------------------------------------------------------------
1 | package ex;
2 |
3 | import javafx.beans.value.ChangeListener;
4 | import javafx.beans.value.ObservableValue;
5 | import javafx.collections.FXCollections;
6 | import javafx.collections.ObservableList;
7 | import javafx.fxml.FXML;
8 | import javafx.scene.control.CheckBox;
9 | import javafx.scene.control.ComboBox;
10 | import javafx.scene.control.ListView;
11 | import javafx.scene.control.Slider;
12 | import javafx.scene.control.TextArea;
13 | import javafx.scene.control.TextField;
14 |
15 | public class EventHandlerDemoController {
16 |
17 | @FXML
18 | private TextArea outputTextArea;
19 | @FXML
20 | private CheckBox checkBox;
21 | @FXML
22 | private ComboBox comboBox;
23 | @FXML
24 | private Slider slider;
25 | @FXML
26 | private TextField textField;
27 | @FXML
28 | private ListView listView;
29 |
30 |
31 | private ObservableList comboBoxData = FXCollections.observableArrayList();
32 | private ObservableList listViewData = FXCollections.observableArrayList();
33 |
34 | /**
35 | * The constructor. The constructor is called before the initialize()
36 | * method.
37 | */
38 | public EventHandlerDemoController() {
39 | // Create some sample data for the ComboBox and ListView
40 | comboBoxData.add(new Person("Hans", "Muster"));
41 | comboBoxData.add(new Person("Ruth", "Mueller"));
42 | comboBoxData.add(new Person("Heinz", "Kurz"));
43 | comboBoxData.add(new Person("Cornelia", "Meier"));
44 | comboBoxData.add(new Person("Werner", "Meyer"));
45 |
46 | listViewData.add(new Person("Lydia", "Kunz"));
47 | listViewData.add(new Person("Anna", "Best"));
48 | listViewData.add(new Person("Stefan", "Meier"));
49 | listViewData.add(new Person("Martin", "Mueller"));
50 | }
51 |
52 | /**
53 | * Initializes the controller class. This method is automatically called
54 | * after the fxml file has been loaded.
55 | */
56 | @FXML
57 | private void initialize() {
58 | // Init ComboBox
59 | comboBox.setItems(comboBoxData);
60 |
61 | // Listen for Slider value changes
62 | slider.valueProperty().addListener(new ChangeListener() {
63 | @Override
64 | public void changed(ObservableValue extends Number> observable,
65 | Number oldValue, Number newValue) {
66 |
67 | outputTextArea.appendText("Slider Value Changed (newValue: " + newValue.intValue() + ")\n");
68 | }
69 | });
70 |
71 | // Listen for TextField text changes
72 | textField.textProperty().addListener(new ChangeListener() {
73 | @Override
74 | public void changed(ObservableValue extends String> observable,
75 | String oldValue, String newValue) {
76 |
77 | outputTextArea.appendText("TextField Text Changed (newValue: " + newValue + ")\n");
78 | }
79 | });
80 |
81 | // Init ListView and listen for selection changes
82 | listView.setItems(listViewData);
83 | listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
84 | @Override
85 | public void changed(ObservableValue extends Person> observable,
86 | Person oldValue, Person newValue) {
87 |
88 | outputTextArea.appendText("ListView Selection Changed (newValue: " + newValue + ")\n");
89 | }
90 | });
91 | }
92 |
93 | @FXML
94 | private void handleButtonAction() {
95 | outputTextArea.appendText("Button Action\n");
96 | }
97 |
98 | @FXML
99 | private void handleCheckBoxAction() {
100 | outputTextArea.appendText("CheckBox Action (selected: " + checkBox.isSelected() + ")\n");
101 | }
102 |
103 | @FXML
104 | private void handleComboBoxAction() {
105 | Person selectedPerson = comboBox.getSelectionModel().getSelectedItem();
106 | outputTextArea.appendText("ComboBox Action (selected: " + selectedPerson + ")\n");
107 | }
108 |
109 | @FXML
110 | private void handleHyperlinkAction() {
111 | outputTextArea.appendText("Hyperlink Action\n");
112 | }
113 |
114 | @FXML
115 | private void handleTextFieldAction() {
116 | outputTextArea.appendText("TextField Action\n");
117 | }
118 | }
--------------------------------------------------------------------------------
/src/ex/KeyboardExample.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 Oracle and/or its affiliates.
3 | * All rights reserved. Use is subject to license terms.
4 | *
5 | * This file is available and licensed under the following license:
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * - Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * - Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in
15 | * the documentation and/or other materials provided with the distribution.
16 | * - Neither the name of Oracle nor the names of its
17 | * contributors may be used to endorse or promote products derived
18 | * from this software without specific prior written permission.
19 | *
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | */
32 |
33 | /*
34 | * Handling keyboard events with event handlers
35 | */
36 | package ex;
37 |
38 | import java.util.Iterator;
39 | import java.util.List;
40 |
41 | import javafx.application.Application;
42 | import javafx.beans.binding.Bindings;
43 | import javafx.beans.property.BooleanProperty;
44 | import javafx.beans.property.SimpleBooleanProperty;
45 | import javafx.event.EventHandler;
46 | import javafx.geometry.Insets;
47 | import javafx.scene.*;
48 | import javafx.scene.input.KeyCode;
49 | import javafx.scene.input.KeyEvent;
50 | import javafx.scene.layout.HBox;
51 | import javafx.scene.layout.StackPane;
52 | import javafx.scene.paint.Color;
53 | import javafx.scene.shape.Rectangle;
54 | import javafx.scene.text.*;
55 | import javafx.stage.Stage;
56 |
57 | public final class KeyboardExample extends Application {
58 | @Override
59 | public void start(final Stage stage) {
60 | final Keyboard keyboard = new Keyboard(new Key(KeyCode.A), new Key(
61 | KeyCode.S), new Key(KeyCode.D), new Key(KeyCode.F));
62 |
63 | final Scene scene = new Scene(new Group(keyboard.createNode()));
64 | stage.setScene(scene);
65 | stage.setTitle("Keyboard Example");
66 | stage.show();
67 | }
68 |
69 | public static void main(final String[] args) {
70 | launch(args);
71 | }
72 |
73 | private static final class Key {
74 | private final KeyCode keyCode;
75 | private final BooleanProperty pressedProperty;
76 |
77 | public Key(final KeyCode keyCode) {
78 | this.keyCode = keyCode;
79 | this.pressedProperty = new SimpleBooleanProperty(this, "pressed");
80 | }
81 |
82 | public KeyCode getKeyCode() {
83 | return keyCode;
84 | }
85 |
86 | public boolean isPressed() {
87 | return pressedProperty.get();
88 | }
89 |
90 | public void setPressed(final boolean value) {
91 | pressedProperty.set(value);
92 | }
93 |
94 | public Node createNode() {
95 | final StackPane keyNode = new StackPane();
96 | keyNode.setFocusTraversable(true);
97 | installEventHandler(keyNode);
98 |
99 | final Rectangle keyBackground = new Rectangle(50, 50);
100 | keyBackground.fillProperty().bind(
101 | Bindings.when(pressedProperty)
102 | .then(Color.RED)
103 | .otherwise(
104 | Bindings.when(keyNode.focusedProperty())
105 | .then(Color.LIGHTGRAY)
106 | .otherwise(Color.WHITE)));
107 | keyBackground.setStroke(Color.BLACK);
108 | keyBackground.setStrokeWidth(2);
109 | keyBackground.setArcWidth(12);
110 | keyBackground.setArcHeight(12);
111 |
112 | final Text keyLabel = new Text(keyCode.getName());
113 | keyLabel.setFont(Font.font("Arial", FontWeight.BOLD, 20));
114 |
115 | keyNode.getChildren().addAll(keyBackground, keyLabel);
116 |
117 | return keyNode;
118 | }
119 |
120 | private void installEventHandler(final Node keyNode) {
121 | // handler for enter key press / release events, other keys are
122 | // handled by the parent (keyboard) node handler
123 | final EventHandler keyEventHandler = new EventHandler() {
124 | public void handle(final KeyEvent keyEvent) {
125 | if (keyEvent.getCode() == KeyCode.ENTER) {
126 | setPressed(keyEvent.getEventType() == KeyEvent.KEY_PRESSED);
127 |
128 | keyEvent.consume();
129 | }
130 | }
131 | };
132 |
133 | keyNode.setOnKeyPressed(keyEventHandler);
134 | keyNode.setOnKeyReleased(keyEventHandler);
135 | }
136 | }
137 |
138 | private static final class Keyboard {
139 | private final Key[] keys;
140 |
141 | public Keyboard(final Key... keys) {
142 | this.keys = keys.clone();
143 | }
144 |
145 | public Node createNode() {
146 | final HBox keyboardNode = new HBox(6);
147 | keyboardNode.setPadding(new Insets(6));
148 |
149 | final List keyboardNodeChildren = keyboardNode.getChildren();
150 | for (final Key key : keys) {
151 | keyboardNodeChildren.add(key.createNode());
152 | }
153 |
154 | installEventHandler(keyboardNode);
155 | return keyboardNode;
156 | }
157 |
158 | private void installEventHandler(final Parent keyboardNode) {
159 | // handler for key pressed / released events not handled by
160 | // key nodes
161 | final EventHandler keyEventHandler = new EventHandler() {
162 | public void handle(final KeyEvent keyEvent) {
163 | final Key key = lookupKey(keyEvent.getCode());
164 | if (key != null) {
165 | key.setPressed(keyEvent.getEventType() == KeyEvent.KEY_PRESSED);
166 |
167 | keyEvent.consume();
168 | }
169 | }
170 | };
171 |
172 | keyboardNode.setOnKeyPressed(keyEventHandler);
173 | keyboardNode.setOnKeyReleased(keyEventHandler);
174 |
175 | keyboardNode.addEventHandler(KeyEvent.KEY_PRESSED,
176 | new EventHandler() {
177 | public void handle(final KeyEvent keyEvent) {
178 | handleFocusTraversal(keyboardNode, keyEvent);
179 | }
180 | });
181 | }
182 |
183 | private Key lookupKey(final KeyCode keyCode) {
184 | for (final Key key : keys) {
185 | if (key.getKeyCode() == keyCode) {
186 | return key;
187 | }
188 | }
189 | return null;
190 | }
191 |
192 | private static void handleFocusTraversal(final Parent traversalGroup,
193 | final KeyEvent keyEvent) {
194 | final Node nextFocusedNode;
195 | switch (keyEvent.getCode()) {
196 | case LEFT:
197 | nextFocusedNode = getPreviousNode(traversalGroup,
198 | (Node) keyEvent.getTarget());
199 | keyEvent.consume();
200 | break;
201 |
202 | case RIGHT:
203 | nextFocusedNode = getNextNode(traversalGroup,
204 | (Node) keyEvent.getTarget());
205 | keyEvent.consume();
206 | break;
207 |
208 | default:
209 | return;
210 | }
211 |
212 | if (nextFocusedNode != null) {
213 | nextFocusedNode.requestFocus();
214 | }
215 | }
216 |
217 | private static Node getNextNode(final Parent parent, final Node node) {
218 | final Iterator childIterator = parent
219 | .getChildrenUnmodifiable().iterator();
220 |
221 | while (childIterator.hasNext()) {
222 | if (childIterator.next() == node) {
223 | return childIterator.hasNext() ? childIterator.next()
224 | : null;
225 | }
226 | }
227 |
228 | return null;
229 | }
230 |
231 | private static Node getPreviousNode(final Parent parent, final Node node) {
232 | final Iterator childIterator = parent
233 | .getChildrenUnmodifiable().iterator();
234 | Node lastNode = null;
235 |
236 | while (childIterator.hasNext()) {
237 | final Node currentNode = childIterator.next();
238 | if (currentNode == node) {
239 | return lastNode;
240 | }
241 |
242 | lastNode = currentNode;
243 | }
244 |
245 | return null;
246 | }
247 | }
248 | }
249 |
--------------------------------------------------------------------------------
/src/ex/MainApp.java:
--------------------------------------------------------------------------------
1 | package ex;
2 |
3 | import java.io.IOException;
4 |
5 | import javafx.application.Application;
6 | import javafx.fxml.FXMLLoader;
7 | import javafx.scene.Scene;
8 | import javafx.scene.layout.AnchorPane;
9 | import javafx.stage.Stage;
10 |
11 | public class MainApp extends Application {
12 |
13 | @Override
14 | public void start(Stage primaryStage) {
15 | primaryStage.setTitle("Event Handler Demo");
16 |
17 | try {
18 | FXMLLoader loader = new FXMLLoader(getClass().getResource("EventHandlerDemo.fxml"));
19 | AnchorPane page = (AnchorPane)loader.load();
20 | Scene scene = new Scene(page);
21 | primaryStage.setScene(scene);
22 | primaryStage.show();
23 | } catch (IOException e) {
24 | System.err.println("Error loading EventHandlerDemo.fxml!");
25 | e.printStackTrace();
26 | }
27 | }
28 |
29 | public static void main(String[] args) {
30 | launch(args);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/ex/Person.java:
--------------------------------------------------------------------------------
1 | package ex;
2 |
3 | public class Person {
4 | private String firstName;
5 | private String lastName;
6 |
7 | public Person(String firstName, String lastName) {
8 | this.firstName = firstName;
9 | this.lastName = lastName;
10 | }
11 |
12 | public String getFirstName() {
13 | return firstName;
14 | }
15 |
16 | public void setFirstName(String firstName) {
17 | this.firstName = firstName;
18 | }
19 |
20 | public String getLastName() {
21 | return lastName;
22 | }
23 |
24 | public void setLastName(String lastName) {
25 | this.lastName = lastName;
26 | }
27 |
28 | @Override
29 | public String toString() {
30 | return firstName + " " + lastName;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/ex/ScrollBarPane.java:
--------------------------------------------------------------------------------
1 | package ex;
2 |
3 | import javafx.application.Application;
4 | import javafx.scene.Scene;
5 | import javafx.scene.control.TextArea;
6 | import javafx.scene.layout.AnchorPane;
7 | import javafx.stage.Stage;
8 |
9 | public class ScrollBarPane extends Application {
10 |
11 | @Override
12 | public void start(Stage primaryStage) {
13 | AnchorPane pane = new AnchorPane();
14 | Scene frame = new Scene(pane, 300, 300);
15 | primaryStage.setScene(frame);
16 |
17 | TextArea ta = new TextArea("fdsgthtyjyt");
18 | ta.setMaxSize(150, 150);
19 | ta.setWrapText(true);
20 | pane.getChildren().addAll(ta);
21 |
22 | primaryStage.show();
23 |
24 |
25 | }
26 |
27 | public static void main(String[] args) {
28 | launch(args);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/game/BallGame.java:
--------------------------------------------------------------------------------
1 | package game;
2 |
3 | import java.io.IOException;
4 |
5 | import javafx.application.Application;
6 | import javafx.fxml.FXMLLoader;
7 | import javafx.scene.Parent;
8 | import javafx.scene.Scene;
9 | import javafx.scene.SceneBuilder;
10 | import javafx.scene.image.Image;
11 | import javafx.scene.paint.Color;
12 | import javafx.stage.Stage;
13 |
14 | public class BallGame extends Application {
15 | private static String VIEW_GAME = "/resources/GameView.fxml";
16 | private static String STYLESHEET_FILE = "/resources/style.css";
17 | public static Image ICON = new Image(BallGame.class.getResourceAsStream("/resources/head.png"));
18 |
19 | @Override
20 | public void start(Stage stage) throws Exception {
21 | initGui(stage);
22 | }
23 |
24 | private void initGui(Stage stage) throws IOException {
25 | Parent root = FXMLLoader.load(getClass().getResource(VIEW_GAME));
26 | Scene scene = SceneBuilder.create().root(root).width(500).height(530)
27 | .fill(Color.GRAY).build();
28 | scene.getStylesheets().add(STYLESHEET_FILE);
29 | stage.setScene(scene);
30 | stage.setTitle("hasCo de.com - Java FX 2 Ball Game Tutorial");
31 | stage.getIcons().add(ICON);
32 | stage.show();
33 | }
34 |
35 | public static void main(String... args) {
36 | launch(args);
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/src/game/BallGameController.java:
--------------------------------------------------------------------------------
1 | package game;
2 |
3 | import java.net.URL;
4 | import java.util.ResourceBundle;
5 |
6 | import javafx.animation.KeyFrame;
7 | import javafx.animation.Timeline;
8 | import javafx.animation.TimelineBuilder;
9 | import javafx.application.Platform;
10 | import javafx.beans.binding.Bindings;
11 | import javafx.event.ActionEvent;
12 | import javafx.event.EventHandler;
13 | import javafx.fxml.FXML;
14 | import javafx.fxml.Initializable;
15 | import javafx.scene.Group;
16 | import javafx.scene.control.Button;
17 | import javafx.scene.control.Label;
18 | import javafx.scene.control.ProgressBar;
19 | import javafx.scene.image.ImageView;
20 | import javafx.scene.image.ImageViewBuilder;
21 | import javafx.scene.input.MouseEvent;
22 | import javafx.scene.shape.Circle;
23 | import javafx.scene.shape.Rectangle;
24 | import javafx.scene.text.Text;
25 | import javafx.util.Duration;
26 |
27 | public class BallGameController implements Initializable {
28 | // UI ELEMENTS
29 | @FXML
30 | private Group area;
31 |
32 | @FXML
33 | private Circle ball;
34 |
35 | @FXML
36 | private Rectangle borderTop;
37 |
38 | @FXML
39 | private Rectangle borderBottom;
40 |
41 | @FXML
42 | private Rectangle borderLeft;
43 |
44 | @FXML
45 | private Rectangle borderRight;
46 |
47 | @FXML
48 | private Rectangle paddle;
49 |
50 | @FXML
51 | private Text gameOverText;
52 |
53 | @FXML
54 | private Text winnerText;
55 |
56 | @FXML
57 | private Button startButton;
58 |
59 | @FXML
60 | private Button quitButton;
61 |
62 | @FXML
63 | private ProgressBar progressBar;
64 |
65 | @FXML
66 | private Label remainingBlocksLabel;
67 |
68 | // GAME MODEL
69 | private final GameModel model = new GameModel();
70 |
71 | // GAME HEARTBEAT
72 | private final EventHandler pulseEvent = new EventHandler() {
73 | @Override
74 | public void handle(final ActionEvent evt) {
75 | checkWin();
76 | checkCollisions();
77 | updateBallPosition();
78 | }
79 | };
80 |
81 | // THE TIMELINE, RUNS EVERY 10MS
82 | private final Timeline heartbeat = TimelineBuilder.create()
83 | .keyFrames(new KeyFrame(new Duration(10.0), pulseEvent))
84 | .cycleCount(Timeline.INDEFINITE).build();
85 |
86 | /*
87 | * (non-Javadoc)
88 | *
89 | * @see javafx.fxml.Initializable#initialize(java.net.URL,
90 | * java.util.ResourceBundle)
91 | */
92 | @Override
93 | public void initialize(final URL url, final ResourceBundle bundle) {
94 | bindPaddleMouseEvents();
95 | bindStartButtonEvents();
96 | bindQuitButtonEvents();
97 | bindElementsToModel();
98 | initializeBoxes();
99 | initializeGame();
100 | area.requestFocus();
101 | }
102 |
103 | /**
104 | * binds ui elements to model state
105 | */
106 | private void bindElementsToModel() {
107 | startButton.disableProperty().bind(model.getGameStopped().not());
108 | ball.centerXProperty().bind(model.getBallX());
109 | ball.centerYProperty().bind(model.getBallY());
110 | paddle.xProperty().bind(model.getPaddleX());
111 | gameOverText.visibleProperty().bind(model.getGameLost());
112 | winnerText.visibleProperty().bind(model.getGameWon());
113 | progressBar.progressProperty().bind(
114 | model.getBoxesLeft().subtract(model.getInitialAmountBlocks())
115 | .multiply(-1).divide(model.getInitialAmountBlocks()));
116 | remainingBlocksLabel.textProperty().bind(
117 | Bindings.format("%.0f boxes left", model.getBoxesLeft()));
118 | }
119 |
120 | /**
121 | * initializes the game, is called for every new game
122 | */
123 | private void initializeGame() {
124 | model.reset();
125 | }
126 |
127 | /**
128 | * initializes the boxes.
129 | */
130 | private void initializeBoxes() {
131 | int startX = 15;
132 | int startY = 30;
133 | for (int v = 1; v <= model.getInitialBlocksVertical(); v++) {
134 | for (int h = 1; h <= model.getInitialBlocksHorizontal(); h++) {
135 | int x = startX + (h * 40);
136 | int y = startY + (v * 40);
137 | ImageView imageView = ImageViewBuilder.create()
138 | .image(BallGame.ICON).layoutX(x).layoutY(y).build();
139 | model.getBoxes().add(imageView);
140 | }
141 | }
142 | area.getChildren().addAll(model.getBoxes());
143 | }
144 |
145 | /**
146 | * creates event handler for the quit button. pressing it immediatly quits
147 | * the application.
148 | */
149 | private void bindQuitButtonEvents() {
150 | quitButton.setOnAction(new EventHandler() {
151 | @Override
152 | public void handle(final ActionEvent evt) {
153 | Platform.exit();
154 | }
155 | });
156 | }
157 |
158 | /**
159 | * binds events to the start button. by pressing the start button, the game
160 | * is initialized and the timeline execution is started.
161 | */
162 | private void bindStartButtonEvents() {
163 | startButton.setOnAction(new EventHandler() {
164 | @Override
165 | public void handle(final ActionEvent evt) {
166 | initializeGame();
167 | model.getGameStopped().set(false);
168 | heartbeat.playFromStart();
169 | }
170 | });
171 | }
172 |
173 | /**
174 | * binds events to drag the paddle using the mouse.
175 | */
176 | private void bindPaddleMouseEvents() {
177 | paddle.setOnMousePressed(new EventHandler() {
178 | @Override
179 | public void handle(final MouseEvent evt) {
180 | model.setPaddleTranslateX(model.getPaddleTranslateX() + 150);
181 | model.setPaddleDragX(evt.getSceneX());
182 | }
183 | });
184 | paddle.setOnMouseDragged(new EventHandler() {
185 | @Override
186 | public void handle(final MouseEvent evt) {
187 | if (!model.getGameStopped().get()) {
188 | double x = model.getPaddleTranslateX() + evt.getSceneX()
189 | - model.getPaddleDragX();
190 | model.getPaddleX().setValue(x);
191 | }
192 | }
193 | });
194 | }
195 |
196 | /**
197 | * checks if the game is won.
198 | */
199 | private void checkWin() {
200 | if (0 == model.getBoxesLeft().get()) {
201 | model.getGameWon().set(true);
202 | model.getGameStopped().set(true);
203 | heartbeat.stop();
204 | }
205 | }
206 |
207 | /**
208 | * checks if the ball has collisions with the walls or the paddle.
209 | */
210 | private void checkCollisions() {
211 | checkBoxCollisions();
212 | if (ball.intersects(paddle.getBoundsInLocal())) {
213 | model.incrementSpeed();
214 | model.setMovingDown(false);
215 | }
216 | if (ball.intersects(borderTop.getBoundsInLocal())) {
217 | model.incrementSpeed();
218 | model.setMovingDown(true);
219 | }
220 | if (ball.intersects(borderBottom.getBoundsInLocal())) {
221 | model.getGameStopped().set(true);
222 | model.getGameLost().set(true);
223 | heartbeat.stop();
224 | }
225 | if (ball.intersects(borderLeft.getBoundsInLocal())) {
226 | model.incrementSpeed();
227 | model.setMovingRight(true);
228 | }
229 | if (ball.intersects(borderRight.getBoundsInLocal())) {
230 | model.incrementSpeed();
231 | model.setMovingRight(false);
232 | }
233 | if (paddle.intersects(borderRight.getBoundsInLocal())) {
234 | model.getPaddleX().set(350);
235 | }
236 | if (paddle.intersects(borderLeft.getBoundsInLocal())) {
237 | model.getPaddleX().set(0);
238 | }
239 | }
240 |
241 | /**
242 | * checks if the ball collides with one or more of the boxes. if there's a
243 | * collision, the box is removed.
244 | */
245 | private void checkBoxCollisions() {
246 | for (ImageView r : model.getBoxes()) {
247 | if (r.isVisible() && ball.intersects(r.getBoundsInParent())) {
248 | model.getBoxesLeft().set(model.getBoxesLeft().get() - 1);
249 | r.setVisible(false);
250 | }
251 | }
252 | }
253 |
254 | /**
255 | * updates the ball position by calculating the ball's speed, position and
256 | * direction.
257 | */
258 | private void updateBallPosition() {
259 | double x = model.isMovingRight() ? model.getMovingSpeed() : -model
260 | .getMovingSpeed();
261 | double y = model.isMovingDown() ? model.getMovingSpeed() : -model
262 | .getMovingSpeed();
263 | model.getBallX().set(model.getBallX().get() + x);
264 | model.getBallY().set(model.getBallY().get() + y);
265 | }
266 | }
267 |
--------------------------------------------------------------------------------
/src/game/GameModel.java:
--------------------------------------------------------------------------------
1 | package game;
2 |
3 | import javafx.beans.property.BooleanProperty;
4 | import javafx.beans.property.DoubleProperty;
5 | import javafx.beans.property.SimpleBooleanProperty;
6 | import javafx.beans.property.SimpleDoubleProperty;
7 | import javafx.collections.FXCollections;
8 | import javafx.collections.ObservableList;
9 | import javafx.scene.image.ImageView;
10 |
11 | public class GameModel {
12 | // amount of horizontal blocks.
13 | private final int INITIAL_BLOCKS_HORIZONTAL = 10;
14 |
15 | // amount of vertical blocks.
16 | private final int INITIAL_BLOCKS_VERTICAL = 5;
17 |
18 | // amount of blocks total = vertical * horizontal
19 | private final int INITIAL_AMOUNT_BLOCKS = getInitialBlocksHorizontal()
20 | * getInitialBlocksVertical();
21 |
22 | // coordinates of the ball
23 | private final DoubleProperty ballX = new SimpleDoubleProperty();
24 | private final DoubleProperty ballY = new SimpleDoubleProperty();
25 |
26 | // x coordinate of the paddle
27 | private final DoubleProperty paddleX = new SimpleDoubleProperty();
28 |
29 | // game is stopped?
30 | private final BooleanProperty gameStopped = new SimpleBooleanProperty();
31 |
32 | // game is lost?
33 | private final BooleanProperty gameLost = new SimpleBooleanProperty(false);
34 |
35 | // game is won?
36 | private final BooleanProperty gameWon = new SimpleBooleanProperty(false);
37 |
38 | // amount of boxes left
39 | private final DoubleProperty boxesLeft = new SimpleDoubleProperty(
40 | getInitialAmountBlocks());
41 |
42 | // ball is moving in direction: down?
43 | private boolean movingDown = true;
44 |
45 | // ball is moving in direction: right?
46 | private boolean movingRight = true;
47 |
48 | // ball moving speed
49 | private double movingSpeed = 1.0;
50 |
51 | // paddle drag/translate x
52 | private double paddleDragX = 0.0;
53 | private double paddleTranslateX = 0.0;
54 |
55 | // a collection of image elements
56 | private final ObservableList boxes = FXCollections
57 | .observableArrayList();
58 |
59 | public double getPaddleTranslateX() {
60 | return paddleTranslateX;
61 | }
62 |
63 | public double getPaddleDragX() {
64 | return paddleDragX;
65 | }
66 |
67 | public BooleanProperty getGameStopped() {
68 | return gameStopped;
69 | }
70 |
71 | public void setPaddleTranslateX(final double d) {
72 | this.paddleTranslateX = d;
73 |
74 | }
75 |
76 | public void setPaddleDragX(final double d) {
77 | this.paddleDragX = d;
78 | }
79 |
80 | public DoubleProperty getPaddleX() {
81 | return paddleX;
82 | }
83 |
84 | public int getInitialBlocksVertical() {
85 | return INITIAL_BLOCKS_VERTICAL;
86 | }
87 |
88 | public int getInitialBlocksHorizontal() {
89 | return INITIAL_BLOCKS_HORIZONTAL;
90 | }
91 |
92 | public ObservableList getBoxes() {
93 | return boxes;
94 | }
95 |
96 | public void reset() {
97 | getBoxesLeft().set(getInitialAmountBlocks());
98 | for (ImageView r : boxes) {
99 | r.setVisible(true);
100 | }
101 | setMovingSpeed(1.0);
102 | setMovingDown(true);
103 | setMovingRight(true);
104 | getBallX().setValue(250);
105 | getBallY().setValue(350);
106 | paddleX.setValue(175);
107 | gameStopped.set(true);
108 | getGameLost().set(false);
109 | getGameWon().set(false);
110 | setPaddleDragX(0.);
111 | setPaddleTranslateX(0.);
112 | }
113 |
114 | public DoubleProperty getBallX() {
115 | return ballX;
116 | }
117 |
118 | public DoubleProperty getBallY() {
119 | return ballY;
120 | }
121 |
122 | public BooleanProperty getGameLost() {
123 | return gameLost;
124 | }
125 |
126 | public BooleanProperty getGameWon() {
127 | return gameWon;
128 | }
129 |
130 | public DoubleProperty getBoxesLeft() {
131 | return boxesLeft;
132 | }
133 |
134 | public int getInitialAmountBlocks() {
135 | return INITIAL_AMOUNT_BLOCKS;
136 | }
137 |
138 | public void incrementSpeed() {
139 | if (getMovingSpeed() <= 6)
140 | setMovingSpeed(getMovingSpeed() + getMovingSpeed() * 0.5);
141 | }
142 |
143 | public boolean isMovingDown() {
144 | return movingDown;
145 | }
146 |
147 | public void setMovingDown(final boolean movingDown) {
148 | this.movingDown = movingDown;
149 | }
150 |
151 | public boolean isMovingRight() {
152 | return movingRight;
153 | }
154 |
155 | public void setMovingRight(final boolean movingRight) {
156 | this.movingRight = movingRight;
157 | }
158 |
159 | public double getMovingSpeed() {
160 | return movingSpeed;
161 | }
162 |
163 | public void setMovingSpeed(final double movingSpeed) {
164 | this.movingSpeed = movingSpeed;
165 | }
166 |
167 | }
168 |
--------------------------------------------------------------------------------
/src/game/HideShowApp.java:
--------------------------------------------------------------------------------
1 | package game;
2 |
3 | import javafx.application.Application;
4 | import javafx.event.ActionEvent;
5 | import javafx.event.EventHandler;
6 | import javafx.scene.Scene;
7 | import javafx.scene.control.Button;
8 | import javafx.scene.control.Label;
9 | import javafx.scene.layout.HBox;
10 | import javafx.stage.Stage;
11 |
12 | public class HideShowApp extends Application {
13 | public static void main(String[] args) {
14 | launch(args);
15 | }
16 |
17 | @Override
18 | public void start(Stage stage) throws Exception {
19 | final Stage window = new Stage();
20 | window.setX(10);
21 | Scene innerScene = new Scene(new Label("inner window"));
22 | window.setScene(innerScene);
23 |
24 | HBox root = new HBox(10d);
25 | Button showButton = new Button("show");
26 | showButton.setOnAction(new EventHandler() {
27 | @Override
28 | public void handle(ActionEvent event) {
29 | window.show();
30 | }
31 | });
32 | Button hideButton = new Button("hide");
33 | hideButton.setOnAction(new EventHandler() {
34 | @Override
35 | public void handle(ActionEvent event) {
36 | window.hide();
37 | }
38 | });
39 | root.getChildren().add(showButton);
40 | root.getChildren().add(hideButton);
41 | stage.setScene(new Scene(root));
42 | stage.show();
43 | }
44 | }
--------------------------------------------------------------------------------
/src/game/SingleClassNoXmlBallGame.java:
--------------------------------------------------------------------------------
1 | package game;
2 |
3 | import javafx.animation.KeyFrame;
4 | import javafx.animation.Timeline;
5 | import javafx.animation.TimelineBuilder;
6 | import javafx.application.Application;
7 | import javafx.application.Platform;
8 | import javafx.beans.binding.Bindings;
9 | import javafx.beans.property.BooleanProperty;
10 | import javafx.beans.property.DoubleProperty;
11 | import javafx.beans.property.SimpleBooleanProperty;
12 | import javafx.beans.property.SimpleDoubleProperty;
13 | import javafx.collections.FXCollections;
14 | import javafx.collections.ObservableList;
15 | import javafx.event.ActionEvent;
16 | import javafx.event.EventHandler;
17 | import javafx.scene.Cursor;
18 | import javafx.scene.Group;
19 | import javafx.scene.GroupBuilder;
20 | import javafx.scene.Scene;
21 | import javafx.scene.SceneBuilder;
22 | import javafx.scene.control.Button;
23 | import javafx.scene.control.ButtonBuilder;
24 | import javafx.scene.control.Hyperlink;
25 | import javafx.scene.control.HyperlinkBuilder;
26 | import javafx.scene.control.Label;
27 | import javafx.scene.control.LabelBuilder;
28 | import javafx.scene.control.ProgressBar;
29 | import javafx.scene.control.ProgressBarBuilder;
30 | import javafx.scene.control.ToolBar;
31 | import javafx.scene.control.ToolBarBuilder;
32 | import javafx.scene.effect.DropShadow;
33 | import javafx.scene.effect.DropShadowBuilder;
34 | import javafx.scene.image.Image;
35 | import javafx.scene.image.ImageView;
36 | import javafx.scene.image.ImageViewBuilder;
37 | import javafx.scene.input.MouseEvent;
38 | import javafx.scene.paint.Color;
39 | import javafx.scene.shape.Circle;
40 | import javafx.scene.shape.CircleBuilder;
41 | import javafx.scene.shape.Rectangle;
42 | import javafx.scene.shape.RectangleBuilder;
43 | import javafx.scene.text.Font;
44 | import javafx.scene.text.Text;
45 | import javafx.scene.text.TextBuilder;
46 | import javafx.stage.Stage;
47 | import javafx.util.Duration;
48 |
49 | public class SingleClassNoXmlBallGame extends Application {
50 | private static final String STYLESHEET_FILE = "/stylesheet/style.css";
51 | private static final int INITIAL_BLOCKS_HORIZONTAL = 10;
52 | private static final int INITIAL_BLOCKS_VERTICAL = 5;
53 | private static final int INITIAL_AMOUNT_BLOCKS = INITIAL_BLOCKS_HORIZONTAL
54 | * INITIAL_BLOCKS_VERTICAL;
55 |
56 | private final DoubleProperty ballX = new SimpleDoubleProperty();
57 | private final DoubleProperty ballY = new SimpleDoubleProperty();
58 | private final DoubleProperty paddleX = new SimpleDoubleProperty();
59 | private final BooleanProperty gameStopped = new SimpleBooleanProperty();
60 | private final BooleanProperty gameLost = new SimpleBooleanProperty(false);
61 | private final BooleanProperty gameWon = new SimpleBooleanProperty(false);
62 | private final DoubleProperty boxesLeft = new SimpleDoubleProperty(
63 | INITIAL_AMOUNT_BLOCKS);
64 |
65 | private boolean movingDown = true;
66 | private boolean movingRight = true;
67 | private double movingSpeed = 1.0;
68 | private double paddleDragX = 0.0;
69 | private double paddleTranslateX = 0.0;
70 |
71 | private static final Image ICON = new Image(
72 | SingleClassNoXmlBallGame.class
73 | .getResourceAsStream("/image/head.png"));
74 |
75 | private final DropShadow dropshadowEffect = DropShadowBuilder.create()
76 | .offsetY(4.0).offsetX(0.5).color(Color.BLACK).build();
77 |
78 | private final ObservableList boxes = FXCollections
79 | .observableArrayList();
80 |
81 | private final Circle ball = CircleBuilder.create().radius(10.0)
82 | .fill(Color.BLACK).effect(dropshadowEffect).build();
83 |
84 | private final Rectangle borderTop = RectangleBuilder.create().x(0).y(30)
85 | .width(500).height(2).effect(dropshadowEffect).build();
86 |
87 | private final Rectangle borderBottom = RectangleBuilder.create().x(0)
88 | .y(500).width(500).height(2).build();
89 |
90 | private final Rectangle borderLeft = RectangleBuilder.create().x(0).y(0)
91 | .width(2).height(500).build();
92 |
93 | private final Rectangle borderRight = RectangleBuilder.create().x(498).y(0)
94 | .width(2).height(500).build();
95 |
96 | private final Rectangle paddle = RectangleBuilder.create().x(200).y(460)
97 | .width(100).layoutX(20).height(15).effect(dropshadowEffect)
98 | .fill(Color.BLACK).cursor(Cursor.HAND)
99 | .onMousePressed(new EventHandler() {
100 | @Override
101 | public void handle(final MouseEvent evt) {
102 | paddleTranslateX = paddle.getTranslateX() + 150;
103 | paddleDragX = evt.getSceneX();
104 | }
105 | }).onMouseDragged(new EventHandler() {
106 | @Override
107 | public void handle(final MouseEvent evt) {
108 | if (!gameStopped.get()) {
109 | double x = paddleTranslateX + evt.getSceneX()
110 | - paddleDragX;
111 | paddleX.setValue(x);
112 | }
113 | }
114 | }).build();
115 |
116 | private final Text gameOverText = TextBuilder.create().text("Game Over")
117 | .font(Font.font("Arial", 40.0)).fill(Color.RED).layoutX(150)
118 | .layoutY(330).effect(dropshadowEffect).build();
119 |
120 | private final Text winnerText = TextBuilder.create().text("You've won!")
121 | .font(Font.font("Arial", 40.0)).fill(Color.GREEN).layoutX(150)
122 | .layoutY(330).effect(dropshadowEffect).build();
123 |
124 | private final Button startButton = ButtonBuilder.create().text("Start")
125 | .onAction(new EventHandler() {
126 | @Override
127 | public void handle(final ActionEvent evt) {
128 | initGame();
129 | gameStopped.set(false);
130 | heartbeat.playFromStart();
131 | }
132 | }).build();
133 |
134 | private final Button quitButton = ButtonBuilder.create().text("Quit")
135 | .onAction(new EventHandler() {
136 | @Override
137 | public void handle(final ActionEvent evt) {
138 | Platform.exit();
139 | }
140 | }).build();
141 |
142 | private final Hyperlink link = HyperlinkBuilder.create()
143 | .text("www.hascode.com").layoutX(360).layoutY(505).build();
144 |
145 | private final ProgressBar progressBar = ProgressBarBuilder.create()
146 | .progress(100).build();
147 |
148 | private final Label remainingBlocksLabel = LabelBuilder.create().build();
149 |
150 | private final ToolBar toolbar = ToolBarBuilder.create().minWidth(500)
151 | .items(startButton, quitButton, progressBar, remainingBlocksLabel)
152 | .build();
153 |
154 | private final ToolBar footerBar = ToolBarBuilder.create().minWidth(500)
155 | .items(link).layoutY(500).build();
156 |
157 | private final Group area = GroupBuilder
158 | .create()
159 | .focusTraversable(true)
160 | .children(ball, borderTop, borderBottom, borderLeft, borderRight,
161 | paddle, gameOverText, winnerText, toolbar, footerBar)
162 | .build();
163 |
164 | private final EventHandler pulseEvent = new EventHandler() {
165 | @Override
166 | public void handle(final ActionEvent evt) {
167 | checkWin();
168 | checkCollisions();
169 | double x = movingRight ? movingSpeed : -movingSpeed;
170 | double y = movingDown ? movingSpeed : -movingSpeed;
171 | ballX.set(ballX.get() + x);
172 | ballY.set(ballY.get() + y);
173 | }
174 | };
175 |
176 | private final Timeline heartbeat = TimelineBuilder.create()
177 | .keyFrames(new KeyFrame(new Duration(10.0), pulseEvent))
178 | .cycleCount(Timeline.INDEFINITE).build();
179 |
180 | @Override
181 | public void start(final Stage stage) throws Exception {
182 | initGui(stage);
183 | initGame();
184 | }
185 |
186 | protected void checkWin() {
187 | if (0 == boxesLeft.get()) {
188 | gameWon.set(true);
189 | gameStopped.set(true);
190 | heartbeat.stop();
191 | }
192 | }
193 |
194 | protected void checkCollisions() {
195 | checkBoxCollisions();
196 | if (ball.intersects(paddle.getBoundsInLocal())) {
197 | incrementSpeed();
198 | movingDown = false;
199 | }
200 | if (ball.intersects(borderTop.getBoundsInLocal())) {
201 | incrementSpeed();
202 | movingDown = true;
203 | }
204 | if (ball.intersects(borderBottom.getBoundsInLocal())) {
205 | gameStopped.set(true);
206 | gameLost.set(true);
207 | heartbeat.stop();
208 | }
209 | if (ball.intersects(borderLeft.getBoundsInLocal())) {
210 | incrementSpeed();
211 | movingRight = true;
212 | }
213 | if (ball.intersects(borderRight.getBoundsInLocal())) {
214 | incrementSpeed();
215 | movingRight = false;
216 | }
217 | if (paddle.intersects(borderRight.getBoundsInLocal())) {
218 | paddleX.set(350);
219 | }
220 | if (paddle.intersects(borderLeft.getBoundsInLocal())) {
221 | paddleX.set(0);
222 | }
223 | }
224 |
225 | private void checkBoxCollisions() {
226 | for (ImageView r : boxes) {
227 | if (r.isVisible() && ball.intersects(r.getBoundsInParent())) {
228 | boxesLeft.set(boxesLeft.get() - 1);
229 | r.setVisible(false);
230 | }
231 | }
232 | }
233 |
234 | private void incrementSpeed() {
235 | if (movingSpeed <= 6)
236 | movingSpeed += movingSpeed * 0.5;
237 | }
238 |
239 | private void initGame() {
240 | boxesLeft.set(INITIAL_AMOUNT_BLOCKS);
241 | for (ImageView r : boxes) {
242 | r.setVisible(true);
243 | }
244 | movingSpeed = 1.0;
245 | movingDown = true;
246 | movingRight = true;
247 | ballX.setValue(250);
248 | ballY.setValue(350);
249 | paddleX.setValue(175);
250 | startButton.disableProperty().bind(gameStopped.not());
251 | ball.centerXProperty().bind(ballX);
252 | ball.centerYProperty().bind(ballY);
253 | paddle.xProperty().bind(paddleX);
254 | gameStopped.set(true);
255 | gameLost.set(false);
256 | gameOverText.visibleProperty().bind(gameLost);
257 | gameWon.set(false);
258 | winnerText.visibleProperty().bind(gameWon);
259 | area.requestFocus();
260 | progressBar.progressProperty().bind(
261 | boxesLeft.subtract(INITIAL_AMOUNT_BLOCKS).multiply(-1)
262 | .divide(INITIAL_AMOUNT_BLOCKS));
263 | remainingBlocksLabel.textProperty().bind(
264 | Bindings.format("%.0f boxes left", boxesLeft));
265 | }
266 |
267 | private void initBoxes() {
268 | int startX = 15;
269 | int startY = 30;
270 | for (int v = 1; v <= INITIAL_BLOCKS_VERTICAL; v++) {
271 | for (int h = 1; h <= INITIAL_BLOCKS_HORIZONTAL; h++) {
272 | int x = startX + (h * 40);
273 | int y = startY + (v * 40);
274 | ImageView imageView = ImageViewBuilder.create().image(ICON)
275 | .layoutX(x).layoutY(y).build();
276 | boxes.add(imageView);
277 | }
278 | }
279 | area.getChildren().addAll(boxes);
280 | }
281 |
282 | private void initGui(final Stage stage) {
283 | Scene scene = SceneBuilder.create().width(500).height(530)
284 | .fill(Color.GRAY).root(area).build();
285 | initBoxes();
286 | stage.setScene(scene);
287 | stage.setTitle("hasCode.com - Java FX 2 Ball Game Tutorial");
288 | stage.getIcons().add(ICON);
289 | scene.getStylesheets().add(STYLESHEET_FILE);
290 | stage.show();
291 | }
292 |
293 | public static void main(final String... args) {
294 | Application.launch(args);
295 | }
296 |
297 | }
298 |
--------------------------------------------------------------------------------
/src/james/AvcHmi.java:
--------------------------------------------------------------------------------
1 | package james;
2 |
3 | import james.DataSource;
4 |
5 | import javafx.application.Application;
6 | import javafx.application.Platform;
7 | import javafx.scene.Group;
8 | import javafx.scene.Scene;
9 | import javafx.scene.text.Text;
10 | import javafx.stage.Stage;
11 |
12 | public class AvcHmi extends Application {
13 | public static void main(String[] args) {
14 | launch(args);
15 | }
16 |
17 | @Override
18 | public void start(Stage primaryStage) {
19 | Text t = new Text(10, 50,
20 | "Replace/update this text periodically with data");
21 |
22 | Group root = new Group();
23 | root.getChildren().add(t);
24 |
25 | primaryStage.setScene(new Scene(root, 400, 300));
26 | primaryStage.show();
27 |
28 | new Thread() {
29 | private DataSource dataSource = new DataSource();
30 |
31 | {
32 | setDaemon(true);
33 | }
34 |
35 | @Override
36 | public void run() {
37 | try {
38 | for (;;) {
39 | Thread.sleep(100);
40 |
41 | Platform.runLater(new Runnable() {
42 | @Override
43 | public void run() {
44 | System.out.println(dataSource.getDataMap().get(
45 | "key1"));
46 | }
47 | });
48 | }
49 | } catch (InterruptedException e) {
50 | e.printStackTrace();
51 | }
52 | }
53 | }.start();
54 | }
55 | }
--------------------------------------------------------------------------------
/src/james/DataSource.java:
--------------------------------------------------------------------------------
1 | package james;
2 |
3 | import java.util.*;
4 |
5 | public class DataSource {
6 | Map dataMap = new HashMap<>();
7 |
8 | public DataSource() {
9 | dataMap.put("key1", "value1");
10 | dataMap.put("key2", "value2");
11 | dataMap.put("key3", "value3");
12 | }
13 |
14 | public Map getDataMap() {
15 | Random generator = new Random();
16 | int randInt = generator.nextInt();
17 | dataMap.put("key1", "value" + randInt);
18 | return dataMap;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/james/Indicators.java:
--------------------------------------------------------------------------------
1 | package james;
2 |
3 | import java.util.*;
4 | import javafx.application.*;
5 | import javafx.scene.*;
6 | import javafx.scene.control.*;
7 | import javafx.scene.layout.*;
8 | import javafx.stage.*;
9 |
10 | public class Indicators extends Application {
11 |
12 | public static void main(String[] args) {
13 | launch(args);
14 | }
15 |
16 | @Override
17 | public void start(Stage stage) {
18 | Pane root = new HBox();
19 | stage.setScene(new Scene(root, 300, 100));
20 |
21 | for (int i = 0; i < 10; i++) {
22 | final ProgressIndicator pi = new ProgressIndicator(0);
23 | root.getChildren().add(pi);
24 |
25 | // separate non-FX thread
26 | new Thread() {
27 |
28 | // runnable for that thread
29 | public void run() {
30 | for (int i = 0; i < 20; i++) {
31 | try {
32 | // imitating work
33 | Thread.sleep(new Random().nextInt(1000));
34 | } catch (InterruptedException ex) {
35 | ex.printStackTrace();
36 | }
37 | final double progress = i * 0.05;
38 | // update ProgressIndicator on FX thread
39 | Platform.runLater(new Runnable() {
40 |
41 | public void run() {
42 | pi.setProgress(progress);
43 | }
44 | });
45 | }
46 | }
47 | }.start();
48 | }
49 |
50 | stage.show();
51 |
52 | }
53 | }
--------------------------------------------------------------------------------
/src/james/Minimal.java:
--------------------------------------------------------------------------------
1 | package james;
2 |
3 | import java.util.Random;
4 |
5 | import javafx.application.Application;
6 | import javafx.application.Platform;
7 | import javafx.beans.property.SimpleDoubleProperty;
8 | import javafx.beans.value.ChangeListener;
9 | import javafx.beans.value.ObservableValue;
10 | import javafx.concurrent.Service;
11 | import javafx.concurrent.Task;
12 | import javafx.concurrent.Worker;
13 | import javafx.concurrent.Worker.State;
14 | import javafx.geometry.Insets;
15 | import javafx.scene.Group;
16 | import javafx.scene.Node;
17 | import javafx.scene.Scene;
18 | import javafx.scene.control.Label;
19 | import javafx.scene.control.ProgressBar;
20 | import javafx.scene.control.ProgressIndicator;
21 | import javafx.scene.layout.StackPane;
22 | import javafx.scene.layout.TilePane;
23 | import javafx.scene.paint.Color;
24 | import javafx.scene.shape.Rectangle;
25 | import javafx.stage.Stage;
26 |
27 | /*
28 | * http://stackoverflow.com/questions/9165251/execute-task-in-background-in-javafx
29 | */
30 | public class Minimal extends Application {
31 |
32 | private TilePane loadPane;
33 | private ProgressIndicator[] indicators = new ProgressIndicator[9];
34 | private Label loading[] = new Label[9];
35 | private Color[] colors = { Color.BLACK, Color.BLUE, Color.CRIMSON,
36 | Color.DARKCYAN, Color.FORESTGREEN, Color.GOLD, Color.HOTPINK,
37 | Color.INDIGO, Color.KHAKI };
38 | private int counter = 0;
39 |
40 | @Override
41 | public void start(Stage primaryStage) throws Exception {
42 | // creating Layout
43 | final Group root = new Group();
44 | Scene scene = new Scene(root, 400, 400);
45 | primaryStage.setScene(scene);
46 | primaryStage.setResizable(false);
47 | StackPane waitingPane = new StackPane();
48 | final ProgressBar progress = new ProgressBar();
49 | Label load = new Label("loading things...");
50 | progress.setTranslateY(-25);
51 | load.setTranslateY(25);
52 | waitingPane.getChildren().addAll(new Rectangle(400, 400, Color.WHITE),
53 | load, progress);
54 | root.getChildren().add(waitingPane);
55 |
56 | // Task for computing the Panels:
57 | Task task = new Task() {
58 | @Override
59 | protected Void call() throws Exception {
60 | for (int i = 0; i < 20; i++) {
61 | try {
62 | Thread.sleep(new Random().nextInt(1000));
63 | } catch (InterruptedException ex) {
64 | ex.printStackTrace();
65 | }
66 | final double prog = i * 0.05;
67 | Platform.runLater(new Runnable() {
68 | public void run() {
69 | progress.setProgress(prog);
70 | }
71 | });
72 | }
73 | return null;
74 | }
75 | };
76 |
77 | // stateProperty for Task:
78 | task.stateProperty().addListener(new ChangeListener() {
79 |
80 | @Override
81 | public void changed(ObservableValue extends State> observable,
82 | State oldValue, Worker.State newState) {
83 | if (newState == Worker.State.SUCCEEDED) {
84 | loadPanels(root);
85 | }
86 | }
87 | });
88 |
89 | // start Task
90 | new Thread(task).start();
91 |
92 | primaryStage.show();
93 | }
94 |
95 | private void loadPanels(Group root) {
96 | // change to loadPanel:
97 | root.getChildren().set(0, createLoadPane());
98 |
99 | // Service:
100 | final Service RecBuilder = new Service() {
101 | @Override
102 | protected Task createTask() {
103 | return new Task() {
104 | @Override
105 | protected Rectangle call() throws InterruptedException {
106 | updateMessage("loading rectangle . . .");
107 | updateProgress(0, 10);
108 | for (int i = 0; i < 10; i++) {
109 | Thread.sleep(100);
110 | }
111 | updateMessage("Finish!");
112 | return new Rectangle((380) / 3, (380) / 3,
113 | colors[counter]);
114 | }
115 | };
116 | }
117 | };
118 |
119 | // StateListener
120 | RecBuilder.stateProperty().addListener(
121 | new ChangeListener() {
122 | @Override
123 | public void changed(
124 | ObservableValue extends Worker.State> observableValue,
125 | Worker.State oldState, Worker.State newState) {
126 | switch (newState) {
127 | case SCHEDULED:
128 | break;
129 | case READY:
130 | case RUNNING:
131 | break;
132 | case SUCCEEDED:
133 | Rectangle rec = RecBuilder.valueProperty()
134 | .getValue();
135 | indicators[counter].progressProperty().unbind();
136 | loading[counter].textProperty().unbind();
137 | loadPane.getChildren().set(counter, rec);
138 | if (counter < 8) {
139 | counter++;
140 | nextPane(RecBuilder);
141 | }
142 | break;
143 | case CANCELLED:
144 | case FAILED:
145 | loading[counter].textProperty().unbind();
146 | loading[counter].setText("Failed!");
147 | if (counter < 8) {
148 | counter++;
149 | nextPane(RecBuilder);
150 | }
151 | break;
152 | }
153 | }
154 | });
155 |
156 | // begin PanelBuilding:
157 | nextPane(RecBuilder);
158 | }
159 |
160 | private void nextPane(Service recBuilder) {
161 | loading[counter].textProperty().bind(recBuilder.messageProperty());
162 | indicators[counter].visibleProperty().bind(
163 | recBuilder.progressProperty().isNotEqualTo(
164 | new SimpleDoubleProperty(
165 | ProgressBar.INDETERMINATE_PROGRESS)));
166 | recBuilder.restart();
167 | }
168 |
169 | private Node createLoadPane() {
170 | loadPane = new TilePane(5, 5);
171 | loadPane.setPrefColumns(3);
172 | loadPane.setPadding(new Insets(5));
173 | for (int i = 0; i < 9; i++) {
174 | StackPane waitingPane = new StackPane();
175 | Rectangle background = new Rectangle((380) / 3, (380) / 3,
176 | Color.WHITE);
177 | indicators[i] = new ProgressIndicator();
178 | indicators[i].setPrefSize(50, 50);
179 | indicators[i].setMaxSize(50, 50);
180 | indicators[i].setTranslateY(-25);
181 | indicators[i].setTranslateX(-10);
182 | loading[i] = new Label();
183 | loading[i].setTranslateY(25);
184 | waitingPane.getChildren().addAll(background, indicators[i],
185 | loading[i]);
186 | loadPane.getChildren().add(waitingPane);
187 | }
188 |
189 | return loadPane;
190 | }
191 |
192 | public static void main(String[] args) {
193 | launch(args);
194 | }
195 |
196 | }
--------------------------------------------------------------------------------
/src/james/PopupExample.java:
--------------------------------------------------------------------------------
1 | package james;
2 |
3 | import javafx.application.*;
4 | import javafx.event.*;
5 | import javafx.scene.*;
6 | import javafx.scene.control.*;
7 | import javafx.scene.layout.*;
8 | import javafx.scene.paint.*;
9 | import javafx.scene.shape.*;
10 | import javafx.scene.text.*;
11 | import javafx.stage.*;
12 |
13 | public class PopupExample extends Application {
14 | public static void main(String[] args) {
15 | launch(args);
16 | }
17 |
18 | @Override
19 | public void start(final Stage primaryStage) {
20 | primaryStage.setTitle("Popup Example");
21 | final Popup popup = new Popup();
22 | popup.setX(300);
23 | popup.setY(200);
24 | popup.getContent().addAll(new Circle(25, 25, 50, Color.AQUAMARINE));
25 |
26 | Button show = new Button("Show");
27 | show.setOnAction(new EventHandler() {
28 | @Override
29 | public void handle(ActionEvent event) {
30 | popup.show(primaryStage);
31 | }
32 | });
33 |
34 | Button hide = new Button("Hide");
35 | hide.setOnAction(new EventHandler() {
36 | @Override
37 | public void handle(ActionEvent event) {
38 | popup.hide();
39 | }
40 | });
41 |
42 | HBox layout = new HBox(10);
43 | layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
44 | layout.getChildren().addAll(show, hide);
45 | primaryStage.setScene(new Scene(layout));
46 | primaryStage.show();
47 | }
48 | }
--------------------------------------------------------------------------------
/src/james/SecondStage.java:
--------------------------------------------------------------------------------
1 | package james;
2 |
3 | import javafx.application.Application;
4 | import javafx.beans.value.*;
5 | import javafx.event.EventHandler;
6 | import javafx.scene.*;
7 | import javafx.scene.control.*;
8 | import javafx.scene.layout.StackPane;
9 | import javafx.stage.*;
10 |
11 | /*
12 | * http://stackoverflow.com/questions/9997851/javafx-2-0-subwindow
13 | */
14 |
15 | public class SecondStage extends Application {
16 | public static void main(String[] args) {
17 | launch(args);
18 | }
19 |
20 | @Override
21 | public void start(Stage primaryStage) {
22 | // setup some dynamic data to display.
23 | final String STANDARD_TEXT = "Every Good Boy Deserves Fruit";
24 | final String ALTERNATE_TEXT = "Good Boys Deserve Fruit Always";
25 | final Label label = new Label(STANDARD_TEXT);
26 |
27 | // configure the primary stage.
28 | StackPane primaryLayout = new StackPane();
29 | primaryLayout.getChildren().add(label);
30 | primaryLayout
31 | .setStyle("-fx-background-color: lightgreen; -fx-padding: 10;");
32 | primaryStage.setScene(new Scene(primaryLayout, 200, 100));
33 | primaryStage.setTitle("Primary Stage");
34 |
35 | // configure the secondary stage.
36 | final Stage secondaryStage = new Stage(StageStyle.DECORATED);
37 | CheckBox alternateTextCheck = new CheckBox("Show alternate text");
38 | alternateTextCheck.selectedProperty().addListener(
39 | new ChangeListener() {
40 | @Override
41 | public void changed(
42 | ObservableValue extends Boolean> selected,
43 | Boolean oldValue, Boolean newValue) {
44 | if (newValue)
45 | label.setText(ALTERNATE_TEXT);
46 | else
47 | label.setText(STANDARD_TEXT);
48 | }
49 | });
50 | StackPane secondaryLayout = new StackPane();
51 | secondaryLayout.getChildren().add(alternateTextCheck);
52 | secondaryLayout
53 | .setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
54 | secondaryStage.setScene(new Scene(secondaryLayout, 200, 100));
55 | secondaryStage.setTitle("Secondary Stage");
56 |
57 | // specify stage locations.
58 | secondaryStage.setX(400);
59 | secondaryStage.setY(200);
60 | primaryStage.setX(400);
61 | primaryStage.setY(350);
62 |
63 | // add a trigger to hide the secondary stage when the primary stage is
64 | // hidden.
65 | // this will cause all stages to be hidden (which will cause the app to
66 | // terminate).
67 | primaryStage.setOnHidden(new EventHandler() {
68 | @Override
69 | public void handle(WindowEvent onClosing) {
70 | secondaryStage.hide();
71 | }
72 | });
73 |
74 | // show both stages.
75 | primaryStage.show();
76 | secondaryStage.show();
77 | }
78 | }
--------------------------------------------------------------------------------
/src/james/ServiceSample.java:
--------------------------------------------------------------------------------
1 | package james;
2 |
3 | import java.util.Random;
4 | import javafx.application.Application;
5 | import javafx.beans.value.*;
6 | import javafx.scene.Group;
7 | import javafx.scene.Scene;
8 | import javafx.stage.Stage;
9 | import javafx.collections.FXCollections;
10 | import javafx.collections.ObservableList;
11 | import javafx.concurrent.Service;
12 | import javafx.concurrent.Task;
13 | import javafx.event.ActionEvent;
14 | import javafx.event.EventHandler;
15 | import javafx.geometry.Insets;
16 | import javafx.scene.control.*;
17 | import javafx.scene.layout.Region;
18 | import javafx.scene.layout.StackPane;
19 | import javafx.scene.layout.VBox;
20 | import javafx.util.Callback;
21 |
22 | /**
23 | * A sample showing use of a Service to retrieve data in a background thread.
24 | * Selecting the Refresh button restarts the Service.
25 | * http://stackoverflow.com/questions
26 | * /13163947/how-to-update-a-set-of-javafx2-2-textfields-from-a-service
27 | *
28 | * @see javafx.collections.FXCollections
29 | * @see javafx.concurrent.Service
30 | * @see javafx.concurrent.Task
31 | * @see javafx.scene.control.ProgressIndicator
32 | * @see javafx.scene.control.TableColumn
33 | * @see javafx.scene.control.TableView
34 | */
35 | public class ServiceSample extends Application {
36 |
37 | final GetDailySalesService service = new GetDailySalesService();
38 | final Random random = new Random();
39 |
40 | private void init(Stage primaryStage) {
41 | Group root = new Group();
42 | primaryStage.setScene(new Scene(root));
43 |
44 | VBox vbox = new VBox(5);
45 | vbox.setPadding(new Insets(12));
46 | ListView listView = new ListView<>();
47 | listView.setEditable(true);
48 | Button button = new Button("Refresh");
49 | button.setOnAction(new EventHandler() {
50 | @Override
51 | public void handle(ActionEvent t) {
52 | service.restart();
53 | }
54 | });
55 | listView.setCellFactory(new Callback, ListCell>() {
56 | @Override
57 | public ListCell call(ListView param) {
58 | return new InstantEditingCell();
59 | }
60 | });
61 | listView.setPrefHeight(200);
62 | vbox.getChildren().addAll(listView, button);
63 |
64 | Region veil = new Region();
65 | veil.setStyle("-fx-background-color: rgba(0, 0, 0, 0.4)");
66 | ProgressIndicator p = new ProgressIndicator();
67 | p.setMaxSize(150, 150);
68 |
69 | p.progressProperty().bind(service.progressProperty());
70 | veil.visibleProperty().bind(service.runningProperty());
71 | p.visibleProperty().bind(service.runningProperty());
72 | listView.itemsProperty().bind(service.valueProperty());
73 |
74 | StackPane stack = new StackPane();
75 | stack.getChildren().addAll(vbox, veil, p);
76 |
77 | root.getChildren().add(stack);
78 | service.start();
79 | }
80 |
81 | /**
82 | * A service for getting the DailySales data. This service exposes an
83 | * ObservableList for convenience when using the service. This
84 | * results
list is final, though its contents are replaced when
85 | * a service call successfully concludes.
86 | */
87 | public class GetDailySalesService extends Service> {
88 |
89 | /**
90 | * Create and return the task for fetching the data. Note that this
91 | * method is called on the background thread (all other code in this
92 | * application is on the JavaFX Application Thread!).
93 | *
94 | * @return A task
95 | */
96 | @Override
97 | protected Task createTask() {
98 | return new GetDailySalesTask();
99 | }
100 | }
101 |
102 | public class GetDailySalesTask extends Task> {
103 | @Override
104 | protected ObservableList call() throws Exception {
105 | for (int i = 0; i < 500; i++) {
106 | updateProgress(i, 500);
107 | Thread.sleep(5);
108 | }
109 | ObservableList sales = FXCollections.observableArrayList();
110 | sales.add("A1: " + random.nextInt());
111 | sales.add("A2: " + random.nextInt());
112 | return sales;
113 | }
114 | }
115 |
116 | @Override
117 | public void start(Stage primaryStage) throws Exception {
118 | init(primaryStage);
119 | primaryStage.show();
120 | }
121 |
122 | public static void main(String[] args) {
123 | launch(args);
124 | }
125 |
126 | class InstantEditingCell extends ListCell {
127 | @Override
128 | public void updateItem(String item, boolean empty) {
129 | super.updateItem(item, empty);
130 |
131 | if (empty) {
132 | setGraphic(null);
133 | } else {
134 | final TextField textField = new TextField(getString());
135 | textField.setMinWidth(this.getWidth()
136 | - this.getGraphicTextGap() * 2);
137 | textField.focusedProperty().addListener(
138 | new ChangeListener() {
139 | @Override
140 | public void changed(
141 | ObservableValue extends Boolean> value,
142 | Boolean wasFocused, Boolean isFocused) {
143 | if (!isFocused) {
144 | commitEdit(textField.getText());
145 | }
146 | }
147 | });
148 | setGraphic(textField);
149 | }
150 | }
151 |
152 | private String getString() {
153 | return getItem() == null ? "" : getItem().toString();
154 | }
155 | }
156 |
157 | class ClickableEditingCell extends ListCell {
158 |
159 | private TextField textField;
160 |
161 | public ClickableEditingCell() {
162 | }
163 |
164 | @Override
165 | public void startEdit() {
166 | if (!isEmpty()) {
167 | super.startEdit();
168 | createTextField();
169 | setText(null);
170 | setGraphic(textField);
171 | textField.selectAll();
172 | }
173 | }
174 |
175 | @Override
176 | public void cancelEdit() {
177 | super.cancelEdit();
178 |
179 | setText((String) getItem());
180 | setGraphic(null);
181 | }
182 |
183 | @Override
184 | public void updateItem(String item, boolean empty) {
185 | super.updateItem(item, empty);
186 |
187 | if (empty) {
188 | setText(null);
189 | setGraphic(null);
190 | } else {
191 | if (isEditing()) {
192 | if (textField != null) {
193 | textField.setText(getString());
194 | }
195 | setText(null);
196 | setGraphic(textField);
197 | } else {
198 | setText(getString());
199 | setGraphic(null);
200 | }
201 | }
202 | }
203 |
204 | private void createTextField() {
205 | textField = new TextField(getString());
206 | textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()
207 | * 2);
208 | textField.focusedProperty().addListener(
209 | new ChangeListener() {
210 | @Override
211 | public void changed(
212 | ObservableValue extends Boolean> arg0,
213 | Boolean arg1, Boolean arg2) {
214 | if (!arg2) {
215 | commitEdit(textField.getText());
216 | }
217 | }
218 | });
219 | }
220 |
221 | private String getString() {
222 | return getItem() == null ? "" : getItem().toString();
223 | }
224 | }
225 |
226 | }
--------------------------------------------------------------------------------
/src/james/s.java:
--------------------------------------------------------------------------------
1 | package james;
2 |
3 | import javafx.animation.*;
4 | import javafx.application.*;
5 | import javafx.event.*;
6 | import javafx.stage.*;
7 | import javafx.util.*;
8 |
9 | /*
10 | * http://stackoverflow.com/questions/9966136/javafx-periodic-background-task?rq=1
11 | */
12 | public class s extends Application {
13 | public void start(Stage primaryStage) throws Exception {
14 | Timeline fiveSecondsWonder = new Timeline(new KeyFrame(
15 | Duration.seconds(5), new EventHandler() {
16 |
17 | @Override
18 | public void handle(ActionEvent event) {
19 | System.out
20 | .println("this is called every 5 seconds on UI thread");
21 | }
22 | }));
23 | fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);
24 | fiveSecondsWonder.play();
25 | }
26 |
27 | public static void main(String[] args) {
28 | launch(args);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/james/testScheduledExecutorService.java:
--------------------------------------------------------------------------------
1 | package james;
2 |
3 | import java.util.concurrent.Executors;
4 | import java.util.concurrent.ScheduledExecutorService;
5 | import java.util.concurrent.TimeUnit;
6 | import javafx.application.Application;
7 | import javafx.application.Platform;
8 | import javafx.concurrent.Task;
9 | import javafx.event.ActionEvent;
10 | import javafx.event.EventHandler;
11 | import javafx.scene.Scene;
12 | import javafx.scene.control.Button;
13 | import javafx.scene.layout.StackPane;
14 | import javafx.scene.layout.VBox;
15 | import javafx.scene.text.Text;
16 | import javafx.stage.Stage;
17 |
18 | /**
19 | *
20 | * @web http://java-buddy.blogspot.com/
21 | */
22 | public class testScheduledExecutorService extends Application {
23 |
24 | Text textCounter;
25 |
26 | /**
27 | * @param args
28 | * the command line arguments
29 | */
30 | public static void main(String[] args) {
31 | launch(args);
32 | }
33 |
34 | @Override
35 | public void start(Stage primaryStage) {
36 | Button btn = new Button();
37 | btn.setText("Start ScheduledExecutorService");
38 | btn.setOnAction(new EventHandler() {
39 | @Override
40 | public void handle(ActionEvent event) {
41 | startScheduledExecutorService();
42 | }
43 | });
44 |
45 | textCounter = new Text();
46 |
47 | VBox vBox = new VBox();
48 | vBox.getChildren().addAll(btn, textCounter);
49 |
50 | StackPane root = new StackPane();
51 | root.getChildren().add(vBox);
52 |
53 | Scene scene = new Scene(root, 300, 250);
54 |
55 | primaryStage.setTitle("java-buddy.blogspot.com");
56 | primaryStage.setScene(scene);
57 | primaryStage.show();
58 | }
59 |
60 | private void startScheduledExecutorService() {
61 |
62 | final ScheduledExecutorService scheduler = Executors
63 | .newScheduledThreadPool(1);
64 |
65 | scheduler.scheduleAtFixedRate(new Runnable() {
66 |
67 | int counter = 0;
68 |
69 | @Override
70 | public void run() {
71 | counter++;
72 | if (counter <= 10) {
73 |
74 | Platform.runLater(new Runnable() {
75 | @Override
76 | public void run() {
77 | textCounter.setText("isFxApplicationThread: "
78 | + Platform.isFxApplicationThread() + "\n"
79 | + "Counting: " + String.valueOf(counter));
80 | }
81 | });
82 |
83 | } else {
84 | scheduler.shutdown();
85 | Platform.runLater(new Runnable() {
86 | @Override
87 | public void run() {
88 | textCounter.setText("isFxApplicationThread: "
89 | + Platform.isFxApplicationThread() + "\n"
90 | + "-Finished-");
91 | }
92 | });
93 | }
94 |
95 | }
96 | }, 1, 1, TimeUnit.SECONDS);
97 | }
98 | }
--------------------------------------------------------------------------------
/src/malc/Main.java:
--------------------------------------------------------------------------------
1 | package malc;
2 |
3 | import java.io.IOException;
4 |
5 | import javafx.application.Application;
6 | import javafx.stage.Stage;
7 | import javafx.scene.Group;
8 | import javafx.scene.Scene;
9 | import javafx.scene.paint.Color;
10 | import javafx.scene.shape.Ellipse;
11 |
12 | public class Main extends Application {
13 |
14 | @Override
15 | public void start(Stage primaryStage) throws IOException {
16 | Group root = new Group();
17 | // describes the window itself: name, size
18 | primaryStage.setTitle(" Aufgabe 10 by John Malc ");
19 | primaryStage.setScene(new Scene(root));
20 | // say: center on screen, user can resize, and it will in general exists
21 | primaryStage.centerOnScreen();
22 | primaryStage.setResizable(true);
23 | primaryStage.show();
24 |
25 | // Ellipse alone
26 | Ellipse a = new Ellipse();
27 | a.setFill(Color.RED);
28 | a.setCenterX(205);
29 | a.setCenterY(150);
30 | a.setRadiusX(80);
31 | a.setRadiusY(30);
32 |
33 | // shows Ellipse and it will add it to the group
34 | root.getChildren().add(new Group(a));
35 | }
36 |
37 | public static void main(String[] args) {
38 | launch(args);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/malc/layout.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/src/malc2/Main.java:
--------------------------------------------------------------------------------
1 | package malc2;
2 |
3 | import java.io.IOException;
4 | import java.net.MalformedURLException;
5 |
6 | import javafx.application.Application;
7 | import javafx.event.ActionEvent;
8 | import javafx.event.EventHandler;
9 | import javafx.fxml.FXML;
10 | import javafx.fxml.FXMLLoader;
11 | import javafx.scene.Group;
12 | import javafx.scene.Parent;
13 | import javafx.scene.Scene;
14 | import javafx.scene.control.Button;
15 | import javafx.scene.control.ButtonBuilder;
16 | import javafx.scene.control.ListView;
17 | import javafx.scene.control.TextArea;
18 | import javafx.scene.image.Image;
19 | import javafx.scene.text.Text;
20 | import javafx.stage.Stage;
21 |
22 | public class Main extends Application {
23 | /*
24 | * Display text from a method, Whether a webpage is 404 - yes or not
25 | * (non-Javadoc) taken from
26 | * http://java-buddy.blogspot.de/2012/01/javafx-exercise
27 | * -display-text-on-scene.html
28 | *
29 | * @see javafx.application.Application#start(javafx.stage.Stage)
30 | */
31 |
32 | @Override
33 | public void start(Stage primaryStage) throws MalformedURLException,
34 | IOException {
35 |
36 | // icon
37 | Image icon = new Image("resources/icon.png");
38 | Parent root = FXMLLoader.load(getClass().getResource("UI.fxml"));
39 |
40 | TextArea textArea1;
41 | ListView list1;
42 |
43 |
44 | Methods cl = new Methods();
45 | String sit = "http://www.google.com/";
46 | // int ado = cl.getResponseCode(sit);
47 | String bobob = cl.getImp(cl.getResponseCode(sit)); // then ado
48 |
49 | Text t = new Text(10, 20, bobob);
50 | Group ab = new Group(root);
51 |
52 | Scene sc = new Scene(root, 600, 330);
53 |
54 | primaryStage.setScene(sc);
55 | primaryStage.setTitle("Maly Test");
56 | primaryStage.getIcons().add(icon);
57 | primaryStage.setResizable(true);
58 | primaryStage.centerOnScreen();
59 | primaryStage.show();
60 |
61 | }
62 | public static void main(String[] args) {
63 | launch(args);
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/src/malc2/Methods.java:
--------------------------------------------------------------------------------
1 | package malc2;
2 |
3 | import java.io.IOException;
4 | import java.net.HttpURLConnection;
5 | import java.net.MalformedURLException;
6 | import java.net.URL;
7 |
8 | public class Methods {
9 | // http://stackoverflow.com/questions/1378199/how-to-check-if-a-url-exists-or-returns-404-with-java
10 | public int getResponseCode(String sit) throws MalformedURLException,
11 | IOException {
12 | URL u = new URL(sit);
13 | HttpURLConnection huc = (HttpURLConnection) u.openConnection();
14 | huc.setRequestMethod("HEAD");
15 | huc.connect();
16 | return huc.getResponseCode();
17 | }
18 |
19 | public String getImp(int papa) {
20 | if (papa == 404) {
21 | return "wrong";
22 | } else {
23 | return "good";
24 | }
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/malc2/UI.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/malc2/UI.fxml.bak:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/malc3/DemoShowHide.java:
--------------------------------------------------------------------------------
1 | package malc3;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Parent;
6 | import javafx.scene.Scene;
7 | import javafx.stage.Stage;
8 |
9 | /**
10 | * http://www.e-zest.net/blog/javafx-2-x-development-using-fxml/
11 | * @author amol.hingmire
12 | */
13 | public class DemoShowHide extends Application {
14 |
15 | private Stage stage;
16 |
17 | public static void main(String[] args) {
18 | Application.launch(args);
19 | }
20 |
21 | @Override
22 | public void start(Stage mainStage) throws Exception {
23 | stage = mainStage;
24 | Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml")); // zde proste jenom zmenit na TextArea
25 | Scene scene = stage.getScene();
26 | if (scene == null) {
27 | scene = new Scene(root);
28 | stage.setScene(scene);
29 | } else {
30 | stage.getScene().setRoot(root);
31 | }
32 | stage.sizeToScene();
33 | stage.show();
34 | }
35 |
36 | public Stage getStage() {
37 | return stage;
38 | }
39 |
40 | }
--------------------------------------------------------------------------------
/src/malc3/Sample.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/src/malc3/Sample.java:
--------------------------------------------------------------------------------
1 | package malc3;
2 |
3 | import java.net.URL;
4 | import java.util.ResourceBundle;
5 |
6 | import javafx.event.ActionEvent;
7 | import javafx.fxml.FXML;
8 | import javafx.fxml.Initializable;
9 | import javafx.scene.control.TextArea;
10 | import javafx.scene.control.TreeView;
11 | import javafx.scene.layout.StackPane;
12 |
13 | /**
14 | *
15 | * @author amol.hingmire
16 | */
17 | public class Sample implements Initializable {
18 |
19 | @FXML
20 | private TreeView> tree;
21 | @FXML
22 | private TextArea area;
23 | @FXML
24 | private StackPane parentStack;
25 |
26 | @FXML
27 | private void loadTree(ActionEvent event) {
28 | tree.setVisible(true);
29 | area.setVisible(false);
30 | }
31 |
32 | @FXML
33 | private void loadTextArea(ActionEvent event) {
34 | area.setVisible(true);
35 | tree.setVisible(false);
36 | }
37 |
38 | @Override
39 | public void initialize(URL url, ResourceBundle rb) {
40 | area = (TextArea) parentStack.lookup("#textArea");
41 | }
42 | }
--------------------------------------------------------------------------------
/src/malc3/TextArea.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/src/resources/GameView.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/src/resources/head.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmpe/JavaFX/2b2c87f417866c7fa703f0e06927d972fc3a32a9/src/resources/head.png
--------------------------------------------------------------------------------
/src/resources/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmpe/JavaFX/2b2c87f417866c7fa703f0e06927d972fc3a32a9/src/resources/icon.png
--------------------------------------------------------------------------------
/src/resources/style.css:
--------------------------------------------------------------------------------
1 | .root {
2 | -fx-font-size: 16pt;
3 | -fx-font-family: "Arial";
4 | -fx-width: 500px;
5 | -fx-height: 530px;
6 | }
7 |
8 | #gameOverText,#winnerText {
9 | -fx-font-size: 40pt;
10 | -fx-effect: dropshadow(three-pass-box, rgba(0, 0, 0, 100), 1, 1, 4, 2 );
11 | }
12 |
13 | #paddle,#ball {
14 | -fx-effect: dropshadow(three-pass-box, #000000, 10.0,0.0,0.5,4.0);
15 | }
16 |
--------------------------------------------------------------------------------
/src/uka/Account.java:
--------------------------------------------------------------------------------
1 | package uka;
2 |
3 | public class Account {
4 | private int accountNumber;
5 | private double overdraft;
6 | private double bankDeposit;
7 |
8 | /**
9 | * This constructor allows a fast creation of accounts
10 | *
11 | * @param accountNumber
12 | * @param overdraft
13 | * @param bankDeposit
14 | */
15 |
16 | public Account(int accountNumber, double overdraft, double bankDeposit) {
17 | this.accountNumber = accountNumber;
18 | this.overdraft = overdraft;
19 | this.bankDeposit = bankDeposit;
20 | }
21 |
22 | /**
23 | * Saves the account number
24 | *
25 | * @param value
26 | * accountNumber
27 | */
28 | public void setAccountNumber(int value) {
29 | this.accountNumber = value;
30 | }
31 |
32 | /**
33 | *
34 | * @return accountNumber
35 | */
36 | public int getAccountNumber() {
37 | return accountNumber;
38 | }
39 |
40 | /**
41 | * Credit you could withdraw. You set a limit here
42 | *
43 | * @param value2
44 | * overdraft
45 | */
46 | public void setOverdraft(double value2) {
47 | this.overdraft = value2;
48 | }
49 |
50 | /**
51 | *
52 | * @return overdraft
53 | */
54 | public double getOverdraft() {
55 | return overdraft;
56 | }
57 |
58 | /**
59 | * Your initial balance in the account
60 | *
61 | * @param value3
62 | * bankDeposit
63 | */
64 | public void setBankDeposit(double value3) {
65 | this.bankDeposit = value3;
66 | }
67 |
68 | /**
69 | * @return bankDeposit
70 | */
71 | public double getBankDeposit() {
72 | return bankDeposit;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/uka/uka.java:
--------------------------------------------------------------------------------
1 | package uka;
2 |
3 | import java.util.*;
4 |
5 | import javafx.application.*;
6 | import javafx.event.*;
7 | import javafx.scene.*;
8 | import javafx.scene.control.*;
9 | import javafx.scene.layout.*;
10 | import javafx.stage.*;
11 |
12 | public class uka extends Application {
13 |
14 | @Override
15 | public void start(final Stage primary) {
16 | final Stage q = new Stage();
17 | final List i = new LinkedList();
18 |
19 | AnchorPane s = new AnchorPane();
20 | Scene p = new Scene(s);
21 |
22 | Button w = new Button("Display Account info");
23 | w.setOnAction(new EventHandler() {
24 |
25 | @Override
26 | public void handle(ActionEvent arg0) {
27 | GridPane s = new GridPane();
28 |
29 | Account a = new Account(50, 650, 60);
30 |
31 | i.add(a);
32 |
33 | TextArea o = new TextArea();
34 | o.setLayoutX(20);
35 | o.setLayoutY(20);
36 | o.appendText(a.getAccountNumber() + " " + a.getBankDeposit()
37 | + " " + a.getOverdraft());
38 |
39 | s.add(o, 1, 1);
40 | Scene d = new Scene(s);
41 | q.setHeight(200);
42 | q.setWidth(200);
43 | q.setScene(d);
44 | q.show();
45 | }
46 | });
47 |
48 | // add a trigger to hide the secondary stage when the primary stage is
49 | // hidden.
50 | // this will cause all stages to be hidden (which will cause the app to
51 | // terminate).
52 |
53 | q.setOnHidden(new EventHandler() {
54 | @Override
55 | public void handle(WindowEvent onClosing) {
56 | primary.hide();
57 | }
58 | });
59 |
60 | s.getChildren().add(w);
61 | primary.setScene(p);
62 | primary.show();
63 |
64 | }
65 |
66 | public static void main(String[] args) {
67 | launch(args);
68 | }
69 | }
70 |
--------------------------------------------------------------------------------