代码之家  ›  专栏  ›  技术社区  ›  caleb baker

JComponent在重新绘制时不保持透明度,但在调整大小时保持透明度

  •  0
  • caleb baker  · 技术社区  · 7 年前

    所以我在网格中绘制了两个自定义JComponet。就像一个简单的战舰游戏。然而,我想给它们增加透明度。第一次渲染效果很好,但是如果我调用repait,那么alpha级别就会消失。然后,我可以调整框架的大小,它会自动更新,并具有正确的透明度。

    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) {
    
    
    }
    

    }

    Example

    2 回复  |  直到 7 年前
        1
  •  1
  •   miskender    7 年前

    如果将以下行添加到 paintComponent 方法,它应该会起作用。

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

    alpha 0.0f表示完全透明,1.0f表示不透明。

        2
  •  0
  •   caleb baker    7 年前

    好的,我知道答案了。抱歉,这是其他代码领域的一个bug。出于某种原因,这些瓷砖是在JPanels背景色下渲染的。我所要做的就是将后面的JPanel设置为透明。

    推荐文章