BufferedImage ARGB透明度仅在绘制的图像移动时有效

时间:2017-09-06 05:23:47

标签: java

我刚将我的项目从RGB转换为ARGB。不透明的对象都画得很好但是当我为任何绘制对象的'color'(由hex:0x55ff0000设置)添加透明度时,如果它们正在移动,它们只会绘制正确的透明度。

问题:Alpha绘制的对象似乎在每一步都“叠加”,直到它们变得不透明。

当它们保持静止时,它们会在一秒钟内迅速变得越来越不透明。我还在学习BufferedImages的工作方式,但我相信我错过了一段我需要的ARGB代码,而不仅仅是RGB。

public class Window
{
private JFrame frame;
static BufferedImage image;
private Canvas canvas;
private BufferStrategy bs;
private Graphics g;

public Window(Game game)
    {
    image = new BufferedImage(game.getWidth(), game.getHeight(), BufferedImage.TYPE_INT_ARGB);
    canvas = new Canvas();
    Dimension s = new Dimension(game.getWidth() * game.getScale(),game.getHeight() * game.getScale());
    canvas.setPreferredSize(s);
    canvas.setMaximumSize(s);
    canvas.setMinimumSize(s);

    frame = new JFrame(game.getName());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    frame.add(canvas, BorderLayout.CENTER); //Add canvas to the frame
    frame.pack(); //Set frame to size of canvas
    //Removed some other 'frame.xxx' lines of code which don't assist the problem.
    //frame.repaint();

    canvas.createBufferStrategy(2);
    bs = canvas.getBufferStrategy();
    g = bs.getDrawGraphics();
    }

public void update()
    {
    g.drawImage(image, 0, 0, canvas.getWidth(), canvas.getHeight(), null); //Draws everything to buffer strategy.
    bs.show(); //Draws from buffer strategy to canvas.
    }

public JFrame getFrame()
    {
    return frame;
    }
}

我改为转换为ARGB的唯一其他代码是事先为0的代码:

    public Renderer(Game game)
    {
    display = ((DataBufferInt)game.getWindow().getImage().getRaster().getDataBuffer()).getData(); //Displays all the data drawn to screen.
    }

public void clear()
    {
    for(int i = 0; i < display.length; i++) //Loops through the screens pixels and clears them.
        {
        display[i] = 0xff000000; //Clear the screen with opaque black.
        }
    }

当我改变显示[i] = 0xff000000;到0x0f000000问题加剧了它有一个慢动作效果 - 但我不知道如何增加已经完全的“ff”更高的alpha值。

我制作了一个问题的GIF,但它有点扭曲:https://giphy.com/gifs/java-RF6stHznOiR9e 请注意在我停止移动后它会如何消失,并且圆圈重叠的位置保持不透明。

0 个答案:

没有答案