clearRect绘制黑色而不是透明

时间:2017-05-11 17:28:01

标签: java swing graphics

clearRect似乎没有将我的BufferedImage设置为透明而是设置为黑色。创建new BufferedImage有效,但我不想产生太多垃圾。 我的国际象棋程序有这个代码:

//graphics
private Graphics2D g2d;

private Graphics2D gLayer1;
private Graphics2D gLayer2;
private Graphics2D gLayer3;

public BufferedImage imgLayer1;
private BufferedImage imgLayer2;
private BufferedImage imgLayer3;

private Game() throws IOException {

    //initialize...

    imgLayer1 = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    gLayer1 = imgLayer1.createGraphics();

    imgLayer2 = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    gLayer2 = imgLayer2.createGraphics();

    imgLayer3 = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    gLayer3 = imgLayer3.createGraphics();
}

//main
public static void main(String[] args) throws IOException {

    //initialize frame
}

public void run() {

    while (isRunning) {

        updateGraphicsLayer1();
        updateGraphicsLayer2();
        updateGraphicsLayer3();

        repaint();
    }
}

protected void paintComponent(Graphics g) {
    g2d = (Graphics2D) g;

    g.drawImage(imgLayer1, 0, 0, null);
    g.drawImage(imgLayer2, 0, 0, null);
    g.drawImage(imgLayer3, 0, 0, null);

    g2d.dispose();
}

public void updateGraphicsLayer1() {
    if (redrawLayer1) {

        gLayer1.clearRect(0, 0, WIDTH, HEIGHT);

        //draw this layer
    }
}

public void updateGraphicsLayer2() {
    if (redrawLayer2) {

        gLayer2.clearRect(0, 0, WIDTH, HEIGHT);

        //draw this layer
    }
}

public void updateGraphicsLayer3() {
    if (redrawLayer3) {

        gLayer3.clearRect(0, 0, WIDTH, HEIGHT);

        //draw this layer
    }
}

这就是我得到的:

enter image description here

我有什么要做的? 如何清除我的图层,使它们透明?

0 个答案:

没有答案