• 用JAVA实现一个时钟


    用JAVA实现一个时钟

    用图形库绘制表盘,然后用事件处理机制刷新窗口,反复重绘,让表针转动起来
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    import java.awt.geom.Line2D.Double;
    import java.math.*;
    import java.time.LocalTime;
    
    public class MyClock {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		ActionListener listener = new TimerClock();
    		Timer t = new Timer(1000, listener);
    		t.start();
    		//System.exit(0);
    	}
    
    }
    
    class ClockWindow extends JFrame {
    	ClockWindow() {
    		add(new ClockInfo());
    		pack();
    	}
    }
    
    class ClockInfo extends JComponent {
    	private static final int DEFAULT_WIDTH = 500;
    	private static final int DEFAULT_HEIGHT = 500;
    	
    	public void paintComponent(Graphics g) {
    		Graphics2D g2 = (Graphics2D) g;
    		
    		g2.draw(new Ellipse2D.Double(50.0, 50.0, 400.0, 400.0));
    		//圆心为250, 250
    		//绘制小时刻度
    		g2.setPaint(Color.red);
    		double r1 = 190, r2 = 200;
    		double cx = 250.0, cy = 250.0;
    		for(double i = 0; i < 2.0 * Math.PI; i += (Math.PI / 6.0)) {
    			double lx, ly, rx, ry;
    			lx = r1 * Math.sin(i);
    			ly = r1 * Math.cos(i);
    			rx = r2 * Math.sin(i);
    			ry = r2 * Math.cos(i);
    			g2.draw(new Line2D.Double(cx + lx, cy - ly, cx + rx, cy - ry));
    		}
    		//绘制分钟刻度
    		r1 = 195.0;
    		for(double i = 0; i < 2.0 * Math.PI; i += (Math.PI / 30.0)) {
    			double lx, ly, rx, ry;
    			lx = r1 * Math.sin(i);
    			ly = r1 * Math.cos(i);
    			rx = r2 * Math.sin(i);
    			ry = r2 * Math.cos(i);
    			g2.draw(new Line2D.Double(cx + lx, cy - ly, cx + rx, cy - ry));
    		}
    		//绘制指针
    		double hour = LocalTime.now().getHour() * Math.PI / 6.0;
    		double minute = LocalTime.now().getMinute() * Math.PI / 30.0;
    		double second = LocalTime.now().getSecond() * Math.PI / 30.0;
    		//时针
    		g2.setPaint(Color.black);
    		g2.setStroke(new BasicStroke(4.0f));
    		g2.draw(new Line2D.Double(cx, cy, cx + 120.0 * Math.sin(hour), cy - 120.0 * Math.cos(hour)));
    		//分针
    		g2.setPaint(Color.green);
    		g2.setStroke(new BasicStroke(2.0f));
    		g2.draw(new Line2D.Double(cx, cy, cx + 140.0 * Math.sin(minute), cy - 140.0 * Math.cos(minute)));
    		//秒针
    		g2.setPaint(Color.red);
    		g2.setStroke(new BasicStroke(1.0f));
    		g2.draw(new Line2D.Double(cx, cy, cx + 160.0 * Math.sin(second), cy - 160.0 * Math.cos(second)));
    		//文字
    		Font f = new Font("Serif", Font.PLAIN, 30);//逻辑字体
    		g2.setFont(f);
    		g2.setColor(Color.orange);
    		int h = LocalTime.now().getHour();
    		int m = LocalTime.now().getMinute();
    		int s = LocalTime.now().getSecond();
    		g2.drawString("" + h + ":" + m + ":" + s,10,50);
    	}
    	
    	public Dimension getPreferredSize() {
    		return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    	}
    }
    
    class TimerClock implements ActionListener {
    	ClockWindow frame;
    	
    	TimerClock() {
    		frame = new ClockWindow();
    		frame.setTitle("MyClock");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    	
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		frame.repaint();
    	}
    	
    }
    


  • 相关阅读:
    GetForegroundWindow 与 GetActiveWindow 的区别 回复 "delphier" 的问题
    给 TStringGrid 添加鼠标拖动功能 回复 "dxx" 的问题
    Delphi 的编译指令(3): 常用的预定义条件标识符
    Delphi 的编译指令(1): $DEFINE、$UNDEF、$IFDEF、$ELSE、$ENDIF
    用多媒体库 Bass.dll 播放 mp3 [17] : 如何从内存流播放 回复 "小李子子" 的问题
    Delphi 的编译指令(4): 编译指令全表(未完)
    窗口跟随 回复 "heyongan" 的问题
    字符串转换到指定格式的宽字符 回复 "厨师" 的问题
    Dll 使用 PChar 参数的小例子 回复 "linximf" 的问题
    上周热点回顾(5.286.3)
  • 原文地址:https://www.cnblogs.com/LeafLove/p/14643075.html
Copyright © 2020-2023  润新知