英文:
what is the correct way to change the color of a circle in JavaFx?
问题
圆圈的填充颜色正在改变,但在屏幕上没有显示为已更改的颜色。屏幕每隔125毫秒更新一次,使用以下代码:
// 定期刷新屏幕
public void updateScreen(QTree qt, double n) {
Timeline timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(n), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent ae) {
/* 每隔n毫秒运行此代码 */
qGroup.getChildren().clear();
pGroup.getChildren().clear();
uGroup.getChildren().clear();
box.show(uGroup);
// 首先将所有圆圈更改为默认颜色
for(Point p : points) {
p.setColor(Color.CADETBLUE);
}
// 然后将特定的点更改为绿色
ArrayList<Point> found = qt.query(box);
for(int i = 0; i < found.size(); i++) {
found.get(i).setColor(Color.LIMEGREEN);
// 我使用这个来判断颜色是否实际改变
System.out.println(found.get(i).cir);
}
// 然后显示这些点
for(Point p : points) {
p.show(pGroup); // 显示一个点
}
qt.show(qGroup); // 显示四叉树
/**************************************/
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
我创建的Point类是这样的:
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class Point {
public double x, y;
public Circle cir;
public Point(double x, double y) {
this.x = x;
this.y = y;
cir = new Circle(this.x, this.y, 4);
}
public void show(Group g) {
g.getChildren().add(cir);
}
public void setColor(Color c) {
cir.setFill(c);
}
public void setRadius(double r) {
cir.setRadius(r);
}
}
当我运行代码时,我的打印语句显示:
Circle[centerX=408.1527831669267, centerY=357.17624700724684, radius=4.0, fill=0x32cd32ff]
颜色正在更改为32cd32ff,这是绿色,但没有更新为屏幕上的颜色。
屏幕截图如下:
框内的点应该变为绿色,但实际上保持为cadet blue。
我可能遗漏了一些非常明显的东西,但我还没有找到解决方法。我知道我可以在没有时间线的情况下完成,但我计划稍后使用鼠标移动框。我是否遗漏了什么?
英文:
The fill color of my circle is changing, but not appearing as the changed color on the screen. The screen updates every 125 milliseconds with the code below:
// refresh screen periodically
public void updateScreen(QTree qt, double n) {
Timeline timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(n), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent ae) {
/* Run this code every n milliseconds */
qGroup.getChildren().clear();
pGroup.getChildren().clear();
uGroup.getChildren().clear();
box.show(uGroup);
// first I change all circles to default color
for(Point p : points) {
p.setColor(Color.CADETBLUE);
}
// then change specific points to lime
ArrayList<Point> found = qt.query(box);
for(int i = 0; i < found.size(); i++) {
found.get(i).setColor(Color.LIMEGREEN);
// I use this to find out if the
// color is actually changing
System.out.println(found.get(i).cir);
}
// then display the points
for(Point p : points) {
p.show(pGroup); // display a point
}
qt.show(qGroup); // display quad tree
/**************************************/
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
The point class that I made is this
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class Point {
public double x, y;
public Circle cir;
public Point(double x, double y) {
this.x = x;
this.y = y;
cir = new Circle(this.x, this.y, 4);
}
public void show(Group g) {
g.getChildren().add(cir);
}
public void setColor(Color c) {
cir.setFill(c);
}
public void setRadius(double r) {
cir.setRadius(r);
}
}
when I run the code my print statement writes
Circle[centerX=408.1527831669267, centerY=357.17624700724684, radius=4.0, fill=0x32cd32ff]
the color is changing to 32cd32ff which is lime green but doesn't update to the color on the screen.
the points within the box are supposed to turn lime green, but instead, it stays cadet blue.
I might be missing something painfully obvious, but I haven't been able to figure it out. I know I could do it without the timeline, but I am planning on moving the box with my mouse later. Am I missing something?
答案1
得分: 0
如果您每隔125毫秒刷新屏幕..并且代码以其他颜色启动,然后您更改颜色,那么它会刷新为旧颜色并进行更改,因此您看不到新颜色。
英文:
if you refresh your screen every 125 ms .. and the code start with other color and u change then it's refresh to old color and change so you won't see the new color
专注分享java语言的经验与见解,让所有开发者获益!
评论