每次调用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()之后重置了合成,为什么必须这样做呢?
答案 0 :(得分:1)
是;你的结果是预期的结果。在Default Rendering Attributes中,默认复合
应用AlphaComposite.SRC_OVER
规则,即“等同于setComposite(new AlphaComposite.SrcOver)
”。请注意,AlphaComposite.SrcOver
的字母为1.0f
。