JavaFX 3D Tutorial #1 – Introduction to 3D development

This is the the first chapter of JavaFX 3D Tutorial.

JavaFX provides an easy to use 3D API. It provides GPU based acceleration and hence can make use of latest powerful hardware. In this tutorial series, we will learn about using JavaFX 3D in our everyday applications.

Course Introduction

JavaFX 3D Coordinates

When we dive in to 3D application development, the most important part is the coordinate system. You have to understand how the x, y and z-axis changes in the screen. The following image describes the coordinate system in JavaFX 3D.

  • X increases when you go from left to right
  • Y increases when you go from top to bottom
  • Z increases when the objects goes away from you.

JavaFX 3D Camera System

Camera defines how we see an object. When you want to transform an object, you can either work on the object or the camera.

JavaFX rotation concept. Taken from tutorial video

So, let’s say you want to rotate an object. You can do this either by rotating the object or rotating the camera around the object itself.

JavaFX provides two cameras. Perspective camera and Parallel Camera. We’ll talk more about camera in Chapter 2.

Create a sphere in JavaFX 3D

JavaFX provides set of predefined 3D objects. You can create your own custom 3D shapes if you want. But to start with, these predefined shapes are the best.

import javafx.application.Application;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;

/**
 * @author afsal villan
 * @version 1.0
 *
 * https://genuinecoder.com
 */
public class Sphere3D extends Application {

  private static final int WIDTH = 1400;
  private static final int HEIGHT = 800;

  @Override
  public void start(Stage primaryStage) {
    Sphere sphere = new Sphere(50);

    Group group = new Group();
    group.getChildren().add(sphere);

    Camera camera = new PerspectiveCamera();
    Scene scene = new Scene(group, WIDTH, HEIGHT);
    scene.setFill(Color.SILVER);
    scene.setCamera(camera);

    sphere.translateXProperty().set(WIDTH / 2);
    sphere.translateYProperty().set(HEIGHT / 2);

    primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, event ->{
      switch (event.getCode()) {
        case W:
          sphere.translateZProperty().set(sphere.getTranslateZ() + 100);
          break;
        case S:
          sphere.translateZProperty().set(sphere.getTranslateZ() - 100);
          break;
      }
    });

    primaryStage.setTitle("Genuine Coder");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

Let’s break down the code.

  1. Create a new sphere.
    The predefined Sphere shape is available in javafx.scene.shape.Shape3D package. You can simply create an instance. The parameter specifies the radius.

    Sphere sphere = new Sphere(50);
    
  2. Create a group as container
    Group group = new Group(); 
    group.getChildren().add(sphere);
    
  3. Prepare camera
    JavaFX provides two camera systems. Parallel camera and perspective camera. Perspective camera allows to view objects from a specified point. You can simply create an instance and attach it to the scene.

    //Create a new perspective camera
    Camera camera = new PerspectiveCamera(); 
    //Attach camera to scene
    scene.setCamera(camera);
    
  4. Move sphere to center of the screen
    We can move the 3D object around using translateProperty. Here the object is moved to center of the screen.

    sphere.translateXProperty().set(WIDTH / 2); 
    sphere.translateYProperty().set(HEIGHT / 2);
    
  5. Add keyboard listener to control Z-axis / Zoom.
    The z-axis can be controlled using translateZProperty. Currently, using KeyEvent.KEY_PRESSED event handler, we can listener for keyboard input. When ‘W’ is pressed, the object goes away from the user as the z gets increased and when ‘S’ is pressed, vice versa.

        primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
          switch (event.getCode()) {
            case W:
              sphere.translateZProperty().set(sphere.getTranslateZ() + 100);
              break;
            case S:
              sphere.translateZProperty().set(sphere.getTranslateZ() - 100);
              break;
          }
        });
    
  6. Attach scene to stage and display.

Chapter 1 Tutorial Video

Visit JavaFX 3D Course Index Page

Comments

50 responses to “JavaFX 3D Tutorial #1 – Introduction to 3D development”

  1. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  2. … [Trackback]

    […] Read More Information here to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  3. … [Trackback]

    […] Find More here to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  4. … [Trackback]

    […] Read More here to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  5. … [Trackback]

    […] Read More here to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  6. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  7. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  8. … [Trackback]

    […] Find More Info here on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  9. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  10. … [Trackback]

    […] Find More on on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  11. … [Trackback]

    […] There you can find 4178 more Information on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  12. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  13. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  14. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  15. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  16. … [Trackback]

    […] Find More on to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  17. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  18. … [Trackback]

    […] Read More Information here on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  19. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  20. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  21. … [Trackback]

    […] Information on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  22. … [Trackback]

    […] Here you can find 75324 additional Info to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  23. … [Trackback]

    […] Find More on on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  24. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  25. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  26. … [Trackback]

    […] Find More Info here to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  27. … [Trackback]

    […] Read More on to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  28. … [Trackback]

    […] Find More here to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  29. … [Trackback]

    […] Read More here to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  30. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  31. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  32. … [Trackback]

    […] Information to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  33. … [Trackback]

    […] Info on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  34. … [Trackback]

    […] Read More Information here to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  35. … [Trackback]

    […] There you will find 3462 more Information on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  36. … [Trackback]

    […] Information to that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  37. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/javafx-3d-tutorial-introduction/ […]

  38. buying enclomiphene generic side effect

    online order enclomiphene generic online pharmacy

  39. acheter générique kamagra

    acheter kamagra medicament nist prescrire

  40. androxal at discount price

    get the best androxal tablets androxal

  41. buy cheap dutasteride

    usa phizer brand hame dutasteride from phizer

  42. get flexeril cyclobenzaprine generic order

    cheapest buy flexeril cyclobenzaprine generic overnight shipping

  43. buy cheap gabapentin price from cvs

    how to order gabapentin cheap new zealand

  44. fildena fedex without a perscription

    no rx needed for purchasing fildena

  45. discount itraconazole australia suppliers

    purchase itraconazole generic uk buy

  46. discount staxyn new york city

    how to buy staxyn generic new zealand

  47. buy avodart purchase generic

    buying avodart how to purchase viagra

  48. buy cheap rifaximin australia online no prescription

    discount rifaximin canada on sale

  49. how to order xifaxan australia where to buy

    order xifaxan buy generic

  50. cena kamagra v obchodě s drogami

    jak získat dámské kamagra ad whas náklady

Leave a Reply