Java very slow method
<p>I noticed that one of my methods is being very slow, with profiling I noticed it took up like 95% of the total execution time.</p>

<p>The class:</p>

<pre><code>package gui;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

/**
 *
 * @author Frank
 */
public abstract class CustomRectangle {
    protected final BufferedImage bufferedImage;
    protected final int width;
    protected final int height;
    protected final int xOffset;
    protected final int yOffset;
    protected final int borderSize;
    protected final boolean defaultOrientation;

    protected Color color;

    public CustomRectangle(final BufferedImage bufferedImage, final int width, final int height, final int xOffset, final int yOffset, final int borderSize, final boolean defaultOrientation) {
        this.bufferedImage = bufferedImage;
        this.width = width;
        this.height = height;
        if (defaultOrientation) {
            this.xOffset = xOffset;
            this.yOffset = yOffset;
        }
        else {
            this.xOffset = bufferedImage.getWidth() - 1 - xOffset;
            this.yOffset = bufferedImage.getHeight() - 1 - yOffset;
        }
        this.borderSize = borderSize;
        this.defaultOrientation = defaultOrientation;
    }

    abstract public void inBorder(final int dx, final int dy);

    abstract public void outBorder(final int dx, final int dy);

    public void draw() {
        if (defaultOrientation) {
            drawDefaultOrientation();
        }
        else {
            drawOppositeOrientation();
        }
    }

    private void drawDefaultOrientation() {
        int[] pixelArray = ((DataBufferInt)bufferedImage.getRaster().getDataBuffer()).getData();
        for (int x = 0; x &lt; width; x++) {
            for (int y = 0; y &lt; height; y++) {
                int dx = Math.min(x, width - 1 - x);
                int dy = Math.min(y, height - 1 - y);
                if (dx &lt; borderSize || dy &lt; borderSize) {
                    inBorder(dx, dy);
                }
                else {
                    outBorder(dx, dy);
                }
                pixelArray[(xOffset + x) + ((yOffset + y) * bufferedImage.getWidth())] = color.getRGB();
            }
        }
    }    

    private void drawOppositeOrientation() {
        for (int x = 0; x &lt; width; x++) {
            for (int y = 0; y &lt; height; y++) {
                int dx = Math.min(x, width - 1 - x);
                int dy = Math.min(y, height - 1 - y);
                if (dx &lt; borderSize || dy &lt; borderSize) {
                    inBorder(dx, dy);
                }
                else {
                    outBorder(dx, dy);
                }
                bufferedImage.setRGB(xOffset - x, yOffset - y, color.getRGB());              
            }
        }
    }  

    public void setColor(final Color color) {
        this.color = color;
    }
}
</code></pre>

<p>The slow method is the drawDefaultOrientation() method.
The scary part however is that even if I leave out all image-modifying code, then it is still slow.</p>

<p>The callee code:</p>

<pre><code>    new CustomRectangle(bufferedImage, 440, 180, 30, 490, 10, defaultOrientation) {
        @Override
        public void inBorder(final int dx, final int dy) {
            setColor(new Color(red, green, blue, 255 - (int)Math.round(0.5 * Math.min(dx, dy))));
        }

        @Override
        public void outBorder(final int dx, final int dy) {
            setColor(new Color(red, green, blue, 128 - (int)Math.round(0.5 * Math.min(dx, dy))));
        }
    }.draw(); 
</code></pre>

<p>There must be something going on as in any way this code should not take this long, even if it loops over x and y coordinates.</p>

<p>Some thought I had was that it could have to do with the Anonymous inner class + Override...</p>

<p>But hopefully someone with more knowledge can answer the question.</p>
