gpt4 book ai didi

javafx Glow 粉碎动画

转载 作者:太空宇宙 更新时间:2023-11-04 14:04:19 26 4
gpt4 key购买 nike

我刚刚开始编码和学习 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节点而不是GraphicsContext

动画将以正常速度而不是幻灯片播放速度发生。

更改:

gc.setEffect(glow); 

致:

canvas.setEffect(glow); 

JavaFX 的内部实现中一定有某些东西会导致应用效果,例如 GlowGraphicsContext 而不是 Node 的效率要低几个数量级。您可能想在 JavaFX issue tracker 中提交问题让开发人员调查它。

测试环境:Java 8u40、OS X 10.9

关于javafx Glow 粉碎动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29019024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com