javafx Glow 粉碎动画
javafx Glow shatter the animation
我刚刚开始编写代码和学习 javafx 并在做一个简单的项目,我的问题是在我为我的图形上下文添加发光效果后动画速度显着减慢。
/**
* JavaFX rocks ;)
*/
public class CDLMatrix extends Application {
Stage matrixStage;
private final String characters = "Effect glow = new Glow(1.0); gc.setEffect(glow); WHY SHATTERS ?!";
private final Random random = new Random();
protected final Font font = Font.font("MS PGothic", FontWeight.BOLD, 15);
char[] data = new char[2000 * 2000];
int[] path = new int[2000 * 2000];
private long lastTime = 0;
int getNumberOfCharsPerRow() {
return (int) matrixStage.getWidth() / 12;
}
int getNumberOfCharsPerColumn() {
return (int) matrixStage.getHeight() / 12;
}
// takes random for now
private char getChar() {
return characters.charAt(Math.abs(random.nextInt()
% characters.length()));
}
void update(long now) {
if (lastTime == 0) {
lastTime = now;
}
// fadeTime = how fast trail will fade out
// flipRate = how fast trail chars will change
final int fadeTime = 3;
final float flipRate = 0.01f;
final int fillStart = 100;
final float fillRate = 0.01f;
int numberOfCharsPerRow = getNumberOfCharsPerRow();
int numberOfCharsPerColumn = getNumberOfCharsPerColumn();
int numberOfChars = numberOfCharsPerRow * numberOfCharsPerColumn;
for (int i = numberOfChars - 1; i >= 0; --i) {
if (i + numberOfCharsPerRow < numberOfChars) {
if (path[i] == 255) {
// This means char was just set
// Initialize the next row at this X
// position
path[i + numberOfCharsPerRow] = 255;
data[i + numberOfCharsPerRow] = getChar();
}
}
// path[i] > 64 means if this char Green component > 25%
if (path[i] > 64 && random.nextFloat() < flipRate) {
data[i] = getChar();
}
// Decrement the char Green component
if (path[i] > fadeTime) {
path[i] -= fadeTime;
} else {
path[i] = 0;
}
// First row
// Start doing stuff only if the Green component > 40%
if (i < numberOfCharsPerRow && path[i] <= fillStart) {
if (random.nextFloat() < fillRate) {
path[i] = 255;
data[i] = getChar();
}
}
}
lastTime = now;
}
@Override
public void start(Stage stage) throws Exception {
this.matrixStage = stage;
matrixStage.setTitle("CDL Matrix");
Group root = new Group();
Scene scene = new Scene(root, 1024, 768);
scene.addEventHandler(KeyEvent.KEY_PRESSED,
new EventHandler<KeyEvent>() {
@Override
// F key for full screen ;)
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.F) {
matrixStage.setFullScreen(!matrixStage
.isFullScreen());
}
// ctrl + Q = exit
if (keyEvent.isControlDown()
&& keyEvent.getCode() == KeyCode.Q) {
matrixStage.close();
}
}
});
Canvas canvas = new Canvas();
canvas.widthProperty().bind(matrixStage.widthProperty());
canvas.heightProperty().bind(matrixStage.heightProperty());
final GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFont(font);
// Effect glow = new Glow(1.0); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// gc.setEffect(glow); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
new AnimationTimer() {
@Override
public void handle(long now) {
update(now);
gc.clearRect(0, 0, matrixStage.getWidth(),
matrixStage.getHeight());
gc.setFill(Color.rgb(0, 1, 0));
gc.fillRect(0, 0, matrixStage.getWidth(),
matrixStage.getHeight());
int y = 0;
int numberOfCharsPerRow = getNumberOfCharsPerRow();
int numberOfCharsPerColumn = getNumberOfCharsPerColumn();
// Colors
for (int i = 0; i < numberOfCharsPerRow
* numberOfCharsPerColumn; ++i) {
gc.setFill(Color.rgb(0, path[i], 0));
String text = String.valueOf(data[i]);
gc.fillText(text, (i % numberOfCharsPerRow) * 12 + 1,
y + 13);
if (i % numberOfCharsPerRow == numberOfCharsPerRow - 1) {
y += 12;
}
}
}
}.start();
root.getChildren().add(canvas);
matrixStage.setScene(scene);
matrixStage.show();
}
}
将光晕应用到 Canvas
node rather than to the GraphicsContext
。
动画将以正常速度播放,而不是幻灯片放映速度。
变化:
gc.setEffect(glow);
收件人:
canvas.setEffect(glow);
JavaFX 的内部实现中一定有某些东西导致应用 Glow
to a GraphicsContext
rather than a Node
to be orders of magnitude less efficient. You might want to file an issue in the JavaFX issue tracker 等效果,让开发人员对其进行调查。
测试环境:Java8u40,OSX 10.9
我刚刚开始编写代码和学习 javafx 并在做一个简单的项目,我的问题是在我为我的图形上下文添加发光效果后动画速度显着减慢。
/**
* JavaFX rocks ;)
*/
public class CDLMatrix extends Application {
Stage matrixStage;
private final String characters = "Effect glow = new Glow(1.0); gc.setEffect(glow); WHY SHATTERS ?!";
private final Random random = new Random();
protected final Font font = Font.font("MS PGothic", FontWeight.BOLD, 15);
char[] data = new char[2000 * 2000];
int[] path = new int[2000 * 2000];
private long lastTime = 0;
int getNumberOfCharsPerRow() {
return (int) matrixStage.getWidth() / 12;
}
int getNumberOfCharsPerColumn() {
return (int) matrixStage.getHeight() / 12;
}
// takes random for now
private char getChar() {
return characters.charAt(Math.abs(random.nextInt()
% characters.length()));
}
void update(long now) {
if (lastTime == 0) {
lastTime = now;
}
// fadeTime = how fast trail will fade out
// flipRate = how fast trail chars will change
final int fadeTime = 3;
final float flipRate = 0.01f;
final int fillStart = 100;
final float fillRate = 0.01f;
int numberOfCharsPerRow = getNumberOfCharsPerRow();
int numberOfCharsPerColumn = getNumberOfCharsPerColumn();
int numberOfChars = numberOfCharsPerRow * numberOfCharsPerColumn;
for (int i = numberOfChars - 1; i >= 0; --i) {
if (i + numberOfCharsPerRow < numberOfChars) {
if (path[i] == 255) {
// This means char was just set
// Initialize the next row at this X
// position
path[i + numberOfCharsPerRow] = 255;
data[i + numberOfCharsPerRow] = getChar();
}
}
// path[i] > 64 means if this char Green component > 25%
if (path[i] > 64 && random.nextFloat() < flipRate) {
data[i] = getChar();
}
// Decrement the char Green component
if (path[i] > fadeTime) {
path[i] -= fadeTime;
} else {
path[i] = 0;
}
// First row
// Start doing stuff only if the Green component > 40%
if (i < numberOfCharsPerRow && path[i] <= fillStart) {
if (random.nextFloat() < fillRate) {
path[i] = 255;
data[i] = getChar();
}
}
}
lastTime = now;
}
@Override
public void start(Stage stage) throws Exception {
this.matrixStage = stage;
matrixStage.setTitle("CDL Matrix");
Group root = new Group();
Scene scene = new Scene(root, 1024, 768);
scene.addEventHandler(KeyEvent.KEY_PRESSED,
new EventHandler<KeyEvent>() {
@Override
// F key for full screen ;)
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.F) {
matrixStage.setFullScreen(!matrixStage
.isFullScreen());
}
// ctrl + Q = exit
if (keyEvent.isControlDown()
&& keyEvent.getCode() == KeyCode.Q) {
matrixStage.close();
}
}
});
Canvas canvas = new Canvas();
canvas.widthProperty().bind(matrixStage.widthProperty());
canvas.heightProperty().bind(matrixStage.heightProperty());
final GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFont(font);
// Effect glow = new Glow(1.0); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// gc.setEffect(glow); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
new AnimationTimer() {
@Override
public void handle(long now) {
update(now);
gc.clearRect(0, 0, matrixStage.getWidth(),
matrixStage.getHeight());
gc.setFill(Color.rgb(0, 1, 0));
gc.fillRect(0, 0, matrixStage.getWidth(),
matrixStage.getHeight());
int y = 0;
int numberOfCharsPerRow = getNumberOfCharsPerRow();
int numberOfCharsPerColumn = getNumberOfCharsPerColumn();
// Colors
for (int i = 0; i < numberOfCharsPerRow
* numberOfCharsPerColumn; ++i) {
gc.setFill(Color.rgb(0, path[i], 0));
String text = String.valueOf(data[i]);
gc.fillText(text, (i % numberOfCharsPerRow) * 12 + 1,
y + 13);
if (i % numberOfCharsPerRow == numberOfCharsPerRow - 1) {
y += 12;
}
}
}
}.start();
root.getChildren().add(canvas);
matrixStage.setScene(scene);
matrixStage.show();
}
}
将光晕应用到 Canvas
node rather than to the GraphicsContext
。
动画将以正常速度播放,而不是幻灯片放映速度。
变化:
gc.setEffect(glow);
收件人:
canvas.setEffect(glow);
JavaFX 的内部实现中一定有某些东西导致应用 Glow
to a GraphicsContext
rather than a Node
to be orders of magnitude less efficient. You might want to file an issue in the JavaFX issue tracker 等效果,让开发人员对其进行调查。
测试环境:Java8u40,OSX 10.9