|
我在applet小程序中写了个文字的走马灯的效果,如果用了双缓冲技术(update(Graphics g)的方法以后,那些文字会重叠在一起。如果不用就不会出现这个问题。但是不用双缓冲技术的话,那个显示的界面会很闪。
怎样才能解决这个问题?
或者给个例子出来
下面是程序的代码:
import java.awt.*;
import java.applet.*;
import java.util.Date;
public class Clock extends Applet implements Runnable
{
Thread clockThread;
Font font;
String s="大 家 好 ! !";
int count=0;
public void init() {
font = new Font("TimesRoman",Font.BOLD,48);
}
public void start()
{
if(clockThread==null)
clockThread=new Thread(this,"Showtime");
clockThread.start();
}
public void run()
{
//while(clockThread!=null)
while(true)
{
repaint();
try{
clockThread.sleep(300);
}
catch(InterruptedException e){}
}
}
public void paint(Graphics g) {
double a,b,c,d,e,f;
int h,i,j,k,l,m;
Date now=new Date();
a=now.getSeconds();
b=now.getMinutes();
c=now.getHours();
d=(a-15)*Math.PI/180*6;
e=(b-15)*Math.PI/180*6;
f=(c-15)*Math.PI/180*6;
h=(int)(Math.sin(d)*65);
i=(int)(Math.cos(d)*65);
j=(int)(Math.sin(e)*54);
k=(int)(Math.cos(e)*54);
l=(int)(Math.sin(f)*44);
m=(int)(Math.cos(f)*44);
g.translate(100,100);
play(getDocumentBase(),"a.wav");
g.drawLine(0,0,i,h);
g.drawLine(0,0,k,j);
g.drawLine(0,0,m,l);
g.drawString(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds(),-10,100);
char[] s1=s.toCharArray();
int n1;
String q=new String();
for(n1=count;n1<s1.length;n1++){
q+=s1[n1];
}
count++;
g.drawString(q,100,100);
if(count==s1.length)
{
count=0;
}
g.drawString(s,100,110);
g.drawString(s,160,120);
}
public void update(Graphics g){//双缓冲技术
paint(g);
}
public void stop()
{
clockThread.stop();
}
}
帮帮看看拉 |
|