Guest_1013
2021-08-12 20:42
采纳率: 83.3%
浏览 54
已结题

Java,swing,awt,关于Graphics图像更新的问题

我正在写一个指针式的时钟:
clockPanel.java:

package JTimer;
import javax.swing.JPanel;
import java.awt.*;
import java.util.Calendar;

public class clockPanel extends JPanel {
    Image im;
    Graphics g;
    
    static final int HOUR_HAND = 50,MINUTE_HAND = 75,SECOND_HAND = 100;
    public clockPanel(Image im) {
        if(im != null) {
            this.im = im;
            this.g = im.getGraphics();
        }
    }
    public void display() {
        if(this.g != null) {
            super.paint(g);
            Calendar c = Calendar.getInstance();
            int current_sec = c.get(Calendar.SECOND);
            boolean is_drawn = false;
            
            this.g.setXORMode(Color.BLACK);
            this.g.setColor(Color.WHITE);
            this.g.drawArc(50,50,300,300,0,360);
            this.g.fillArc(195,195,10,10,0,360);
            
            while(true) {//判定指针是否需要更新,但经测试if语句里头的东西没有用
                
                c = Calendar.getInstance();
                
                if((is_drawn == true) && (current_sec != c.get(Calendar.SECOND))) {
                    draw();
                    current_sec = c.get(Calendar.SECOND);
                    is_drawn = false;
                    this.repaint();
                }
                if(is_drawn = false) {
                    draw();
                    is_drawn = true;
                    this.repaint();
                }
                
                this.repaint();
            }
            
        }
    }
    public void draw() {//画指针
        Calendar c = Calendar.getInstance();
        
        g.drawLine(200,200,(int)(200+this.HOUR_HAND*Math.sin(2*Math.PI*c.get(c.HOUR)/12)),(int)(200-this.HOUR_HAND*Math.cos(2*Math.PI*c.get(c.HOUR)/12)));
        g.drawLine(200,200,(int)(200+this.MINUTE_HAND*Math.sin(2*Math.PI*c.get(c.MINUTE)/60)),(int)(200-this.MINUTE_HAND*Math.cos(2*Math.PI*c.get(c.MINUTE)/60)));
        g.drawLine(200,200,(int)(200+this.SECOND_HAND*Math.sin(2*Math.PI*c.get(c.SECOND)/60)),(int)(200-this.SECOND_HAND*Math.cos(2*Math.PI*c.get(c.SECOND)/60)));
    }
    
    @Override
    public void paint(Graphics g) {
        g.drawImage(im,0,0,this);
    }
    
}

launch.java(main函数在这里):

package JTimer;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class launch {

    public static void main(String[] args) {
        
        JFrame frame = new JFrame("JTimer");
        frame.setSize(417, 440);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 400, 400);
        frame.add(panel);
        placeComponents(panel);
    }
    private static void placeComponents(JPanel panel) {
        panel.setLayout(null);
        Image im = new BufferedImage(400,400,BufferedImage.TYPE_INT_RGB);
        clockPanel clock = new clockPanel(im);
        clock.setBackground(Color.WHITE);
        clock.setBounds(0, 0, 400, 400);
        panel.add(clock);
        clock.display();
    }
}

在测试时候发现没办法看到指针,请问如何解决?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题