简体   繁体   中英

JComponent doesn't keep transparency on repaint but does on resize

So I'm painting a couple custom JComponets in a grid. 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. 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.

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

alpha 0.0f means completely transparent, 1.0f means not transparent.

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. All I had to do was set the JPanel in the Back To Transparent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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