Question

Suspicious fillRect() Speed

Okay so let me get this straight. Java fills rectangles by iterating through an array and changing the rgb values to a designated color. If all it does is change the color then why is Texturepaint so expensive if all it is doing is changing the integer in the array? Does changing the integer in between take time to register?

Fast fillRect() operation using setPaint(new Color());

setPaint(new Color(0,0,0));
fillRect(0,0,frame.getWidth(),frame.getHeight());

// Around 100+ fps repainting with timer set to zero milliseconds.

Slow fillRect() operation using setPaint(new TexturePaint());

setPaint(new TexturePaint(image, rectangle));
fillRect(0,0,frame.getWidth(),frame.getHeight());

// Around 20+ fps repainting with timer set to zero milliseconds.
Was it helpful?

Solution

As you can see from its sourcecode, Graphics delegates this functionality to subclasses.

My implementation seems to use SunGraphics2d, which again delegates it to a PixelFillPipe, which there are many implementations of. The OGLRenderer delegates this functionality to the Graphics card if possible, using OpenGL. The X11Renderer uses native X calls, like this:

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 uses this code:

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();
    }
}

I showed you this code, because it is more than setting colors in an array. Your mileage will vary per platform and JRE.

As I don't know which renderer/fillpipe you use, I can only recommend to look at your very own code, it's not that hard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top