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

使用了双缓冲,图片还是会闪烁

2012-07-16 
使用了双缓冲,图片还是会闪烁,求助Java codepackage Forth//一个模拟时钟的程序,下面的是面板上的内容imp

使用了双缓冲,图片还是会闪烁,求助

Java code
package Forth;//一个模拟时钟的程序,下面的是面板上的内容import java.awt.*;import java.util.*;import java.util.concurrent.TimeUnit;import javax.swing.*;public class Clock extends JPanel implements Runnable {    private JPanel numberPanel = new JPanel(); //用来绘制纯数字的时间    private JLabel numberFormatLabel = new JLabel();//用来存放数字内容的Label    private JPanel clockPanel = new JPanel();//用来绘制模拟时钟的面板    private final Point R = new Point(230, 220);    private final int RADIUS = 200;    private final int SECOND_LENGTH = 180;    private final int MINITE_LENGTH = 130;    private final int HOUR_LENGTH = 100;    private Point minute = new Point();    private Point second = new Point();    private Point hour = new Point();    private Image offScreenImage = null;    public Clock() {        super();        this.setLayout(new BorderLayout());        numberFormatLabel.setFont(new Font("微软雅黑", Font.BOLD, 33));        numberFormatLabel.setForeground(Color.pink);        numberFormatLabel.setHorizontalAlignment(SwingConstants.CENTER);        numberFormatLabel.setHorizontalTextPosition(SwingConstants.CENTER);        numberPanel.add(numberFormatLabel);                        this.add(numberPanel, BorderLayout.NORTH);//存放数字的面板放在上面        this.add(clockPanel, BorderLayout.CENTER);//存放模拟时钟的面版放在下面    }    @Override//绘制时钟的部分    public void update(Graphics g) {        super.update(g);        if (this.offScreenImage == null) {            offScreenImage = clockPanel.createImage(500, 500);        }        Graphics2D g2 = (Graphics2D) offScreenImage.getGraphics();        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,                RenderingHints.VALUE_ANTIALIAS_ON);        g2.setColor(Color.CYAN);        g2.fillOval(R.x - this.RADIUS, R.y - this.RADIUS, this.RADIUS * 2,                this.RADIUS * 2);        g2.setColor(Color.green);        g2.fillOval(R.x - 10, R.y - 10, 20, 20);        g2.setColor(Color.red);        g2.drawLine(R.x, R.y, second.x, second.y);        g2.setColor(Color.BLUE);        g2.drawLine(R.x, R.y, minute.x, minute.y);        g2.setColor(Color.magenta);        g2.drawLine(R.x, R.y, hour.x, hour.y);        //paint(g2);        g.drawImage(offScreenImage, 0, 0, null);    }    public void run() {        while (true) {            try {                Date date = new Date();                String timeStr = String.format("%tH:%tM:%tS %tp", date, date,                        date, date);                numberFormatLabel.setText(timeStr);                Calendar calendar = Calendar.getInstance();                int s = calendar.get(Calendar.SECOND);                int m = calendar.get(Calendar.MINUTE);                int h = calendar.get(Calendar.HOUR);                                     //用来计算时钟的坐标                System.out.println(s);                second.x = (int) (R.x + this.SECOND_LENGTH                        * Math.sin(s * Math.PI / 30));                second.y = (int) (R.y - this.SECOND_LENGTH                        * Math.cos(s * Math.PI / 30));                minute.x = (int) (R.x + this.MINITE_LENGTH                        * Math.sin(m * Math.PI / 30));                minute.y = (int) (R.y - this.MINITE_LENGTH                        * Math.cos(m * Math.PI / 30));                hour.x = (int) (R.x + this.HOUR_LENGTH                        * Math.sin(h * Math.PI / 6));                hour.y = (int) (R.y - this.HOUR_LENGTH                        * Math.cos(h * Math.PI / 6));                this.update(clockPanel.getGraphics());                TimeUnit.SECONDS.sleep(1);            } catch (Exception e) {                e.printStackTrace();            }        }    }}

还是会闪烁,不知道怎么回事,

[解决办法]
在事件派发线程中执行clockPanel.getGraphics()
也就是SwingUtilities.invokeLater中执行。


[解决办法]
SwingUtilities.invokelator(new Runnable());
他会自动帮你启动线程

热点排行