📄 clockpanel.java
字号:
package edu.swjtu.sist.java.calendar;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 时钟显示类
*
* @author 518lee@gmail.com
* @version 1.0
*/
class ClockPanel extends JPanel implements Runnable, MouseListener
{
private int xcenter = 80, ycenter = 65, hh, mm, ss;
private Thread timer = null;
private Scrollbar scr2 = new Scrollbar();
private JTextField timeField = new JTextField();
int mode = 1;
ClockPanel()
{
setLayout(null);
scr2.setBounds(new Rectangle(130, 135, 12, 18));
timeField.setBounds(new Rectangle(20, 136, 110, 18));
add(scr2);
add(timeField);
//tf_time.setEditable(false);
timeField.setBackground(Color.white);
timeField.addMouseListener(this);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(getBackground());//消除原有图象
g2.setColor(SystemColor.controlLtHighlight);
g2.fillRect(0, 0, 160, 130);
g2.setColor(SystemColor.controlHighlight);
g2.fill3DRect(80, 65, 2, 2, false);//时钟中心
g2.setColor(Color.green);
for (int i = 0; i < 60; i++)
{
double cos = Math.cos((i * 3.14f) / 30);
double sin = Math.sin((i * 3.14f) / 30);
if (i % 5 == 0)//正点用不同色显示
{
g2.setColor(Color.blue);
g2.fill3DRect((int) (xcenter + 60 * sin),
(int) (ycenter - 60 * cos), 3, 3, true);//
}
else
{
g2.setColor(getBackground());
g2.fill3DRect((int) (xcenter + 60 * sin),
(int) (ycenter - 60 * cos), 3, 3, false);//
}
}
//*****************画表盘结束
Calendar now = Calendar.getInstance();
hh = now.get(Calendar.HOUR);
mm = now.get(Calendar.MINUTE);
ss = now.get(Calendar.SECOND);
int xs, ys;
int xh[] = new int[4];//多边形时针的四个顶点横坐标
int yh[] = new int[4];//多边形时针的四个顶点纵坐标
int xm[] = new int[4];//多边形分针的四个顶点横坐标
int ym[] = new int[4];//多边形分针的四个顶点纵坐标
// 以下计算时针4个点的位置
xh[0] = (int) (Math.cos((hh * 30 + mm / 2) * 3.14f / 180 - 3.14f / 2) * 35 + xcenter);
xh[2] = (int) (xcenter - Math.cos((hh * 30 + mm / 2) * 3.14f / 180
- 3.14f / 2) * 10);
xh[1] = (int) (int) (Math.cos((hh * 30 + mm / 2) * 3.14f / 180) * 5 + xcenter);
xh[3] = (int) (xcenter - Math.cos((hh * 30 + mm / 2) * 3.14f / 180) * 5);
yh[0] = (int) (Math.sin((hh * 30 + mm / 2) * 3.14f / 180 - 3.14f / 2) * 35 + ycenter);
yh[2] = (int) (ycenter - Math.sin((hh * 30 + mm / 2) * 3.14f / 180
- 3.14f / 2) * 10);
yh[1] = (int) (Math.sin((hh * 30 + mm / 2) * 3.14f / 180) * 5 + ycenter);
yh[3] = (int) (ycenter - Math.sin((hh * 30 + mm / 2) * 3.14f / 180) * 5);
//以下计算分针4个点的位置
xm[0] = (int) (Math.cos(mm * 3.14f / 30 - 3.14f / 2) * 50 + xcenter);
xm[2] = (int) (xcenter - Math.cos(mm * 3.14f / 30 - 3.14f / 2) * 15);
xm[1] = (int) (int) (Math.cos(mm * 3.14f / 30) * 4 + xcenter);
xm[3] = (int) (xcenter - Math.cos(mm * 3.14f / 30) * 4);
ym[0] = (int) (Math.sin(mm * 3.14f / 30 - 3.14f / 2) * 50 + ycenter);
ym[2] = (int) (ycenter - Math.sin(mm * 3.14f / 30 - 3.14f / 2) * 15);
ym[1] = (int) (Math.sin(mm * 3.14f / 30) * 3 + ycenter);
ym[3] = (int) (ycenter - Math.sin(mm * 3.14f / 30) * 4);
//计算秒针位置
xs = (int) (Math.cos(ss * 3.14f / 30 - 3.14f / 2) * 52 + xcenter);
ys = (int) (Math.sin(ss * 3.14f / 30 - 3.14f / 2) * 52 + ycenter);
g2.setColor(Color.black);
g2.drawPolygon(xh, yh, 4);//画指针轮廓
g2.setColor(Color.green);
g2.fillPolygon(xh, yh, 4);//画指针
g2.setColor(Color.black);
g2.drawPolygon(xm, ym, 4);
g2.setColor(Color.green);
g2.fillPolygon(xm, ym, 4);
g2.drawLine(xs, ys, xcenter, ycenter);
if (now.get(Calendar.AM_PM) == Calendar.PM) hh += 12;
if (mm < 10 && ss < 10) timeField.setText(hh + ":" + "0" + mm + ":"
+ "0" + ss);
else if (mm > 10 && ss < 10) timeField.setText(hh + ":" + mm + ":"
+ "0" + ss);
else if (mm < 10 && ss > 10) timeField.setText(hh + ":" + "0" + mm
+ ":" + ss);
else if (mm > 10 && ss > 10) timeField
.setText(hh + ":" + mm + ":" + ss);
}
public void start() //启动线程
{
if (timer == null)
{
timer = new Thread(this);
timer.start();
}
}
public void stop() //停止线程
{
timer = null;
}
public void run() //每隔一秒钟,刷新一次画面
{
while (timer != null)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
repaint(); //调用paintComponent(Graphics g)方法重画时钟
}
timer = null;
}
public void mouseClicked(MouseEvent e)
{
int i = e.getX();
if (i > 45 && i < 65) mode = 1;
if (i > 65 && i < 85) mode = 2;
if (i > 85 && i < 110) mode = 3;
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -