java中Canvas的repaint问题 30
我在Jbuilder9中写了如下代码(以下只给出其中的代码片段):publicclasscanvasextendsCanvas{publicvoidpaint(Graph...
我在Jbuilder9中写了如下代码(以下只给出其中的代码片段):
public class canvas extends Canvas {
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
g.fillRoundRect(100,100,50,50,20,20);
g.drawRoundRect(100,100,50,50,20,20);
}
public void update(Graphics g)
{
System.out.println("Paint");
}
}
public class Frame1 extends JFrame {
......
public Frame1() {
......
canvas can=new canvas();
can.setBackground(Color.white);
can.repaint();
......
}
不是说repaint要先调用update,再调用paint的么?这里repain怎么不调用update(因为没有执行System.out.println("Paint");)? 展开
public class canvas extends Canvas {
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
g.fillRoundRect(100,100,50,50,20,20);
g.drawRoundRect(100,100,50,50,20,20);
}
public void update(Graphics g)
{
System.out.println("Paint");
}
}
public class Frame1 extends JFrame {
......
public Frame1() {
......
canvas can=new canvas();
can.setBackground(Color.white);
can.repaint();
......
}
不是说repaint要先调用update,再调用paint的么?这里repain怎么不调用update(因为没有执行System.out.println("Paint");)? 展开
展开全部
一楼的,Canvas是重量级组件,它的repaint会先调用update的。这有个比较奇怪的问题是:如果我把repaint加到paint里面,即:
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
g.fillRoundRect(100,100,50,50,20,20);
g.drawRoundRect(100,100,50,50,20,20);
repaint();
}
那么它会执行System.out.println("Paint"); 也就是说update被调用。如果我在类外执行can.repaint(),就不会执行System.out.println("Paint");也就是不执行update方法,这是为什么啊???不解!
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
g.fillRoundRect(100,100,50,50,20,20);
g.drawRoundRect(100,100,50,50,20,20);
repaint();
}
那么它会执行System.out.println("Paint"); 也就是说update被调用。如果我在类外执行can.repaint(),就不会执行System.out.println("Paint");也就是不执行update方法,这是为什么啊???不解!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询