我要做一个不断变大的圆,又希望保留以前的园。当然方法很多,我的方法是改写update(),让它不执行擦除。但是当我改写了update()后,程序不反映。除非update()的代码和paint()一样(也是绘制圆的代码,darwString都不行)。于是我让update()的代码和paint()一样。程序有反映了。我试着改了update()中的数值(坐标和颜色),发现程序执行的是update,不是paint中的绘图。why?
import java.awt.*;
public class t{
public static void main(String [] args){
f f1=new f();
Thread t1=new Thread(f1);
t1.start();
f1.setSize(400,400);
f1.setVisible(true);
}
}class f extends Frame implements Runnable{
int x=10;
public void paint(Graphics g){
g.setColor(Color.red);
g.drawOval((getWidth()-x)/2,(getHeight()-x)/2,x,x);
}
public void run(){
while(true){
x=x+1;
repaint();
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
public void update(Graphics g){
g.setColor(Color.blue);
g.drawOval((getWidth())/2,(getHeight())/2,x,x);
}
}
import java.awt.*;
public class t{
public static void main(String [] args){
f f1=new f();
Thread t1=new Thread(f1);
t1.start();
f1.setSize(400,400);
f1.setVisible(true);
}
}class f extends Frame implements Runnable{
int x=10;
public void paint(Graphics g){
g.setColor(Color.red);
g.drawOval((getWidth()-x)/2,(getHeight()-x)/2,x,x);
}
public void run(){
while(true){
x=x+1;
repaint();
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
public void update(Graphics g){
g.setColor(Color.blue);
g.drawOval((getWidth())/2,(getHeight())/2,x,x);
}
}
既然你改写的update()方法中没有调用paint()方法,也就是说,即便给出了paint()方法的定义,这个方法也是无法被repaint()调用到的。
你可以在程序运行的时候对窗口做resize,应该可以看到paint被resize调用的效果。
原生update 的功能主要就是 擦除 和 调用paint
楼主在重写的update 里调用下paint就是~
谢谢诶。
改成了
public void update(Graphics g){
paint(this.getGraphics());
}
就行了.