2013-06-24 42 views
0

每次調用paintComponent()時,Graphics2D是否會重置組合?爲什麼?下次調用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

這是一個JFrame或一個小程序 – imulsion

回答

1

是;你的結果是預期的結果。在Default Rendering Attributes中,默認複合 應用AlphaComposite.SRC_OVER規則,該規則「相當於setComposite(new AlphaComposite.SrcOver)」。請注意,AlphaComposite.SrcOver的字母數字爲1.0f

相關問題