首页 新闻 会员 周边

applet线程时钟为什么会闪烁呢

0
悬赏园豆:20 [已关闭问题] 关闭于 2013-10-27 15:12

这个是我的applet程序

package shizhong;

import java.awt.*;

import java.applet.*;

import java.util.*;

import java.text.*;

import java.math.*;

public class Clock extends Applet implements Runnable{  

 private Date date;

 private SimpleDateFormat simple;

 private String lastday,today;

 private Color blue,red;

 private Image img;

 private int h,m,s;

 private Thread th;

 public void init(){

  simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

 date = new Date();

  lastday = simple.format(date);   

  img = getImage(getCodeBase(),"shizhong/clock.jpg");

  resize(300,300);

  blue = Color.BLUE;

  red = Color.RED;

 }

 

 public void start(){

  th = new Thread(this);//为什么用this可以,new Clock()不行  

 th.start();

 }

 public void stop(){

  th = null;

 }

 public void run(){   

while(th != null){   

 try{   

  Thread.currentThread().sleep(100);  

  }catch(Exception e){   

  e.printStackTrace();   

 }   

 repaint();  

 }  }  

 public void paint(Graphics g){

  int centerx = 163,centery = 163;

  int hx,hy,mx,my,sx,sy;   

  //super.paint(g);

  g.drawImage(img,10,10,this);

  simple = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");  

 date = new Date();

  today = simple.format(date);

  Calendar cal = new GregorianCalendar();

  h = cal.get(Calendar.HOUR_OF_DAY);  

 m = cal.get(Calendar.MINUTE);

  s = cal.get(Calendar.SECOND);     

 /**   1.这里的cos(h);h并不是角度,而是弧度,计算弧度的公式2*Math.PI*角度/360   2.Math.cos(h);计算出来的值在-1~1之间,还需要乘以半径,再加减中心坐标   3.特别注意y坐标的减   4.x,y坐标的sin和cos写反了,表针会倒转   5.计算时针时12个小时为一个周期,时针每走一小格(6度),分针走12度,   */  

 hx =(int)(centerx + Math.sin(2*Math.PI*30*(h-12)/360-m/12*6)*60);  

 hy =(int)(centery - Math.cos(2*Math.PI*30*(h-12)/360+m/12*6)*60);  

 mx =(int)(centerx + Math.sin(2*Math.PI*6*m/360)*80);

  my =(int)(centery - Math.cos(2*Math.PI*6*m/360)*80);  

 sx =(int)(centerx + Math.sin(2*Math.PI*6*s/360)*100);   

sy =(int)(centery - Math.cos(2*Math.PI*6*s/360)*100);   

  g.setFont(new Font("宋体",Font.BOLD,18));

  g.setColor(blue);  

 g.drawLine(centerx,centery,hx,hy);

  g.setColor(blue);

  g.drawLine(centerx,centery,mx,my);

  g.setColor(red);  

 g.drawLine(centerx,centery,sx,sy);  

    g.setColor(red);

  g.drawString(today,50,400);

  g.drawString(Thread.currentThread().getName(),100,350);//为什么不加currentThread()方法会提示静态方法不能调用非静态方法  

 g.drawString(h+"   "+m+"   "+(2*Math.PI*30*(h-12)/360+m/12*6)+"  "+2*Math.PI*30*(h-12),50,460);

  g.drawString(""+hx+"   "+hy+"   "+mx+"   "+my+"   "+sx+"   "+sy,100,480);   g.drawString("-----孟志兵-----",100,440);     }

 /*public void update(Graphics g){

  paint(g);

 }*/   

}

1.为什么不加update方法运行时,总出现闪烁现象,甚至加了update方法后,sleep()时间改成100时,也会出现闪烁现象?

2.启动线程时new Thread(this);这里的this指什么,能换成其他的吗?

小弟初学applet,望前辈们能讲解的细致一点!!!

001渴望成功的主页 001渴望成功 | 菜鸟二级 | 园豆:202
提问于:2013-10-27 12:31
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册