简体   繁体   English

JComponent不会在重画时保持透明,但会在调整大小时保持透明

[英]JComponent doesn't keep transparency on repaint but does on resize

So I'm painting a couple custom JComponets in a grid. 因此,我在网格中绘制了几个自定义JComponets。 like a simple battleship game. 就像一个简单的战舰游戏。 However I wanted to add a transparency to them. 但是我想给他们增加透明度。 It renders in great the first time but if I call repaint then the alpha level goes away. 它是第一次很棒的渲染,但是如果我调用重画,则Alpha级别会消失。 I can then resize the frame and it automatically updates it and has the correct transparency. 然后,我可以调整框架的大小,它会自动更新它并具有正确的透明度。

public class Cell extends JComponent implements MouseListener{
public static int CELL_SIZE=50;
private boolean hit = false;
private boolean hasShip = false;
private GridPoint location;
private boolean highlighted = false;
private GameBoard parent;

public static final Color HIT = new Color(Color.RED.getRed(),Color.RED.getGreen(),Color.RED.getBlue(),123);
public static final Color MISS = new Color(80,100,200,123);
public static final Color DEFAULT = new Color(0,0,(150),123);
public static final Color HIGHLIGHT = new Color(255,255,255,50);
public static final Color BORDER = new Color(0,0,40,123);

public Cell()
{
    setSize(Cell.CELL_SIZE,Cell.CELL_SIZE);
}
public Cell(GridPoint g, GameBoard p)
{
    setOpaque(false);
    addMouseListener(this);
    parent = p;
    setGridLocation(g);
    setLocation(CELL_SIZE*location.getX(), CELL_SIZE*location.getY());
    setSize(Cell.CELL_SIZE,Cell.CELL_SIZE);
}

public void paintComponent(Graphics g)
{
    if(hit == false)
    {
        if(highlighted)
        {
            g.setColor(HIGHLIGHT);
            g.fillRect(0, 0, Cell.CELL_SIZE, Cell.CELL_SIZE);
        }   
        g.setColor(DEFAULT);
        g.fillRect(0, 0, Cell.CELL_SIZE, Cell.CELL_SIZE);
    }
    else
    {
        if(hasShip)
        {
            g.setColor(Cell.HIT);
            g.fillRect(0, 0, Cell.CELL_SIZE, Cell.CELL_SIZE);
        }
        else
        {
            g.setColor(Cell.MISS);
            g.fillRect(0, 0, Cell.CELL_SIZE, Cell.CELL_SIZE);
        }
    }
    g.setColor(Cell.BORDER);
    g.drawRect(0, 0, Cell.CELL_SIZE, Cell.CELL_SIZE);
}

public GridPoint getGridLocation() {
    return location;
}
public void setGridLocation(GridPoint location) {
    this.location = location;
}
@Override
public void mouseClicked(MouseEvent e) {
    hit = true;
    repaint();
}
@Override
public void mouseEntered(MouseEvent e)
{
    highlighted = true;
    repaint();
}
@Override
public void mouseExited(MouseEvent e)
{
    highlighted = false;
    repaint();
}
@Override
public void mousePressed(MouseEvent e) {


}
@Override
public void mouseReleased(MouseEvent e) {


}

} }

例

If you add the following lines to start of your paintComponent method, it should work. 如果paintComponent添加到paintComponent方法的开始,则该方法应该起作用。

float alpha = 0.2f;
((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));

alpha 0.0f means completely transparent, 1.0f means not transparent. alpha 0.0f表示完全透明,1.0f表示不透明。

Okay I got the answer. 好吧,我得到了答案。 Sorry it was a bug in some other area of code. 抱歉,这是其他代码区域中的错误。 for some reason the tiles were rendering under the JPanels Background Color. 由于某些原因,这些图块是在“ JPanels背景色”下渲染的。 All I had to do was set the JPanel in the Back To Transparent. 我要做的就是在Back To Transparent中设置JPanel。

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

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