[英]Graphics2D animation, cant understand why paintComponent only draws the shape one time and then stops
[英]Does Graphics2D reset the composititon the next time paintComponent() is called? And why?
每次調用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()之后重置了合成,為什么必須這樣做?
是; 您的結果就是預期的結果。 在默認渲染屬性中 ,默認組合應用AlphaComposite.SRC_OVER
規則,該規則“等效於setComposite(new AlphaComposite.SrcOver)
。 請注意, AlphaComposite.SrcOver
的alpha為1.0f
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.