Welcome guys. Learn the JavaFX UI widgets using these code snippets.
Example 1: Gfx3d
Let us look at the code
Step 1: Create Project
- Open your favorite Java IDE.
- In the menu go to
File --> Create New Project
.
Step 2: Dependencies
No dependencies are needed for this project.
Step 3: Write Code
Our code will comprise the following java files:
Gfx3DExample.java
Gfx3DExampleDraft.java
Gfx3DExperiments.java
Gfx3DUtil.java
- In your editor or IDE, create a file known as
Gfx3DExample.java
. - Then add the following code:
(a). Gfx3DExample.java
We will need functionalities injected into this class via ready made classes. But first we have to import them. Let’s go ahead and do just that:
import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
The next step is to extend our class
as shown below:
public class Gfx3DExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
For you to be able to execute this class you need to assign it a main method using the following definition:
public static void main(String[] args) {
Here is the full code:
package com.jenkov.javafx.gfx3d;
import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
public class Gfx3DExample extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
boolean is3DSupported = Platform.isSupported(ConditionalFeature.SCENE3D);
if(!is3DSupported) {
System.out.println("Sorry, 3D is not supported in JavaFX on this platform.");
return;
}
Box box = new Box(100,100,100);
box.setCullFace(CullFace.NONE);
box.setTranslateX(250);
box.setTranslateY(100);
box.setTranslateZ(400);
boolean fixedEyeAtCameraZero = false;
PerspectiveCamera camera = new PerspectiveCamera(fixedEyeAtCameraZero);
camera.setTranslateX(150);
camera.setTranslateY(-100);
camera.setTranslateZ(250);
Group root = new Group(box);
root.setRotationAxis(Rotate.X_AXIS);
root.setRotate(30);
Scene scene = new Scene(root, 500, 300, true);
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.setTitle("3D Example");
primaryStage.show();
}
}
- Next create another file known as
Gfx3DExampleDraft.java
. - And add the following code:
(b). Gfx3DExampleDraft.java
So guys let;s go ahead and add some imports into our project. We add imports using the import
keyword:
import javafx.animation.Animation;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
The next step is to extend our class
as shown below:
public class Gfx3DExampleDraft extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
For you to be able to execute this class you need to assign it a main method using the following definition:
public static void main(String[] args) {
Here is the full code:
package com.jenkov.javafx.gfx3d;
import javafx.animation.Animation;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Gfx3DExampleDraft extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
boolean is3DSupported = Platform.isSupported(ConditionalFeature.SCENE3D);
if(!is3DSupported) {
System.out.println("Sorry, 3D is not supported in JavaFX on this platform.");
return;
}
Box box = new Box(100,100,100);
box.setCullFace(CullFace.NONE);
box.setTranslateX(450);
box.setTranslateY(100);
box.setTranslateZ(400);
boolean fixedEyeAtCameraZero = false;
PerspectiveCamera camera = new PerspectiveCamera(fixedEyeAtCameraZero);
camera.setTranslateX(100);
camera.setTranslateY(-75);
camera.setTranslateZ(300);
RotateTransition rotateTransition = new RotateTransition(Duration.seconds(2), camera);
rotateTransition.setCycleCount(Animation.INDEFINITE);
rotateTransition.setFromAngle(0);
rotateTransition.setToAngle(360);
rotateTransition.setAutoReverse(true);
rotateTransition.setAxis(Rotate.X_AXIS);
rotateTransition.play();
PointLight redLight = new PointLight();
redLight.setColor(Color.valueOf("#ff0000"));
redLight.setTranslateX(250);
redLight.setTranslateY(-100);
redLight.setTranslateZ(250);
PointLight greenLight = new PointLight();
greenLight.setColor(Color.valueOf("#00ff00"));
greenLight.setTranslateX(275);
greenLight.setTranslateY(325);
greenLight.setTranslateZ(325);
Group root = new Group(box, redLight, greenLight);
root.setRotationAxis(Rotate.X_AXIS);
root.setRotate(70);
Scene scene = new Scene(root, 500, 300, true); //true = use depth buffer in Scene
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.setTitle("3D Example");
primaryStage.show();
}
}
- Next create another file known as
Gfx3DExperiments.java
. - And add the following code:
(c). Gfx3DExperiments.java
So guys let;s go ahead and add some imports into our project. We add imports using the import
keyword:
import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.scene.DepthTest;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
The next step is to extend our class
as shown below:
public class Gfx3DExperiments extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage) throws InterruptedException
For you to be able to execute this class you need to assign it a main method using the following definition:
public static void main(String[] args) {
Here is the full code:
package com.jenkov.javafx.gfx3d;
import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.scene.DepthTest;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
public class Gfx3DExperiments extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws InterruptedException {
boolean is3DSupported = Platform.isSupported(ConditionalFeature.SCENE3D);
if(!is3DSupported) {
System.out.println("Sorry, 3D is not supported in JavaFX on this platform.");
return;
}
Box box1 = Gfx3DUtil.createBox(100, 100, 100, 250, 100, 200);
Box box2 = Gfx3DUtil.createBox(100, 100, 100, 300, 150, 400);
PhongMaterial material = new PhongMaterial();
material.setDiffuseColor(Color.TEAL);
box2.setMaterial(material);
PerspectiveCamera camera = Gfx3DUtil.createPerspectiveCamera(false, 150, -100, 250);
Group root = new Group(box1, box2);
//root.setRotationAxis(Rotate.X_AXIS);
//root.setRotate(30);
Scene scene = new Scene(root, 500, 300, true);
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.setTitle("3D Example");
primaryStage.setX(200);
primaryStage.show();
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000);
Platform.runLater(() -> {
camera.setRotationAxis(Rotate.X_AXIS);
camera.setRotate(10);
});
Thread.sleep(1000);
Platform.runLater(() -> {
camera.setRotationAxis(Rotate.X_AXIS);
camera.setRotate(20);
});
Thread.sleep(1000);
Platform.runLater(() -> {
camera.setRotationAxis(Rotate.X_AXIS);
camera.setRotate(30);
});
} catch (InterruptedException e) {
System.out.println("Thread interrupted: " + e.getMessage());
}
});
thread.start();
/*
Thread.sleep(1000);
Platform.runLater(() -> { camera.setRotationAxis(Rotate.X_AXIS); camera.setRotate(10);});
Thread.sleep(1000);
Platform.runLater(() -> { camera.setRotationAxis(Rotate.X_AXIS); camera.setRotate(20);});
Thread.sleep(1000);
Platform.runLater(() -> { camera.setRotationAxis(Rotate.X_AXIS); camera.setRotate(30);});
*/
}
}
- Next create another file known as
Gfx3DUtil.java
. - And add the following code:
(d). Gfx3DUtil.java
So guys let;s go ahead and add some imports into our project. We add imports using the import
keyword:
import javafx.scene.PerspectiveCamera;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
Let’s go ahead and create the following simple class
:
public class Gfx3DUtil {
Here is the full code:
package com.jenkov.javafx.gfx3d;
import javafx.scene.PerspectiveCamera;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
public class Gfx3DUtil {
public static PerspectiveCamera createPerspectiveCamera(
boolean fixedEyeAtCameraZero, double x, double y, double z ) {
PerspectiveCamera camera = new PerspectiveCamera(fixedEyeAtCameraZero);
camera.setTranslateX(x);
camera.setTranslateY(y);
camera.setTranslateZ(z);
return camera;
}
public static Box createBox(double width, double height, double depth, double x, double y, double z) {
Box box = new Box(width, height, depth);
box.setCullFace(CullFace.NONE);
box.setTranslateX(x);
box.setTranslateY(y);
box.setTranslateZ(z);
return box;
}
}
Download
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |