我需要在JavaFX Canvas上每秒30次从10K到15K的多边形绘制。我创建了不同的Thread,它创建了新的Canvas对象,绘制了它所需的一切,并在Application UI线程中替换了Canvas。但遗憾的是,由于通过调用方法
绘制多边形,因此工作缓慢graphicsContext.fillPolygon(xPoints, yPoints, len);
有没有办法提高绘图速度?
所以绘图线程:
public class RMapFXView implements Runnable {
private final static int FPS = 30;
private final RMapModel mapModel;
private final RMapController controller;
public RMapFXView(RMapModel mapModel, RMapController controller) {
this.mapModel = mapModel;
this.controller = controller;
}
private Canvas draw(){
Canvas bufCanvas = new Canvas(controller.getWidth(), controller.getHeight());
GraphicsContext context = bufCanvas.getGraphicsContext2D();
//getting the objects to draw
Map<Integer, List<RObject>> visibleLayers = mapModel.getVisibleObjects();
MBR mapMBR = mapModel.getVisibleMBR();
for (Integer i: visibleLayers.keySet()){
List<RObject> layer = visibleLayers.get(i);
for(RObject o: layer){
int start = o.getStartPoint();
int end = o.getEndPoint();
double[] xPoints = new double[end - start + 1];
double[] yPoints = new double[end - start + 1];
//filling the xPoints and yPoints, setting the fill color, stroke color and so on
if (o.isClosed()) {
context.fillPolygon(xPoints, yPoints, xPoints.length);
} else {
context.strokePolyline(xPoints, yPoints, xPoints.length);
}
}
context.fillText("" + layer.size(), 10, (i+ 2) * 10);
}
return bufCanvas;
}
@Override
public void run() {
while (!Thread.interrupted()){
long startTime = System.nanoTime();
//replacing current canvas
controller.setMapCanvas(draw());
long endTime = System.nanoTime();
long sleepTime = (TimeUnit.NANOSECONDS.convert(1L, TimeUnit.SECONDS)/FPS) - (endTime - startTime);
if(sleepTime > 0)
try {
Thread.sleep(TimeUnit.MILLISECONDS.convert(sleepTime, TimeUnit.NANOSECONDS));
} catch (InterruptedException e) {
break;
}
}
}
}
计算机和JVM选项:
Prism pipeline init order: d3d sw
Using platform text rasterizer
Using native-based Pisces rasterizer
Using dirty region optimizations
Not using texture mask for primitives
Not forcing power of 2 sizes for textures
Using hardware CLAMP_TO_ZERO mode
Opting in for HiDPI pixel scaling
Prism pipeline name = com.sun.prism.d3d.D3DPipeline
Loading D3D native library ...
succeeded.
Direct3D initialization succeeded
D3DPipelineManager: Created D3D9 device
(X) Got class = class com.sun.prism.d3d.D3DPipeline
Initialized prism pipeline: com.sun.prism.d3d.D3DPipeline
Maximum supported texture size: 8192
Maximum texture size clamped to 4096
OS Information:
Windows 7 build 7601
D3D Driver Information:
Intel(R) HD Graphics 4000 //tried on different machine with ATI Radeon 45XX - the same situation
\\.\DISPLAY1
Driver aticfx32.dll, version 8.17.10.1119
Pixel Shader version 3.0
Device : ven_8086, dev_0166, subsys_17F4103C
Max Multisamples supported: 4
vsync: true vpipe: true