我要做一个不断变大的圆,又希望保留以前的园。当然方法很多,我的方法是改写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);
}
}
解决方案 »
- 针对10,100,32,45,58,126,3,29,200,400,0利用除商留余法构造存于长度为13的数据的HASH
- 数据类型
- 有关的java enum 问题
- 有关char与byte数组的相互转化问题
- java开发数据库c/s结构(非web),c端一般用什么开发?
- 讨论:doGet,doPost等方法传进去的request,response到底是什么类型,迷惑!!!!!
- ¥¥¥ 高分求 在eclipse下安装配置spring的jpetstore示例
- 重装系统出现的问题。请帮忙。
- sun VM jre & Microsoft VM的奇怪问题
- 高分求教,关于focus
- 告诉给我看一下,为什么客户端输入数字,服务器不返回呢?
- java paint
既然你改写的update()方法中没有调用paint()方法,也就是说,即便给出了paint()方法的定义,这个方法也是无法被repaint()调用到的。
你可以在程序运行的时候对窗口做resize,应该可以看到paint被resize调用的效果。
原生update 的功能主要就是 擦除 和 调用paint
楼主在重写的update 里调用下paint就是~
谢谢诶。
改成了
public void update(Graphics g){
paint(this.getGraphics());
}
就行了.