JavaSwing实现动态时钟
Posted 兴奋的大公狗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaSwing实现动态时钟相关的知识,希望对你有一定的参考价值。
代码
package swing.table;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.util.Date;
public class Clock extends Canvas implements ActionListener
static JButton start = new JButton("开始");
static JButton stop = new JButton("停止");
Date date = new Date();
Timer secondTimer;
int hour,minute,second;
Line2D secondLine,minuteLine,hourLine;
int a,b,c;
double[] pointSX = new double[60];
double[] pointSY = new double[60];
double[] pointMX = new double[60];
double[] pointMY = new double[60];
double[] pointHX = new double[60];
double[] pointHY = new double[60];
public Clock()
secondTimer = new Timer(1000,this);
pointSX[0] = 0;
pointSY[0] = -100;
pointMX[0] = 0;
pointMY[0] = -90;
pointHX[0] = 0;
pointHY[0] = -70;
double angle = 6*Math.PI/180;
for (int i = 0; i < 59; i++)
pointSX[i+1]=pointSX[i]*Math.cos(angle)-Math.sin(angle)*pointSY[i];
pointSY[i+1]=pointSY[i]*Math.cos(angle)+pointSX[i]*Math.sin(angle);
pointMX[i+1]=pointMX[i]*Math.cos(angle)-Math.sin(angle)*pointMY[i];
pointMY[i+1]=pointMY[i]*Math.cos(angle)+pointMX[i]*Math.sin(angle);
pointHX[i+1]=pointHX[i]*Math.cos(angle)-Math.sin(angle)*pointHY[i];
pointHY[i+1]=pointHY[i]*Math.cos(angle)+pointHX[i]*Math.sin(angle);
for (int i = 0; i < 60; i++)
pointSX[i] = pointSX[i]+120;
pointSY[i] = pointSY[i]+120;
pointMX[i] = pointMX[i]+120;
pointMY[i] = pointMY[i]+120;
pointHX[i] = pointHX[i]+120;
pointHY[i] = pointHY[i]+120;
secondLine = new Line2D.Double(0,0,0,0);
minuteLine = new Line2D.Double(0,0,0,0);
hourLine = new Line2D.Double(0,0,0,0);
secondTimer.start();
public void paint(Graphics g)
for (int i = 0; i < 60; i++)
int x = (int) pointSX[i];
int y = (int) pointSY[i];
if (i%5==0)
g.setColor(Color.BLACK);
g.fillOval(x-4,y-4,8,8);
else
g.setColor(Color.BLACK);
g.fillOval(x-2,y-2,4,4);
g.fillOval(115,115,10,10);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g.drawOval(20,20,200,200);
g2.draw(secondLine);
BasicStroke bs = new BasicStroke(3f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);
g2.setStroke(bs);
g2.setColor(Color.pink);
g2.draw(minuteLine);
bs = new BasicStroke(6f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);
g2.setStroke(bs);
g2.setColor(Color.cyan);
g2.draw(hourLine);
@Override
public void actionPerformed(ActionEvent e)
if (e.getSource()==secondTimer)
date = new Date();
String s = date.toString();
hour = Integer.parseInt(s.substring(11,13));
minute = Integer.parseInt(s.substring(14,16));
second = Integer.parseInt(s.substring(17,19));
int h = hour%12;
a = second;
b = minute;
c = h*5+minute/12;
secondLine.setLine(120,120,pointSX[a],pointSY[a]);
minuteLine.setLine(120,120,pointMX[b],pointMY[b]);
hourLine.setLine(120,120,pointHX[c],pointHY[c]);
repaint();
if (e.getSource()==start)
secondTimer.start();
if (e.getSource()==stop)
secondTimer.stop();
public static void main(String[] args)
JFrame win = new JFrame("swing.table.Clock");
JPanel jPanel = new JPanel();
jPanel.add(start);
jPanel.add(stop);
Clock clock = new Clock();
start.addActionListener(clock);
stop.addActionListener(clock);
win.add(clock,BorderLayout.CENTER);
win.add(jPanel,"South");
win.setVisible(true);
win.setSize(246,300);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.validate();
效果
shell脚本实现动态时钟
#!/bin/bash
tput civis
while [ 1 ]
do
tput clear
tput cup 3 10
tput setb 0
tput setf 2
echo -e "\e[1;32m$(date "+%Y-%m-%d %H:%M:%S %A")\e[0m"
sleep 1
done
在Xshell中运行会有闪屏,不太正常;在本地运行一切正常
以上是关于JavaSwing实现动态时钟的主要内容,如果未能解决你的问题,请参考以下文章