gpt4 book ai didi

java - 根据 Canvas 上的位置((x,y) 坐标)用不同颜色绘制 SWT 点

转载 作者:太空宇宙 更新时间:2023-11-04 11:10:08 25 4
gpt4 key购买 nike

我正在尝试用不同的颜色在 Canvas 上绘制点。基本上,当前点为蓝色,当前点之前绘制的点为绿色,当前点之后绘制的点为红色。请看代码

private void setElemColor(GC g, int pos) {
int currentPoint = messtisch.getPointPos(); //current point, number
if (pos > currentPoint) {
g.setForeground(cRed);
} else if (pos == currentPoint) {
g.setForeground(cBlue);
} else if (pos < currentPoint) {
g.setForeground(cGreen);
}
}

增加理解力。这很完美。但我试图使用 Point 代替 Int 来做同样的事情,但逻辑不正确。如

private void setPointColor(GC g, Point cpoint) {
if (cpoint.equals(currentPoint)) { // the current point itself
g.setForeground(cBlue);
} else if (!cpoint.equals(currentPoint)) {
if (cpoint.x > currentPoint.x || cpoint.y > currentPoint.y) {
g.setForeground(cRed);
} else {
g.setForeground(cGreen);
}
}
}

请帮我解决一下。

最佳答案

我通过使用新的 ArrayList 并将点保存到已经是 currentPoint 的位置来做到这一点。然后用绿色绘制它们作为旧点。这是我的代码示例。

private ArrayList<Point> oldpoints = new ArrayList<Point>();  

private void setPointColor(GC g, Point cpoint) {
if (oldpoints.contains(cpoint)) {
g.setForeground(cGreen);
} else if (!oldpoints.contains(cpoint)) {
g.setForeground(cRed);
}

if (cpoint.equals(currentPoint)) {
g.setForeground(cBlue);
oldpoints.add(cpoint);
}
}

请建议其他方法,因为这个方法效率不高且不符合逻辑。预先感谢您。

关于java - 根据 Canvas 上的位置((x,y) 坐标)用不同颜色绘制 SWT 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46112985/

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