Skip to content

Instantly share code, notes, and snippets.

@rladstaetter
Created March 25, 2013 19:54
Show Gist options
  • Select an option

  • Save rladstaetter/5240111 to your computer and use it in GitHub Desktop.

Select an option

Save rladstaetter/5240111 to your computer and use it in GitHub Desktop.
JavaFX 3D Axis Demo
package net.ladstatt.apps
import scala.collection.JavaConversions.seqAsJavaList
import scala.util.Random.nextInt
import javafx.application.Application
import javafx.geometry.Point3D
import javafx.scene.Group
import javafx.scene.PerspectiveCamera
import javafx.scene.PointLight
import javafx.scene.Scene
import javafx.scene.input.MouseEvent
import javafx.scene.paint.Color
import javafx.scene.paint.PhongMaterial
import javafx.scene.shape.Box
import javafx.scene.shape.Sphere
import javafx.stage.Stage
object AxisDemo {
def main(args: Array[String]): Unit = {
System.setProperty("prism.dirtyopts", "false")
Application.launch(classOf[AxisDemo], args: _*)
}
}
class AxisDemo extends javafx.application.Application with JfxUtils {
var anchorX: Double = _
var anchorY: Double = _
var anchorAngle: Double = _
def addCamera(scene: Scene): PerspectiveCamera = {
val perspectiveCamera = new PerspectiveCamera(false)
scene.setCamera(perspectiveCamera)
perspectiveCamera
}
def mkMaterial(color: Color) = {
val boxMaterial = new PhongMaterial()
boxMaterial.setDiffuseColor(color)
boxMaterial.setSpecularColor(Color.WHITESMOKE)
boxMaterial
}
def mkRandColor = Color.rgb(nextInt(255), nextInt(255), nextInt(255))
def mkRandBox = {
val width = nextInt(100)
val height = nextInt(100)
val depth = nextInt(100)
val b = new Box(width, height, depth)
b.setMaterial(mkMaterial(mkRandColor))
b.setTranslateX(nextInt(1000 - width))
b.setTranslateY(nextInt(1000 - height))
b.setTranslateZ(nextInt(1000 - depth))
b
}
override def start(primaryStage: Stage) {
primaryStage.setTitle("Axisdemo")
val xAxis = new Box(1000, 10, 10)
xAxis.setMaterial(mkMaterial(Color.GREEN))
xAxis.setTranslateX(500)
val yAxis = new Box(10, 1000, 10)
yAxis.setMaterial(mkMaterial(Color.RED))
yAxis.setTranslateY(500)
val zAxis = new Box(10, 10, 1000)
zAxis.setMaterial(mkMaterial(Color.BLUE))
zAxis.setTranslateZ(500)
val zero = new Sphere(10)
zero.setMaterial(mkMaterial(Color.BLACK))
val parent = new Group(zero, xAxis, yAxis, zAxis)
parent.setTranslateZ(1000)
parent.setRotationAxis(new Point3D(1, 0, 0))
parent.getChildren.addAll(for (i <- 1 to 10) yield mkRandBox)
val root = new Group(parent)
val scene = new Scene(root, 1000, 1000, true)
scene.setOnMousePressed(mkEventHandler((event: MouseEvent) => {
anchorX = event.getSceneX()
anchorY = event.getSceneY()
anchorAngle = parent.getRotate()
}))
scene.setOnMouseDragged(mkEventHandler((event: MouseEvent) => {
parent.setRotate(anchorAngle + anchorX - event.getSceneX())
}))
val pointLight = new PointLight(Color.ANTIQUEWHITE)
pointLight.setTranslateX(15)
pointLight.setTranslateY(-10)
pointLight.setTranslateZ(-100)
root.getChildren().add(pointLight)
addCamera(scene)
primaryStage.setScene(scene)
primaryStage.show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment