关于java重写paint()方法的问题 15
请问这两个方法有什么不同?具体问题看代码中的解释。谢啦!在线等。。。//...MyButtonextendsjavax.swing.JButton;//...在构造器中已...
请问这两个方法有什么不同?具体问题看代码中的解释。谢啦!在线等。。。
//...MyButton extends javax.swing.JButton;
//...在构造器中已经setOpaque(false);
@Override
public void paint(Graphics g) {
// Create an image for the button graphics if necessary
/**(1)新建一个buttonImage,这个有什么用?*/
if (buttonImage == null || buttonImage.getWidth() != getWidth() ||
buttonImage.getHeight() != getHeight()) {
buttonImage = getGraphicsConfiguration().
createCompatibleImage(getWidth(), getHeight());
}
Graphics gButton = buttonImage.getGraphics();
gButton.setClip(g.getClip());
// Have the superclass render the button for us
/**(2)以下两个方法调用,有不同的结果,想不通!麻烦解释!谢谢!*/
// super.paint(gButton);//[1]
super.paint(g);//[2]
// Make the graphics object sent to this paint() method translucent
Graphics2D g2d = (Graphics2D)g;
AlphaComposite newComposite =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f);
g2d.setComposite(newComposite);
// Copy the button's image to the destination graphics, translucently
g2d.drawImage(buttonImage, 0, 0, null);
} 展开
//...MyButton extends javax.swing.JButton;
//...在构造器中已经setOpaque(false);
@Override
public void paint(Graphics g) {
// Create an image for the button graphics if necessary
/**(1)新建一个buttonImage,这个有什么用?*/
if (buttonImage == null || buttonImage.getWidth() != getWidth() ||
buttonImage.getHeight() != getHeight()) {
buttonImage = getGraphicsConfiguration().
createCompatibleImage(getWidth(), getHeight());
}
Graphics gButton = buttonImage.getGraphics();
gButton.setClip(g.getClip());
// Have the superclass render the button for us
/**(2)以下两个方法调用,有不同的结果,想不通!麻烦解释!谢谢!*/
// super.paint(gButton);//[1]
super.paint(g);//[2]
// Make the graphics object sent to this paint() method translucent
Graphics2D g2d = (Graphics2D)g;
AlphaComposite newComposite =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f);
g2d.setComposite(newComposite);
// Copy the button's image to the destination graphics, translucently
g2d.drawImage(buttonImage, 0, 0, null);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询