JAVA----repaint后不执行update

blue601 2010-05-13 09:46:55
都说在repaint()后,系统会自动运行update(),但为什么我这个不执行呢,我想做个类似时钟的,但指针走后会留下痕迹,请指教

import java.awt.*;
import java.util.Date;
import javax.swing.*;

public class DrawClock extends JApplet implements Runnable{

int xCenter,yCenter,width,height,radius;
int xSec,ySec,xMin,yMin,xHour,yHour;
long hour,min,sec;
Date date;
Thread clockThread = null;
public void init(){
width = 100;
height = 100;
xCenter = (int)(width*0.5)+80;
yCenter = (int)(width*0.5)+80;
radius = (int)(Math.min(width, height));
}

public void start(){
if(clockThread==null){
clockThread=new Thread(this);
}
clockThread.start();
}

public void stop(){
clockThread = null;
}
public void run(){
while(null!=clockThread){

repaint();
try{
Thread.sleep(1000);
}
catch(InterruptedException p){
}
}
}

//public synchronized void update(Graphics g){
public void update(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0,width+1000,height+1000);
//paint(g);
}

public void paint(Graphics g){

int x,y;
g.setColor(Color.red);
g.drawOval(30,30, width+100, height+100);
for(int i=0;i<60;i+=5){
x = (int) (xCenter + radius * Math.sin(i*2*Math.PI/60));
y = (int) (yCenter - radius * Math.cos(i*2*Math.PI/60));
g.fillRect(x-1,y-1,3,3);
}

date = new Date();
hour = date.getHours()%12;
min = date.getMinutes();
sec = date.getSeconds();

xSec = (int)(xCenter + radius*0.9*Math.sin(sec*2*Math.PI/60));
ySec = (int)(yCenter - radius*0.9*Math.cos(sec*2*Math.PI/60));
g.drawLine(xCenter, yCenter, xSec, ySec);
repaint();
}
}

...全文
179 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
txwdyyy001 2010-05-26
  • 打赏
  • 举报
回复
这么简单的问题,自己Google去!
blue601 2010-05-18
  • 打赏
  • 举报
回复
有人回答吗?up up
blue601 2010-05-14
  • 打赏
  • 举报
回复
顶起来,希望有人回答……
blue601 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huntor 的回复:]
在paint 方法最开始先 super.paint(g);

直接绘制会有闪烁。最好在JPanel绘制,然后添加到内容窗格。
[/Quote]
但还是不执行update()里的程序啊,update()里面有将背景涂成蓝色的语句,但背景不是蓝色的
joshua1203 2010-05-14
  • 打赏
  • 举报
回复
我也遇到这个问题了!!就卡在这里,留个讯息,改天解决了我来看看~~
huntor 2010-05-13
  • 打赏
  • 举报
回复
在paint 方法最开始先 super.paint(g);

直接绘制会有闪烁。最好在JPanel绘制,然后添加到内容窗格。

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧