1.clock_demo1.java,时间有点不准,望指正
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class clock_demo1 extends JFrame { private static final long serialVersionUID = 6790815213225162093L; Timer timer; int x, y, old_X, old_Y, r, x0, y0, w, h, ang; int sdo, mdo, hdo, old_M, old_H; // TimeZone tz =TimeZone.getTimeZone("JST"); TimeZone tz = TimeZone.getDefault(); final double RAD = Math.PI / 180.0; @SuppressWarnings("deprecation") public clock_demo1() { super("[HappyChat]钟"); setSize(300, 300); setBackground(new Color(0, 0, 192)); setResizable(false); Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();// ����Ļ������ʾ Dimension fra = this.getSize(); if (fra.width > scr.width) { fra.width = scr.width; } if (fra.height > scr.height) { fra.height = scr.height; } this.setLocation((scr.width - fra.width) / 2, (scr.height - fra.height) / 2); show(); int delay = 1000; // ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { repaint(); } }; new Timer(delay, taskPerformer).start(); } public void actionPerformed(ActionEvent e) { timer.restart(); } public void paint(Graphics g) { Insets insets = getInsets(); int L0 = (insets.left) / 2, T0 = (insets.top) / 2; int hh, mm, ss; String st; h = getSize().height; // g.setColor(Color.white); g.drawOval(L0 + 30, T0 + 30, h - 60, h - 60); g.drawOval(L0 + 32, T0 + 32, h - 64, h - 64); r = h / 2 - 30; x0 = 30 + r - 5 + L0; y0 = 30 + r - 5 - T0; ang = 60; for (int i = 1; i <= 12; i++) { x = (int) ((r + 10) * Math.cos(RAD * ang) + x0); y = (int) ((r + 10) * Math.sin(RAD * ang) + y0); g.drawString("" + i, x, h - y); ang -= 30; } x0 = 30 + r + L0; y0 = 30 + r + T0; // Calendar now = Calendar.getInstance(); hh = now.get(Calendar.HOUR_OF_DAY);// mm = now.get(Calendar.MINUTE);// ss = now.get(Calendar.SECOND);// g.setColor(Color.red); g.fillRect(L0, T0, 60, 28);// g.setColor(Color.blue); if (hh < 10) st = "0" + hh; else st = "" + hh; if (mm < 10) st = st + ":0" + mm; else st = st + ":" + mm; if (ss < 10) st = st + ":0" + ss; else st = st + ":" + ss; g.drawString(st, L0, T0 + 25); // sdo = 90 - ss * 6; mdo = 90 - mm * 6; hdo = 90 - hh * 30 - mm / 2; // if (old_X > 0) { g.setColor(getBackground()); g.drawLine(x0, y0, old_X, (h - old_Y)); } else { old_M = mdo; old_H = hdo; } // g.setColor(Color.yellow); x = (int) ((r - 8) * Math.cos(RAD * sdo) + x0); y = (int) ((r - 8) * Math.sin(RAD * sdo) + y0) - 2 * T0; g.drawLine(x0, y0, x, (h - y)); old_X = x; old_Y = y; // if (mdo != old_M) { line(g, old_M, (int) (r * 0.7), getBackground()); old_M = mdo; } if (hdo != old_H) { line(g, old_H, (int) (r * 0.5), getBackground()); old_H = hdo; } // line(g, mdo, (int) (r * 0.7), Color.green); // line(g, hdo, (int) (r * 0.5), Color.red); } // end paint public void line(Graphics g, int t, int n, Color c) { int[] xp = new int[4]; int[] yp = new int[4]; xp[0] = x0; yp[0] = y0; xp[1] = (int) ((n - 10) * Math.cos(RAD * (t - 4)) + x0); yp[1] = h - (int) ((n - 10) * Math.sin(RAD * (t - 4)) + y0); xp[2] = (int) (n * Math.cos(RAD * t) + x0); yp[2] = h - (int) (n * Math.sin(RAD * t) + y0); xp[3] = (int) ((n - 10) * Math.cos(RAD * (t + 4)) + x0); yp[3] = h - (int) ((n - 10) * Math.sin(RAD * (t + 4)) + y0); g.setColor(c); g.fillPolygon(xp, yp, 4); } public static void main(String[] args) { new clock_demo1(); } }
2.clock_demo2.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@SuppressWarnings("serial") public class clock_demo2 extends JFrame { clock_demo2() { MyPanel1 my = new MyPanel1(); this.add(my); Thread t = new Thread(my); t.start(); /* * this.addMouseListener(new MouseAdapter() { public void * mouseExited(MouseEvent e) { System.exit(0); } }); */ // Frame this.setTitle("DIVʱ��"); this.setResizable(false); this.setAlwaysOnTop(true); this.setVisible(true); this.setSize(215, 240); this.setDefaultCloseOperation(3); } public static void main(String[] args) { //int x=0b10100012210; new clock_demo2(); } } @SuppressWarnings("serial") class MyPanel1 extends JPanel implements Runnable { double x1 = 105, y1 = 105, x2 = 105, y2 = 105, x3 = 105, y3 = 105; static double a, b, r, d;// Line2D.Double ld1, ld2, ld3, ld4, ld5, ld6, ld0, ldx1, ldx2, ldx3;// MyPanel1() {// this(0, 0, 200); this.setSize(200, 200); this.setBackground(new Color(200, 0, 200)); this.setLayout(null); ld1 = new Line2D.Double(a - r, b, a + r, b);// ld2 = new Line2D.Double(a - (Math.sqrt(3) / 2) * r, b - r / 2, a// + (Math.sqrt(3) / 2) * r, b + r / 2); ld3 = new Line2D.Double(a - r / 2, b - (Math.sqrt(3) / 2) * r, a + r//5 / 2, b + (Math.sqrt(3) / 2) * r); ld4 = new Line2D.Double(a, b - r, a, b + r);//6 12 ld5 = new Line2D.Double(a + r / 2, b - (Math.sqrt(3) / 2) * r, a - r / 2, b + (Math.sqrt(3) / 2) * r);//1 7 ld6 = new Line2D.Double(a - (Math.sqrt(3) / 2) * r, b + r / 2, a + (Math.sqrt(3) / 2) * r, b - r / 2);//2 8 } @SuppressWarnings("static-access") MyPanel1(double a, double b, double d) {//(a,b) this.d = d; this.r = d / 2; this.a = a + r + 5; this.b = b + r + 5; } public void paint(Graphics g) {// super.paint(g); Graphics2D gg = (Graphics2D) g; Stroke stroke = new BasicStroke(2.0f);// gg.setStroke(stroke); // System.out.println(a + " " + b + " " + r + " " + d); Ellipse2D.Double e = new Ellipse2D.Double(a - r, b - r, d, d); gg.draw(e); gg.draw(ld1); gg.draw(ld2); gg.draw(ld3); gg.draw(ld4); gg.draw(ld5); gg.draw(ld6); // g.setColor(new Color(200, 200, 200)); g.fillOval((int) a - 90, (int) a - 90, 180, 180);// // g.setColor(Color.BLACK); stroke = new BasicStroke(4.0f); gg.setStroke(stroke); ld0 = new Line2D.Double(a, b, a, b); gg.draw(ld0); // g.setColor(Color.RED); stroke = new BasicStroke(4.0f); gg.setStroke(stroke); ldx1 = new Line2D.Double(x1, y1, a, b); gg.draw(ldx1); // g.setColor(new Color(0, 172, 86)); stroke = new BasicStroke(2f); gg.setStroke(stroke); ldx2 = new Line2D.Double(x2, y2, a, b); gg.draw(ldx2); // g.setColor(Color.BLUE); stroke = new BasicStroke(1.5f); gg.setStroke(stroke); ldx3 = new Line2D.Double(x3, y3, a, b); gg.draw(ldx3); // g.setFont(new Font("华文新魏", Font.BOLD, 16)); g.setColor(new Color(120, 40, 110)); g.drawString("华农制作,我参考", 72, 140); } int hour, minute, second; double hour_r, minute_r, second_r;// public void run() { while (true) { try { Thread.sleep(1000); // Calendar c = Calendar.getInstance(); hour = c.get(Calendar.HOUR_OF_DAY+1); minute = c.get(Calendar.MINUTE); second = c.get(Calendar.SECOND); if (hour > 11) { hour -= 12; } hour_r = r / 2; minute_r = r * 0.8; second_r = r; hourPack(); minutePack(); secondPack(); } catch (InterruptedException e) { JOptionPane.showMessageDialog(null, e.toString()); } } } // public void hourPack() { if (hour >= 0 && hour < 3) { x1 = a + (Math.sin(Math.toRadians(hour * 30))) * hour_r; y1 = b - (Math.cos(Math.toRadians(hour * 30))) * hour_r; repaint(); } else if (hour >= 3 && hour < 6) { x1 = a + (Math.sin(Math.toRadians((3 - hour) * 30))) * hour_r; y1 = b + (Math.cos(Math.toRadians((3 - hour) * 30))) * hour_r; repaint(); } else if (hour >= 6 && hour < 9) { x1 = a - (Math.cos(Math.toRadians((6 - hour) * 30))) * hour_r; y1 = b + (Math.sin(Math.toRadians((6 - hour) * 30))) * hour_r; repaint(); } else { x1 = a - (Math.sin(Math.toRadians((12 - hour) * 30))) * hour_r; y1 = b - (Math.cos(Math.toRadians((12 - hour) * 30))) * hour_r; repaint(); } } // public void minutePack() { if (minute >= 0 && minute < 15) { x2 = a + (Math.sin(Math.toRadians(minute * 6))) * minute_r; y2 = b - (Math.cos(Math.toRadians(minute * 6))) * minute_r; repaint(); } else if (minute >= 15 && minute < 30) { x2 = a + (Math.sin(Math.toRadians((30 - minute) * 6))) * minute_r; y2 = b + (Math.cos(Math.toRadians((30 - minute) * 6))) * minute_r; repaint(); } else if (minute >= 30 && minute < 45) { x2 = a - (Math.cos(Math.toRadians((45 - minute) * 6))) * minute_r; y2 = b + (Math.sin(Math.toRadians((45 - minute) * 6))) * minute_r; repaint(); } else { x2 = a - (Math.sin(Math.toRadians((60 - minute) * 6))) * minute_r; y2 = b - (Math.cos(Math.toRadians((60 - minute) * 6))) * minute_r; repaint(); } } // public void secondPack() { if (second >= 0 && second < 15) { x3 = a + (Math.sin(Math.toRadians(second * 6))) * r; y3 = b - (Math.cos(Math.toRadians(second * 6))) * r; repaint(); } else if (second >= 15 && second < 30) { x3 = a + (Math.sin(Math.toRadians((30 - second) * 6))) * r; y3 = b + (Math.cos(Math.toRadians((30 - second) * 6))) * r; repaint(); } else if (second >= 30 && second < 45) { x3 = a - (Math.cos(Math.toRadians((45 - second) * 6))) * r; y3 = b + (Math.sin(Math.toRadians((45 - second) * 6))) * r; repaint(); } else { x3 = a - (Math.sin(Math.toRadians((60 - second) * 6))) * r; y3 = b - (Math.cos(Math.toRadians((60 - second) * 6))) * r; repaint(); } } }
3.clock_demo3.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class clock_demo3 extends javax.swing.JFrame{ /** * Creates new form Clock */ public clock_demo3(){ initComponents(); Calendar time = new GregorianCalendar(); timeLable.setText(String.format("%2d:%02d:%02d", time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), time.get(Calendar.SECOND))); new javax.swing.Timer(1000, new ActionListener(){ public void actionPerformed(ActionEvent e){ timePanel.repaint(); Calendar time = new GregorianCalendar(); timeLable.setText(String.format("%2d:%02d:%02d", time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), time.get(Calendar.SECOND))); } }).start(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { timePanel = new ClockPanel(); timeLable = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setResizable(false); timePanel.setPreferredSize(new java.awt.Dimension(350, 350)); javax.swing.GroupLayout timePanelLayout = new javax.swing.GroupLayout(timePanel); timePanel.setLayout(timePanelLayout); timePanelLayout.setHorizontalGroup( timePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 350, Short.MAX_VALUE) ); timePanelLayout.setVerticalGroup( timePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 350, Short.MAX_VALUE) ); timeLable.setFont(new java.awt.Font("���ź�", 0, 24)); // NOI18N timeLable.setText("12:00:00"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(timePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addGap(121, 121, 121) .addComponent(timeLable, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(timePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(timeLable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]){ java.awt.EventQueue.invokeLater(new Runnable(){ public void run(){ new clock_demo3().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JLabel timeLable; private javax.swing.JPanel timePanel; // End of variables declaration } class ClockPanel extends JPanel{ @Override protected void paintComponent(Graphics g){ super.paintComponent(g); g.drawOval(25, 25, 300, 300); for (int i = 0; i < 60; i++){ g.drawLine((int)(175 + 145 * Math.sin(i / 60.0 * 2 * Math.PI)), (int)(175 - 145 * Math.cos(i / 60.0 * 2 * Math.PI)), (int)(175 + 150 * Math.sin(i / 60.0 * 2 * Math.PI)), (int)(175 - 150 * Math.cos(i / 60.0 * 2 * Math.PI))); } GregorianCalendar time = new GregorianCalendar(); g.setColor(Color.RED); double second = time.get(Calendar.SECOND); g.drawLine(175, 175, (int)(175 + 140 * Math.sin(second / 60 * 2 * Math.PI)), (int)(175 - 140 * Math.cos(second / 60 * 2 * Math.PI))); g.setColor(Color.BLUE); double minute = time.get(Calendar.MINUTE); g.drawLine(175, 175, (int)(175 + 100 * Math.sin(minute / 60 * 2 * Math.PI)), (int)(175 - 100 * Math.cos(minute / 60 * 2 * Math.PI))); g.setColor(Color.BLACK); double hour = time.get(Calendar.HOUR_OF_DAY); g.drawLine(175, 175, (int)(175 + 60 * Math.sin((hour + minute / 60) / 12 * 2 * Math.PI)), (int)(175 - 60 * Math.cos((hour + minute / 60) / 12 * 2 * Math.PI))); } }