简体   繁体   English

Java Graphics2D颜色问题

[英]Java Graphics2D Color Issue

I've searched for some time already but haven't been able to find an answer to my question. 我已经搜索了一段时间,但无法找到我的问题的答案。

First I'll show two comparison pictures: 首先,我将显示两个比较图片:

Method 1: method 1 http://img713.imageshack.us/img713/3558/tcg6.jpg 方法1: 方法1 http://img713.imageshack.us/img713/3558/tcg6.jpg

Method 2: method 2 http://img716.imageshack.us/img716/2755/tcg7.jpg 方法2: 方法2 http://img716.imageshack.us/img716/2755/tcg7.jpg

Method 1 has never given me any trouble, but I recently found out that it simply takes too long, and method 2 fixed that. 方法1从来没有给我带来任何麻烦,但是我最近发现它花费的时间太长了,方法2解决了这个问题。

Code for method 1: 方法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());
        }
    }
}    

Code for method 2: 方法2的代码

private void drawDefaultOrientation() {
    DataBufferInt buffer = (DataBufferInt)bufferedImage.getRaster().getDataBuffer();
    int[] pixelArray = buffer.getData();
    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);
            }
            pixelArray[(xOffset + x) + ((yOffset + y) * bufferedImage.getWidth())] = color.getRGB();
        }
    }
}  

Please also note that the inBorder(dx, dy); 另请注意,inBorder(dx,dy); and outBorder(dx, dy); 和outBorder(dx,dy); set the color variable to a color with a Red, Green, Blue and Alpha value. 将color变量设置为具有Red,Green,Blue和Alpha值的颜色。

Callee code: 被叫方代码:

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

I am really lost as to why the color difference is here. 我真的不知道为什么这里有色差。

I really hope anyone out there can help me. 我真的希望外面有人能帮助我。 First I thought it had to do with the Alpha values, but as seen the alpha variaton still exists with method 2. 首先,我认为它与Alpha值有关,但是可以看出方法2仍然存在alpha可变参数。

Regards. 问候。

I'd suggest, for "simple" thing (including boxes, shapes, gradients, and a lot more), you use directly the Java2D API. 我建议,对于“简单”的东西(包括盒子,形状,渐变等等),您可以直接使用Java2D API。 It will be both more efficient and simpler to write. 它将更加高效且易于编写。

For example to fill a rectangle in your image with a color: 例如,用颜色填充图像中的矩形:

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
}

You can also use "g" to draw as much things as you need to do. 您也可以使用“ g”绘制所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM