Java Graphics2D颜色问题

Java Graphics2D颜色问题,java,Java,我已经搜索了一段时间,但还没有找到我问题的答案 首先,我将展示两张对比图片: 方法1: 方法2: 方法1从来没有给我带来任何麻烦,但我最近发现它只是需要太长时间,方法2解决了这个问题 方法1的代码: private void drawDefaultOrientation() { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int dx =

我已经搜索了一段时间,但还没有找到我问题的答案

首先,我将展示两张对比图片:

方法1:

方法2:

方法1从来没有给我带来任何麻烦,但我最近发现它只是需要太长时间,方法2解决了这个问题

方法1的代码:

private void drawDefaultOrientation() {
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int dx = Math.min(x, width - 1 - x);
            int dy = Math.min(y, height - 1 - y);
            if (dx < borderSize || dy < borderSize) {
                inBorder(dx, dy);
            }
            else {
                outBorder(dx, dy);
            }
            bufferedImage.setRGB(xOffset + x, yOffset + y, color.getRGB());
        }
    }
}    
我真的不明白为什么这里有色差

我真的希望外面的任何人都能帮助我。首先,我认为它与Alpha值有关,但正如所见,Alpha变量仍然存在于方法2中

尊敬。

我建议,对于“简单”的东西(包括方框、形状、渐变等),您可以直接使用Java2D API。它将更高效,编写起来也更简单

例如,要使用颜色填充图像中的矩形,请执行以下操作:

public void rectangle(Color color, float x1, float y1, float w, float h) {
    Graphics2D g = bufferedImage.createGraphics();
    g.setColor(color);
    g.fill(new Rectangle2D.Float(x1, y1, w, h));
    g.dispose(); // optional but releases the resource earlier
}

你也可以用“g”来画你需要画的东西。

随机猜测:不同的颜色空间?我同意如果颜色真的非常不同。。。但是我不确定发生了什么,因为颜色之间的距离不是很远。区别主要在于亮度。另一个原因可能是增加了偏移或压缩扭曲了RGB值。
buffereImage.setRGB(…)
做什么?对于方法2,我们需要知道如何创建图像(以获得颜色空间)。一些颜色空间+编码只是略有不同(比如一个编码亮度,另一个编码亮度的平方),所以它可能会产生你观察到的效果。
    new CustomRectangle(bufferedImage, 220, 90, 15, 245, 5, defaultOrientation) {
        @Override
        public void inBorder(final int dx, final int dy) {
            setColor(new Color(red, green, blue, 255 - Math.min(dx, dy)));
        }

        @Override
        public void outBorder(final int dx, final int dy) {
            setColor(new Color(red, green, blue, 128 - Math.min(dx, dy)));
        }
    }.draw();  
public void rectangle(Color color, float x1, float y1, float w, float h) {
    Graphics2D g = bufferedImage.createGraphics();
    g.setColor(color);
    g.fill(new Rectangle2D.Float(x1, y1, w, h));
    g.dispose(); // optional but releases the resource earlier
}