获取当前时间画出当前时间的表已实现repaint() 求每秒重画Component方法import java.util.*;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;public class Test
{
public static void main(String[] args)
{
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class DrawFrame extends JFrame
{
public DrawFrame()
{
setTitle("clock");
setSize(400, 400);
//向面板添加内容
DrawComponent component = new DrawComponent();
add(component);
}
}class DrawComponent extends JComponent
{
private static final long serialVersionUID = 1L;
private double hour;
private double minute;
private double second;
private double year;
private double month;
private double day;
public DrawComponent()
{
repaint();
}
public void repaint()
{
GregorianCalendar calendar = new GregorianCalendar();
hour = calendar.get(Calendar.HOUR);
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
//画圈
Rectangle2D rect1 = new Rectangle2D.Double(40, 40, 290, 290);
Ellipse2D elli = new Ellipse2D.Double();
Rectangle2D rect2 = new Rectangle2D.Double(30, 30, 310, 310);
Ellipse2D elli2 = new Ellipse2D.Double();
elli.setFrame(rect1);
g2.draw(elli);
elli2.setFrame(rect2);
g2.draw(elli2);
g.translate(185, 185);
Line2D x;
double xPoint;
double yPoint;
double x1Point;
double y1Point;
g2.setPaint(Color.RED);
//秒刻度
for (double i = 0; i < 60; i++)
{
double alpha;
alpha = i / 60 * 2 * Math.PI;
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha);
x1Point = 142 * Math.cos(alpha);
y1Point = 142 * Math.sin(alpha);
x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x);
}
//画小时刻度
Font f = new Font("SansSerif", Font.PLAIN, 15);
g2.setFont(f);
for (double i = 0; i < 12; i++)
{
double alpha;
alpha = i / 12 * 2 * Math.PI;
//刻度终点
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha);
//刻度起点
x1Point = 135 * Math.cos(alpha);
y1Point = 135 * Math.sin(alpha);
x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x);
//画数字
String letter = Integer.toString((int) (i + 1), 10);
int xLetter = (int) (110 * Math.cos(alpha - Math.PI/3));
int yLetter = (int) (110 * Math.sin(alpha - Math.PI/3));
g2.setPaint(Color.BLACK);
g2.drawString(letter, xLetter, yLetter);
}
//画秒针
double alpha = second / 60 * 2 * Math.PI;
xPoint = 100 * Math.cos(alpha - Math.PI/2);
yPoint = 100 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.draw(x);
//画分针
alpha = (minute *60 + second) / 60 / 60 * 2 * Math.PI;
xPoint = 80 * Math.cos(alpha - Math.PI/2);
yPoint = 80 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.BLUE);
g2.draw(x);
//画时针
alpha = (hour * 3600 + minute *60 +second) / 60 / 60 / 12 * 2 * Math.PI;
xPoint = 60 * Math.cos(alpha - Math.PI/2);
yPoint = 60 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.YELLOW);
g2.draw(x);
String times = (int)year + "/" + (int)month +"/" + (int)day + " " + "Hour:" + (int)hour + " " + "Minute:" + (int)minute + " " + "Second:" + (int)second;
g2.setPaint(Color.BLACK);
g2.drawString(times, -125, 170);
}
}
import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;public class Test
{
public static void main(String[] args)
{
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class DrawFrame extends JFrame
{
public DrawFrame()
{
setTitle("clock");
setSize(400, 400);
//向面板添加内容
DrawComponent component = new DrawComponent();
add(component);
}
}class DrawComponent extends JComponent
{
private static final long serialVersionUID = 1L;
private double hour;
private double minute;
private double second;
private double year;
private double month;
private double day;
public DrawComponent()
{
repaint();
}
public void repaint()
{
GregorianCalendar calendar = new GregorianCalendar();
hour = calendar.get(Calendar.HOUR);
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
//画圈
Rectangle2D rect1 = new Rectangle2D.Double(40, 40, 290, 290);
Ellipse2D elli = new Ellipse2D.Double();
Rectangle2D rect2 = new Rectangle2D.Double(30, 30, 310, 310);
Ellipse2D elli2 = new Ellipse2D.Double();
elli.setFrame(rect1);
g2.draw(elli);
elli2.setFrame(rect2);
g2.draw(elli2);
g.translate(185, 185);
Line2D x;
double xPoint;
double yPoint;
double x1Point;
double y1Point;
g2.setPaint(Color.RED);
//秒刻度
for (double i = 0; i < 60; i++)
{
double alpha;
alpha = i / 60 * 2 * Math.PI;
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha);
x1Point = 142 * Math.cos(alpha);
y1Point = 142 * Math.sin(alpha);
x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x);
}
//画小时刻度
Font f = new Font("SansSerif", Font.PLAIN, 15);
g2.setFont(f);
for (double i = 0; i < 12; i++)
{
double alpha;
alpha = i / 12 * 2 * Math.PI;
//刻度终点
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha);
//刻度起点
x1Point = 135 * Math.cos(alpha);
y1Point = 135 * Math.sin(alpha);
x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x);
//画数字
String letter = Integer.toString((int) (i + 1), 10);
int xLetter = (int) (110 * Math.cos(alpha - Math.PI/3));
int yLetter = (int) (110 * Math.sin(alpha - Math.PI/3));
g2.setPaint(Color.BLACK);
g2.drawString(letter, xLetter, yLetter);
}
//画秒针
double alpha = second / 60 * 2 * Math.PI;
xPoint = 100 * Math.cos(alpha - Math.PI/2);
yPoint = 100 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.draw(x);
//画分针
alpha = (minute *60 + second) / 60 / 60 * 2 * Math.PI;
xPoint = 80 * Math.cos(alpha - Math.PI/2);
yPoint = 80 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.BLUE);
g2.draw(x);
//画时针
alpha = (hour * 3600 + minute *60 +second) / 60 / 60 / 12 * 2 * Math.PI;
xPoint = 60 * Math.cos(alpha - Math.PI/2);
yPoint = 60 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.YELLOW);
g2.draw(x);
String times = (int)year + "/" + (int)month +"/" + (int)day + " " + "Hour:" + (int)hour + " " + "Minute:" + (int)minute + " " + "Second:" + (int)second;
g2.setPaint(Color.BLACK);
g2.drawString(times, -125, 170);
}
}
public class TestTimerTask extends TimerTask {
/**
* 此计时器任务要执行的操作。
*/
public void run() {
//我只想在这里repaint 而不是重新构造一次frame 这个不能传参数进来啊
}
}
package stu.timer; import java.util.Timer;
import java.util.TimerTask; /**
* 测试JDK Timer的执行
*
* @author leizhimin,2008-10-9 9:24:35
*/
public class TestTimer {
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask task = new TestTimerTask();
timer.schedule(task, 500L, 1000L);
}
}
import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;public class Test
{
public static void main(String[] args)
{
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try
{
System.out.println("I'm going to bed");
Thread.sleep(1000);
frame.repaint();
System.out.println("I wake up");
}
catch(InterruptedException e)
{
}
}
}class DrawFrame extends JFrame
{
private DrawComponent component;
public DrawFrame()
{
setTitle("clock");
setSize(400, 400);
//向面板添加内容
DrawComponent component = new DrawComponent();
add(component);
}
public void repaint()
{
repaint();
}
}class DrawComponent extends JComponent
{
public void paintComponent(Graphics g)
{
double hour;
double minute;
double second;
double year;
double month;
double day;
GregorianCalendar calendar = new GregorianCalendar();
hour = calendar.get(Calendar.HOUR);
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
Graphics2D g2 = (Graphics2D) g;
//画圈
Rectangle2D rect1 = new Rectangle2D.Double(40, 40, 290, 290);
Ellipse2D elli = new Ellipse2D.Double();
Rectangle2D rect2 = new Rectangle2D.Double(30, 30, 310, 310);
Ellipse2D elli2 = new Ellipse2D.Double();
elli.setFrame(rect1);
g2.draw(elli);
elli2.setFrame(rect2);
g2.draw(elli2);
g.translate(185, 185);
Line2D x;
double xPoint;
double yPoint;
double x1Point;
double y1Point;
g2.setPaint(Color.RED);
//秒刻度
for (double i = 0; i < 60; i++)
{
double alpha;
alpha = i / 60 * 2 * Math.PI;
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha);
x1Point = 142 * Math.cos(alpha);
y1Point = 142 * Math.sin(alpha);
x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x);
}
//画小时刻度
Font f = new Font("SansSerif", Font.PLAIN, 15);
g2.setFont(f);
for (double i = 0; i < 12; i++)
{
double alpha;
alpha = i / 12 * 2 * Math.PI;
//刻度终点
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha);
//刻度起点
x1Point = 135 * Math.cos(alpha);
y1Point = 135 * Math.sin(alpha);
x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x);
//画数字
String letter = Integer.toString((int) (i + 1), 10);
int xLetter = (int) (110 * Math.cos(alpha - Math.PI/3));
int yLetter = (int) (110 * Math.sin(alpha - Math.PI/3));
g2.setPaint(Color.BLACK);
g2.drawString(letter, xLetter, yLetter);
}
//画秒针
double alpha = second / 60 * 2 * Math.PI;
xPoint = 100 * Math.cos(alpha - Math.PI/2);
yPoint = 100 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.draw(x);
//画分针
alpha = (minute *60 + second) / 60 / 60 * 2 * Math.PI;
xPoint = 80 * Math.cos(alpha - Math.PI/2);
yPoint = 80 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.BLUE);
g2.draw(x);
//画时针
alpha = (hour * 3600 + minute *60 +second) / 60 / 60 / 12 * 2 * Math.PI;
xPoint = 60 * Math.cos(alpha - Math.PI/2);
yPoint = 60 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.YELLOW);
g2.draw(x);
String times = (int)year + "/" + (int)month +"/" + (int)day + " " + "Hour:" + (int)hour + " " + "Minute:" + (int)minute + " " + "Second:" + (int)second;
g2.setPaint(Color.BLACK);
g2.drawString(times, -125, 170);
}
}
repaint(1);//这个地方必须马上刷新 否则白屏。import java.util.*;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;public class Test
{
public static void main(String[] args)
{
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
while (true)
{
try
{
Thread.sleep(1000);
frame.repaint();
}
catch(InterruptedException e)
{
}
}
}
}class DrawFrame extends JFrame
{
private static final long serialVersionUID = -8727404225196903743L;
public DrawFrame()
{
setTitle("clock");
setSize(420, 480);
DrawComponent component = new DrawComponent();
add(component);
}
public void repaint()
{
repaint(1);//这个地方必须马上刷新!!!!!!!!!!
}
}class DrawComponent extends JComponent
{
private static final long serialVersionUID = -1587341236735845047L; public void paintComponent(Graphics g)
{
double hour;
double minute;
double second;
double year;
double month;
double day;
GregorianCalendar calendar = new GregorianCalendar();
hour = calendar.get(Calendar.HOUR);
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
Graphics2D g2 = (Graphics2D) g;
//画圈
Rectangle2D rect1 = new Rectangle2D.Double(40, 40, 290, 290);
Ellipse2D elli = new Ellipse2D.Double();
Rectangle2D rect2 = new Rectangle2D.Double(30, 30, 310, 310);
Ellipse2D elli2 = new Ellipse2D.Double();
elli.setFrame(rect1);
g2.draw(elli);
elli2.setFrame(rect2);
g2.draw(elli2);
g.translate(185, 185);
Line2D x;
double xPoint;
double yPoint;
double x1Point;
double y1Point;
g2.setPaint(Color.RED);
//秒刻度
for (double i = 0; i < 60; i++)
{
double alpha;
alpha = i / 60 * 2 * Math.PI;
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha);
x1Point = 142 * Math.cos(alpha);
y1Point = 142 * Math.sin(alpha);
x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x);
}
//画小时刻度
Font f = new Font("宋体", Font.PLAIN, 15);
g2.setFont(f);
for (double i = 0; i < 12; i++)
{
double alpha;
alpha = i / 12 * 2 * Math.PI;
//刻度终点
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha);
//刻度起点
x1Point = 135 * Math.cos(alpha);
y1Point = 135 * Math.sin(alpha);
x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x);
//画数字
String letter = Integer.toString((int) (i + 1), 10);
int xLetter = (int) (110 * Math.cos(alpha - Math.PI/3));
int yLetter = (int) (110 * Math.sin(alpha - Math.PI/3));
g2.setPaint(Color.BLACK);
g2.drawString(letter, xLetter, yLetter);
}
//画秒针
double alpha = second / 60 * 2 * Math.PI;
xPoint = 100 * Math.cos(alpha - Math.PI/2);
yPoint = 100 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.draw(x);
//画分针
alpha = (minute *60 + second) / 60 / 60 * 2 * Math.PI;
xPoint = 80 * Math.cos(alpha - Math.PI/2);
yPoint = 80 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.BLUE);
g2.draw(x);
//画时针
alpha = (hour * 3600 + minute *60 +second) / 60 / 60 / 12 * 2 * Math.PI;
xPoint = 60 * Math.cos(alpha - Math.PI/2);
yPoint = 60 * Math.sin(alpha - Math.PI/2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.RED);
g2.draw(x);
String times = "Date :" + (int)year + "/" + (int)month +"/" + (int)day + " " + "Hour:" + (int)hour + " " + "Minute:" + (int)minute + " " + "Second:" + (int)second;
g2.setPaint(Color.BLACK);
g2.drawString(times, -150, 185);
g2.drawString("如果显示时间与实际时间不同(相差8小时)", -145, 215);
g2.drawString("可能是注册表里的Time Zones值被修改或者其他原因", -160, 235);
}
}
import java.util.*;
import java.util.Timer;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;public class Test {
public static void main(String[] args) {
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); }
}class DrawFrame extends JFrame {
public DrawFrame() {
setTitle("clock");
setSize(400, 400); // 向面板添加内容 DrawComponent component = new DrawComponent();
add(component);
}
}class DrawComponent extends JComponent {
private static final long serialVersionUID = 1L;
private double hour;
private double minute;
private double second;
private double year;
private double month;
private double day; class PaintTimerTask extends TimerTask {
@Override
public void run() {
repaint();
} } public DrawComponent() {
repaint(); Timer paintTimer = new Timer();
paintTimer.schedule(new PaintTimerTask(), 1000, 1000);
} public void repaint() {
GregorianCalendar calendar = new GregorianCalendar();
hour = calendar.get(Calendar.HOUR);
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
super.repaint(); } public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g; // 画圈
Rectangle2D rect1 = new Rectangle2D.Double(40, 40, 290, 290);
Ellipse2D elli = new Ellipse2D.Double(); Rectangle2D rect2 = new Rectangle2D.Double(30, 30, 310, 310);
Ellipse2D elli2 = new Ellipse2D.Double(); elli.setFrame(rect1);
g2.draw(elli); elli2.setFrame(rect2);
g2.draw(elli2);
g.translate(185, 185); Line2D x; double xPoint;
double yPoint;
double x1Point;
double y1Point;
g2.setPaint(Color.RED);
// 秒刻度
for (double i = 0; i < 60; i++) {
double alpha;
alpha = i / 60 * 2 * Math.PI;
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha);
x1Point = 142 * Math.cos(alpha);
y1Point = 142 * Math.sin(alpha);
x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x);
} // 画小时刻度
Font f = new Font("SansSerif", Font.PLAIN, 15);
g2.setFont(f);
for (double i = 0; i < 12; i++) {
double alpha;
alpha = i / 12 * 2 * Math.PI; // 刻度终点
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha); // 刻度起点
x1Point = 135 * Math.cos(alpha);
y1Point = 135 * Math.sin(alpha); x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x); // 画数字
String letter = Integer.toString((int) (i + 1), 10);
int xLetter = (int) (110 * Math.cos(alpha - Math.PI / 3));
int yLetter = (int) (110 * Math.sin(alpha - Math.PI / 3)); g2.setPaint(Color.BLACK);
g2.drawString(letter, xLetter, yLetter);
} // 画秒针
double alpha = second / 60 * 2 * Math.PI;
xPoint = 100 * Math.cos(alpha - Math.PI / 2);
yPoint = 100 * Math.sin(alpha - Math.PI / 2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.draw(x); // 画分针
alpha = (minute * 60 + second) / 60 / 60 * 2 * Math.PI;
xPoint = 80 * Math.cos(alpha - Math.PI / 2);
yPoint = 80 * Math.sin(alpha - Math.PI / 2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.BLUE);
g2.draw(x); // 画时针
alpha = (hour * 3600 + minute * 60 + second) / 60 / 60 / 12 * 2 * Math.PI;
xPoint = 60 * Math.cos(alpha - Math.PI / 2);
yPoint = 60 * Math.sin(alpha - Math.PI / 2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.YELLOW);
g2.draw(x); String times = (int) year + "/" + (int) month + "/" + (int) day + " " + "Hour:" + (int) hour + " "
+ "Minute:" + (int) minute + " " + "Second:" + (int) second;
g2.setPaint(Color.BLACK);
g2.drawString(times, -125, 170);
}
}
其实你的repaint方法可以删除,把里面的代码移到paintComponent方法中。如下代码是我简单重构后的,你看看吧:import java.util.*;
import java.util.Timer;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;public class Test {
public static void main(String[] args) {
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); }
}class DrawFrame extends JFrame {
public DrawFrame() {
setTitle("clock");
setSize(400, 400); // 向面板添加内容 DrawComponent component = new DrawComponent();
add(component);
}
}class DrawComponent extends JComponent {
private static final long serialVersionUID = 1L;
private double hour;
private double minute;
private double second;
private double year;
private double month;
private double day;
Calendar calendar = Calendar.getInstance();//Calender不要每次都创建 class PaintTimerTask extends TimerTask { @Override
public void run() {
repaint();
} } public DrawComponent() {
repaint(); Timer paintTimer = new Timer();
paintTimer.schedule(new PaintTimerTask(), 1000, 1000);
} public void paintComponent(Graphics g) {
calendar.setTime(new Date());
hour = calendar.get(Calendar.HOUR);
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(hour);
System.out.println(minute);
System.out.println(second); Graphics2D g2 = (Graphics2D) g; // 画圈
Rectangle2D rect1 = new Rectangle2D.Double(40, 40, 290, 290);
Ellipse2D elli = new Ellipse2D.Double(); Rectangle2D rect2 = new Rectangle2D.Double(30, 30, 310, 310);
Ellipse2D elli2 = new Ellipse2D.Double(); elli.setFrame(rect1);
g2.draw(elli); elli2.setFrame(rect2);
g2.draw(elli2);
g.translate(185, 185); Line2D x; double xPoint;
double yPoint;
double x1Point;
double y1Point;
g2.setPaint(Color.RED);
// 秒刻度
for (double i = 0; i < 60; i++) {
double alpha;
alpha = i / 60 * 2 * Math.PI;
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha);
x1Point = 142 * Math.cos(alpha);
y1Point = 142 * Math.sin(alpha);
x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x);
} // 画小时刻度
Font f = new Font("SansSerif", Font.PLAIN, 15);
g2.setFont(f);
for (double i = 0; i < 12; i++) {
double alpha;
alpha = i / 12 * 2 * Math.PI; // 刻度终点
xPoint = 145 * Math.cos(alpha);
yPoint = 145 * Math.sin(alpha); // 刻度起点
x1Point = 135 * Math.cos(alpha);
y1Point = 135 * Math.sin(alpha); x = new Line2D.Double(xPoint, yPoint, x1Point, y1Point);
g2.draw(x); // 画数字
String letter = Integer.toString((int) (i + 1), 10);
int xLetter = (int) (110 * Math.cos(alpha - Math.PI / 3));
int yLetter = (int) (110 * Math.sin(alpha - Math.PI / 3)); g2.setPaint(Color.BLACK);
g2.drawString(letter, xLetter, yLetter);
} // 画秒针
double alpha = second / 60 * 2 * Math.PI;
xPoint = 100 * Math.cos(alpha - Math.PI / 2);
yPoint = 100 * Math.sin(alpha - Math.PI / 2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.draw(x); // 画分针
alpha = (minute * 60 + second) / 60 / 60 * 2 * Math.PI;
xPoint = 80 * Math.cos(alpha - Math.PI / 2);
yPoint = 80 * Math.sin(alpha - Math.PI / 2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.BLUE);
g2.draw(x); // 画时针
alpha = (hour * 3600 + minute * 60 + second) / 60 / 60 / 12 * 2 * Math.PI;
xPoint = 60 * Math.cos(alpha - Math.PI / 2);
yPoint = 60 * Math.sin(alpha - Math.PI / 2);
x = new Line2D.Double(0, 0, xPoint, yPoint);
g2.setPaint(Color.YELLOW);
g2.draw(x); String times = (int) year + "/" + (int) month + "/" + (int) day + " " + "Hour:" + (int) hour + " "
+ "Minute:" + (int) minute + " " + "Second:" + (int) second;
g2.setPaint(Color.BLACK);
g2.drawString(times, -125, 170);
}
}