可疑的 fillRect() 速度
好吧,让我说清楚。 Java 通过迭代数组并将 RGB 值更改为指定颜色来填充矩形。如果它所做的只是改变颜色,那么如果它所做的只是改变数组中的整数,为什么Texturepaint 如此昂贵?更改之间的整数是否需要时间来注册?
使用 setPaint(new Color()) 进行快速 fillRect() 操作;
setPaint(new Color(0,0,0));
fillRect(0,0,frame.getWidth(),frame.getHeight());
// Around 100+ fps repainting with timer set to zero milliseconds.
使用 setPaint(new TexturePaint()) 进行缓慢的 fillRect() 操作;
setPaint(new TexturePaint(image, rectangle));
fillRect(0,0,frame.getWidth(),frame.getHeight());
// Around 20+ fps repainting with timer set to zero milliseconds.
最佳答案
从 its sourcecode 可以看出,Graphics 将此功能委托(delegate)给子类。
我的实现似乎使用 SunGraphics2d ,它再次将其委托(delegate)给 PixelFillPipe ,有很多实现。 OGLRenderer如果可能的话,使用 OpenGL 将此功能委托(delegate)给显卡。 X11Renderer使用 native X 调用,如下所示:
native void XFillRect(long pXSData, long xgc,
int x, int y, int w, int h);
public void fillRect(SunGraphics2D sg2d,
int x, int y, int width, int height)
{
SunToolkit.awtLock();
try {
long xgc = validate(sg2d);
XFillRect(sg2d.surfaceData.getNativeOps(), xgc,
x+sg2d.transX, y+sg2d.transY, width, height);
} finally {
SunToolkit.awtUnlock();
}
}
XRRenderer使用此代码:
public synchronized void fillRect(SunGraphics2D sg2d,
int x, int y, int width, int height) {
SunToolkit.awtLock();
try {
validateSurface(sg2d);
XRSurfaceData xrsd = (XRSurfaceData) sg2d.surfaceData;
x += sg2d.transform.getTranslateX();
y += sg2d.transform.getTranslateY();
tileManager.addRect(x, y, width, height);
tileManager.fillMask(xrsd);
} finally {
SunToolkit.awtUnlock();
}
}
我向您展示了这段代码,因为它不仅仅是在数组中设置颜色。您的里程会因平台和 JRE 而异。
由于我不知道您使用哪个渲染器/填充管道,所以我只能建议您查看您自己的代码,这并不难。
关于Java fillRect() 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23172632/