Graphics2D会在下次调用paintComponent()时重置组合吗?为什么?

时间:2013-06-24 18:20:51

标签: java swing transparency graphics2d paintcomponent

每次调用paintComponent()时,Graphics2D都会重置组合吗?为什么?

例如,

public void paintComponent(Graphics g)

    {
    Graphics2D g2 = (Graphics2D)g;
    System.out.println(((AlphaComposite)g2.getComposite()).getAlpha()); //printing alpha value before setting composition
    g.drawImage(background,0,0,500,500,null);
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
    g.setColor(Color.RED);
    g.fillOval(x-100,y-100,200,200);
    System.out.println(((AlphaComposite)g2.getComposite()).getAlpha()); //printing alpha value after setting composition
}

上面的代码在几次调用paintComponent()时打印以下内容......

1.0

0.5

1.0

0.5

1.0

0.5

1.0

0.5

这是否意味着graphics2D在paintComponent()之后重置了合成,为什么必须这样做呢?

1 个答案:

答案 0 :(得分:1)

是;你的结果是预期的结果。在Default Rendering Attributes中,默认复合 应用AlphaComposite.SRC_OVER规则,即“等同于setComposite(new AlphaComposite.SrcOver)”。请注意,AlphaComposite.SrcOver的字母为1.0f