首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

处理Applet闪烁有关问题

2011-11-24 
处理Applet闪烁问题这是我的代码public class myapplet extends Applet {public void paint(Graphics g) {

处理Applet闪烁问题
这是我的代码

public class myapplet extends Applet {

public void paint(Graphics g) {

Date date=new Date();
g.drawString(date.toString(),20,50);
repaint(1000);
}
public void update(Graphics g){ paint(g); } 
}

重写update解决了闪烁问题 但是画面没有刷新 新的画面和原先的都重叠在一起了 请问怎么解决啊?

[解决办法]
用双缓冲

import java.applet.*;
import java.util.*;
import java.awt.*;

public class myapplet extends Applet 

private Image bgImage;
private Graphics bg;

public void paint(Graphics g) 

Date date=new Date(); 
g.drawString(date.toString(),20,50); 
repaint(1000); 
}
 
public void update (Graphics g)
{
if (bgImage == null)
{
bgImage = createImage (this.getSize().width, this.getSize().height);
bg = bgImage.getGraphics ();
}
bg.setColor (getBackground ());
bg.fillRect (0, 0, this.getSize().width, this.getSize().height);
bg.setColor (getForeground());
paint (bg);
g.drawImage (bgImage, 0, 0, this);
}

}
[解决办法]
参考这个:在 paint 里调用 repaint(1000) AWT 线程得忙死。

Java code
import java.util.Date;import java.io.PrintWriter;import java.io.StringWriter;import java.awt.Font;import java.awt.BorderLayout;import java.awt.event.MouseEvent;import java.awt.event.MouseAdapter;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.SwingConstants;/** * TimerFrame - 倒计时窗口 * @author SageZk * @version 1.0 */@SuppressWarnings("serial")public class TimerFrame extends JFrame {    private long time = (long) (1.2 * 60 * 1000L);  //倒计时时间(单位毫秒)    private JLabel lblTime;    private Thread runner;    public TimerFrame() {        super("TimerFrame");        this.lblTime = new JLabel("单击开始");        this.lblTime.setFont(new Font("Monospaced", Font.BOLD, 60));        this.lblTime.setHorizontalAlignment(SwingConstants.CENTER);        this.lblTime.addMouseListener(new MouseAdapter() {            public void mouseClicked(MouseEvent e) {                if (runner != null) runner.start();            }        });        this.runner = new Thread(new Runnable() {            public void run() {                Date t = new Date();                StringWriter sw = new StringWriter();                StringBuffer sb = sw.getBuffer();                PrintWriter pw = new PrintWriter(sw);                long cur = 0L, end = System.currentTimeMillis() + time;                while ((cur = end - System.currentTimeMillis()) > 0) {                    t.setTime(cur);                    pw.format("%1$tM:%1$tS.%tL", t);                    pw.flush();                    lblTime.setText(sb.toString());                    sb.setLength(0);                    try {                        Thread.sleep(6L);                    } catch (InterruptedException e) {                    }                }                lblTime.setText("00:00.000");                try {                    Thread.sleep(1200L);                } catch (InterruptedException e) {                }                lblTime.setText("Bomb!!!");            }        });        this.runner.setDaemon(true);        getContentPane().add(this.lblTime, BorderLayout.CENTER);        setResizable(false);        setBounds(0, 0, 460, 330);        setLocationRelativeTo(null);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    public static void main(String[] args) {        new TimerFrame().setVisible(true);    }} 

热点排行