今天我在下列情况下遇到了一个问题:我想要使用fillPolygon(...)绘制到JavaFX Canvas中的很多多边形,但是由于没有明确的原因,一些填充有时会被扭曲(在多边形的边界框)。奇怪的是:它似乎只在以下情况下发生(如下例所示):
使用Java JDK 1.8.0_73
在我的Windows7计算机(64位)上出现问题我将问题跟踪到下面的自包含示例。启动时,将调用paint()并在窗口顶部绘制所有多边形。当我按下某个键时,再次调用paint(),但多边形会失真。
任何提示都表示赞赏。这是代码:
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class PolygonBug
extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage)
throws Exception {
Canvas canvas = new Canvas(1500, 50);
LineChart<Number,Number> chart = new LineChart<>(new NumberAxis(), new NumberAxis());
VBox box = new VBox(canvas, chart);
Scene scene = new Scene(box);
scene.setOnKeyPressed((e) -> paint(canvas));
primaryStage.setScene(scene);
primaryStage.show();
paint(canvas);
}
private void paint(Canvas canvas) {
// create coordinates
List<double[][]> list = new ArrayList<>();
list.add(new double[][]{new double[]{100, 60, 60, 100}, new double[]{30, 10, 30, 30}});
list.add(new double[][]{new double[]{110, 113, 116, 110}, new double[]{30, 10, 30, 30}});
list.add(new double[][]{new double[]{130, 133, 136, 130}, new double[]{30, 10, 30, 30}});
// clear canvas and draw polygons
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.YELLOW);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setFill(Color.ORANGE);
for (int i=0; i<5000; i++) {
for (double[][] coords : list) {
gc.fillPolygon(coords[0], coords[1], coords[0].length);
}
}
}
}