代码如下:
package cn.cjl.study0410.Calendar; import java.awt.*; import javax.swing.JFrame; import javax.swing.JLabel; /** * 主类 * * @author lingzi * */ public class Clock extends JFrame { Image offScreenImage; private static final long serialVersionUID = 1L; Rectangle circleinner = new Rectangle(100, 100, 200, 200); Rectangle center = new Rectangle(196, 196, 8, 8); Rectangle circleouter = new Rectangle(80, 80, 240, 240); Rectangle second = new Rectangle(200, 100, 2, 100); // Rectangle center=new Rectangle(200,200,8,8); private int x1 = 200; private int y1 = 120; private int mx = 200; private int my = 140; private int hx = 200; private int hy = 150; private int x2 = 200; private int y2 = 200; /** * 重写update方法 */ public void update(Graphics g){ System.out.println("双缓冲方法调用"); //创建一幅用于双缓冲的、可变的、可在屏幕外绘制的图像。 this.offScreenImage=this.createImage(400,400); //得到缓冲图像的画笔 Graphics offg=this.offScreenImage.getGraphics(); offg.setColor(Color.BLACK); offg.fillRect(0, 0, 400, 400); //调用paint(),将缓冲图像的画笔传入 paint(offg); //将词缓冲图像一次性绘到代表屏幕的Graphics对象,即该方法传入的g上 g.drawImage(offScreenImage,0,0,null); } public Clock() { super(); setTitle("时钟"); setBounds(0, 0, 400, 400); setLayout(new FlowLayout()); // JLabel o12=new JLabel("12"); // o12.setBounds(center.x,center.y,center.width,center.height); // o12.setBackground(Color.GREEN); // o12.setBounds(center.x,center.y-200,center.width,center.height); // o12.setFont(new Font("Harrington",1,20)); // this.add(o12); setVisible(true); // Graphics g=getGraphics(); new PointMove(); } /** * 绘制背景方法 */ public void paint(Graphics g) { super.paint(g); g.setColor(Color.BLACK); g.fillRect(0, 0, 400, 400); g.setColor(Color.DARK_GRAY); g.fillOval(circleouter.x, circleouter.y, circleouter.width, circleouter.height); g.setColor(Color.BLUE); g.fillOval(circleinner.x, circleinner.y, circleinner.width, circleinner.height); g.setColor(Color.RED); g.fillOval(center.x, center.y, center.width, center.height); g.setColor(Color.RED); g.setFont(new Font("Harrington",1,20)); g.setColor(Color.BLACK); g.drawString("12",center.x, center.y - 100 + 16); g.drawString("6",center.x, center.y + 100 - 4); g.drawString("3",center.x + 100 - 4, center.y); g.drawString("9",center.x - 100 + 4, center.y); g.setColor(Color.RED); g.drawLine(x1, y1, x2, y2); g.setColor(Color.YELLOW); g.drawLine(mx, my, x2, y2); g.setColor(Color.DARK_GRAY); g.drawLine(hx, hy, x2, y2); g.setColor(Color.red); //测试线 // g.drawLine((int)(200+50*(Math.sqrt(2))),(int)(200-50*(Math.sqrt(2))), x2, y2); } /** * 继承Runnable接口的内部类,控制指针移动 * * @author lingzi * */ private class PointMove implements Runnable { Thread thread = null; /** * PointMove类构造方法 */ public PointMove() { if (null == thread) { thread = new Thread(this, "秒针移动"); } // 启动线程 thread.start(); } public void run() { int count = 0; int minutecount =1; int hourcount=1; // 无限循环 while (true) { try { repaint(); x1 = (int) (200 + (90 * (Math.sin((2*Math.PI) / 60* count)))); y1 = (int) (200 - (90 * (Math.cos((2*Math.PI) / 60* count)))); System.out.println(count); count++; if(count==60){ mx = (int) (200 + (80 * (Math.sin((2*Math.PI) / 60* minutecount)))); my = (int) (200 - (80 * (Math.cos((2*Math.PI) / 60* minutecount)))); count=1; minutecount++; System.out.print("MINUTE走动1"); } if(minutecount==60){ hx = (int) (200 + (60 * (Math.sin((2*Math.PI) / 60* hourcount)))); hy = (int) (200 - (60 * (Math.cos((2*Math.PI) / 60* hourcount)))); minutecount=1; System.out.print("HOUR走动1"); } Thread.sleep(1000); } catch (Exception e) { break; } } } } /** * 主方法 * * @param args */ public static void main(String[] args) { new Clock(); } }