Skip to content

Instantly share code, notes, and snippets.

@rladstaetter
Last active December 14, 2015 03:29
Show Gist options
  • Save rladstaetter/5021163 to your computer and use it in GitHub Desktop.
Save rladstaetter/5021163 to your computer and use it in GitHub Desktop.
A simple JavaFX 3D Demo using scala
package net.ladstatt.apps
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
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
import javafx.scene.image.Image
/**
* original source from here https://wikis.oracle.com/display/OpenJDK/SphereAndBox.java
**/
object SphereAndBox {
def main(args: Array[String]): Unit = {
System.setProperty("prism.dirtyopts", "false")
Application.launch(classOf[SphereAndBox], args: _*)
}
}
class SphereAndBox extends javafx.application.Application with JfxUtils {
var anchorX: Double = _
var anchorY: Double = _
var anchorAngle: Double = _
val bumpMap = new Image(getClass.getResourceAsStream("/jfx.png"))
def addCamera(scene: Scene): PerspectiveCamera = {
val perspectiveCamera = new PerspectiveCamera(false)
scene.setCamera(perspectiveCamera)
perspectiveCamera
}
override def start(primaryStage: Stage) {
primaryStage.setTitle("SphereAndBox")
val boxMaterial = new PhongMaterial()
boxMaterial.setDiffuseColor(Color.GREEN)
boxMaterial.setSpecularColor(Color.WHITESMOKE)
val sphereMaterial = new PhongMaterial()
sphereMaterial.setDiffuseColor(Color.BISQUE)
sphereMaterial.setSpecularColor(Color.LIGHTBLUE)
sphereMaterial.setBumpMap(bumpMap)
val box = new Box(400, 400, 400)
box.setMaterial(boxMaterial)
val sphere = new Sphere(200)
sphere.setMaterial(sphereMaterial)
sphere.setTranslateX(250)
sphere.setTranslateY(250)
sphere.setTranslateZ(50)
box.setTranslateX(250)
box.setTranslateY(250)
box.setTranslateZ(450)
val parent = new Group(box, sphere)
parent.setTranslateZ(500)
parent.setRotationAxis(new Point3D(1, 1, 1))
val root = new Group(parent)
val scene = new Scene(root, 500, 500, 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