亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復時鐘閃爍?

如何修復時鐘閃爍?

慕少森 2023-09-20 16:06:28
我目前正在使用 AWT Package 和 Swing 在 java 中制作模擬時鐘。但數字時鐘覆蓋目前每隔一段時間就會閃爍一次,我想解決這個問題。我讀過有關與 repaint() 結合實現雙緩沖的內容,但我對如何實現它感到困惑。import java.applet.Applet;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.Graphics;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.TimeZone;import java.util.Timer;import java.util.TimerTask;import javax.swing.JFrame;public class Clock2d extends Applet {    GregorianCalendar cal;    Timer clockTimer = new Timer();    TimeZone clockTimeZone = TimeZone.getDefault();    public Clock2d() {        clockTimer.schedule(new TickTimerTask(), 0, 1000);    }    @Override    public void init() {    }    public void paint(Graphics g) {        g.setColor(Color.BLUE);        g.fillOval(40, 40, 220, 220);        g.setColor(Color.WHITE);        g.fillOval(50, 50, 200, 200);        double second = cal.get(Calendar.SECOND);        double minute = cal.get(Calendar.MINUTE);        double hours = cal.get(Calendar.HOUR);        for (int i = 0; i < 60; i++) {            int length = 90;            double rad = (i * 6) * (Math.PI) / 180;            if (i % 5 == 0) {                length = 82;                g.setColor(Color.BLUE);            } else {                g.setColor(Color.GRAY);            }            int x = 150 + (int) (95 * Math.cos(rad - (Math.PI / 2)));            int y = 150 + (int) (95 * Math.sin(rad - (Math.PI / 2)));            int x1 = 150 + (int) (length * Math.cos(rad - (Math.PI / 2)));            int y1 = 150 + (int) (length * Math.sin(rad - (Math.PI / 2)));            g.drawLine(x, y, x1, y1);        }除了編號數字時鐘偶爾閃爍之外,代碼完全按照預期工作。
查看完整描述

1 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

  1. public class Clock2d extends AppletAjava.awt.Applet不僅已被棄用,而且默認情況下也不是雙緩沖的。更改它以擴展 a JPanel(即)。

  2. 然后改為public void paint(Graphics g) {尊重public void paintComponent(Graphics g) { super.paintComponent(g);油漆鏈。

  3. class TickTimerTask extends TimerTask使用 ajavax.swing.Timer代替。它在事件調度線程上運行,對 GUI 的任何調用都應在 EDT 上進行。

這是代碼中表達的這三點。您在這個版本中看到“閃爍”的偽像嗎?

import java.awt.*;

import java.awt.event.*;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.TimeZone;

import javax.swing.*;


public class Clock2d extends JPanel {


    TimeZone clockTimeZone = TimeZone.getDefault();

    GregorianCalendar cal = 

            (GregorianCalendar) GregorianCalendar.getInstance(clockTimeZone);

    ActionListener repaintListener = (ActionEvent e) -> {

        cal = (GregorianCalendar) GregorianCalendar.getInstance(clockTimeZone);

        repaint();

    };

    Timer clockTimer = new Timer(100, repaintListener);


    public Clock2d() {

        clockTimer.start();

    }


    @Override

    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        g.setColor(Color.BLUE);

        g.fillOval(40, 40, 220, 220);

        g.setColor(Color.WHITE);

        g.fillOval(50, 50, 200, 200);


        double second = cal.get(Calendar.SECOND);

        double minute = cal.get(Calendar.MINUTE);

        double hours = cal.get(Calendar.HOUR);


        for (int i = 0; i < 60; i++) {

            int length = 90;

            double rad = (i * 6) * (Math.PI) / 180;

            if (i % 5 == 0) {

                length = 82;

                g.setColor(Color.BLUE);

            } else {

                g.setColor(Color.GRAY);

            }

            int x = 150 + (int) (95 * Math.cos(rad - (Math.PI / 2)));

            int y = 150 + (int) (95 * Math.sin(rad - (Math.PI / 2)));

            int x1 = 150 + (int) (length * Math.cos(rad - (Math.PI / 2)));

            int y1 = 150 + (int) (length * Math.sin(rad - (Math.PI / 2)));

            g.drawLine(x, y, x1, y1);

        }


        drawHands(g, second, minute, hours);


        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");

        g.setColor(Color.BLUE);

        g.setFont(new Font("Tahoma", Font.BOLD, 16));

        g.drawString(sdf.format(cal.getTime()), 120, 20);

        g.setFont(new Font("Arial", Font.BOLD, 10));


    }


    public void drawHands(Graphics g, double second, double minute, double hours) {

        double rSecond = (second * 6) * (Math.PI) / 180;

        double rMinute = ((minute + (second / 60)) * 6) * (Math.PI) / 180;

        double rHours = ((hours + (minute / 60)) * 30) * (Math.PI) / 180;

        g.setColor(Color.RED);

        g.drawLine(150, 150, 150 + (int) (100 * Math.cos(rSecond - (Math.PI / 2))), 150 + (int) (100 * Math.sin(rSecond - (Math.PI / 2))));

        g.setColor(Color.BLACK);

        g.drawLine(150, 150, 150 + (int) (70 * Math.cos(rMinute - (Math.PI / 2))), 150 + (int) (70 * Math.sin((rMinute - (Math.PI / 2)))));

        g.drawLine(150, 150, 150 + (int) (50 * Math.cos(rHours - (Math.PI / 2))), 150 + (int) (50 * Math.sin(rHours - (Math.PI / 2))));

    }


    public static void main(String[] args) {

        // Swing / AWT GUIs should be created & changed on the EDT ..

        Runnable r = () -> {

            JFrame frame = new JFrame("Clock 2D");

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setPreferredSize(new Dimension(330, 330));

            Clock2d clock2d = new Clock2d();

            clock2d.setPreferredSize(new Dimension(320, 320));

            frame.setLayout(new BorderLayout());

            frame.getContentPane().add(clock2d, BorderLayout.CENTER);

            frame.pack();

            frame.setVisible(true);

        };

        // .. this is how we ensure that Runnable is on the EDT.

        SwingUtilities.invokeLater(r);

    }

}

關于字體的更多提示:


g.setFont(new Font("Arial", Font.BOLD, 10));

鑒于 Paint 方法被重復調用,最好在構造類時建立字體(兩者)并將它們存儲為類的屬性(可以在方法中引用)。


但最好使用邏輯字體而不是“Arial”之類的字體,如下所示:


g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 10));

這不僅提供編譯時檢查,而且可以跨操作系統平臺進行調整。例如,該SANS_SERIF字體在 Windows 上會生成 Arial,可能在大多數 *nix 盒子上,但在 OS X 上默認SANS_SERIF(未修飾的)字體是 Helvetica。


另一種字體 Tahoma 的問題更大。同樣,它可能出現在許多 Windows 機器上,但最好要么對其進行測試并擁有備份列表,要么在時鐘應用程序中提供字體。(假設您擁有發行權)。


查看完整回答
反對 回復 2023-09-20
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號