Created
October 28, 2012 20:40
-
-
Save miho/3969834 to your computer and use it in GitHub Desktop.
Example custom render node for JavaFX 2.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.dejay.javafx.example; | |
import javafx.animation.Transition; | |
import javafx.application.Application; | |
import javafx.collections.ObservableList; | |
import javafx.scene.Node; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.Region; | |
import javafx.scene.text.Font; | |
import javafx.stage.Stage; | |
import javafx.util.Duration; | |
import com.sun.javafx.geom.transform.Affine2D; | |
import com.sun.javafx.scene.DirtyBits; | |
import com.sun.javafx.sg.PGNode; | |
import com.sun.javafx.sg.prism.NGRegion; | |
import com.sun.prism.Graphics; | |
import com.sun.prism.paint.Color; | |
public class ExampleCustomPGNode extends Application { | |
@Override | |
public void start(Stage stage) throws Exception { | |
final CustomNode customNode = new CustomNode(); | |
final Label label = new Label("Hello World"); | |
label.setFont(new Font("Arial", 40)); | |
customNode.getChildren().add(label); | |
stage.setScene(new Scene(customNode)); | |
stage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
// custom node that creates the custom PGNode for rendering | |
private static class CustomNode extends Region { | |
private PGCustomNode pgNode; | |
public CustomNode() { | |
setCache(false); | |
setPrefWidth(320); | |
setPrefHeight(200); | |
} | |
@Override | |
public ObservableList<Node> getChildren() { | |
return super.getChildren(); | |
} | |
@Override | |
public PGNode impl_createPGNode() { | |
pgNode = new PGCustomNode(); | |
// bind this to an fps property | |
double framerate = 60; | |
new Transition(framerate) { | |
{ | |
setCycleDuration(Duration.INDEFINITE); | |
} | |
@Override | |
protected void interpolate(double frac) { | |
impl_markDirty(DirtyBits.NODE_CONTENTS); | |
} | |
}.play(); | |
return pgNode; | |
} | |
// custom PG render node that is used in the prism render thread | |
private class PGCustomNode extends NGRegion { | |
@Override | |
protected void renderContent(Graphics graphics) { | |
super.renderContent(graphics); | |
// you are in the rendering thread here | |
// under OS X or linux, you can call openGL commands here | |
double rot = System.nanoTime() * 1e-9; | |
double w = getWidth() / 2; | |
double h = getHeight() / 2; | |
Affine2D t = new Affine2D(); | |
t.setToTranslation(w / 2, h / 2); | |
t.rotate(rot); | |
graphics.setTransform(t); | |
graphics.setPaint(Color.BLACK); | |
graphics.drawEllipse(0, 0, 100, 100); | |
graphics.setPaint(Color.BLUE); | |
graphics.fillRect(-100, -20, 100, 20); | |
graphics.setPaint(Color.BLACK); | |
graphics.drawRect(-100, -20, 100, 20); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!