2D绘图 钟表

package pro.use.wang;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.Toolkit;


import java.util.Calendar;
import java.util.GregorianCalendar;


import javax.swing.JFrame;
import javax.swing.JLabel;


import pro.tool.wang.MF;

public class zhongbiao2 {

 public static void main(String[] args) {

  JFrame.setDefaultLookAndFeelDecorated(true);
  new MyFrame2();
  
 }

}

class MyFrame2 {

 private JFrame jf =jft1(400, 450, "clock");
 private MyPanel mp;
 private JLabel jbl2;
 

 public MyFrame2()  {

  mp = new MyPanel();
  jf.add(mp);

  // ------------------------------------

  new Thread(new Runnable() {

   @Override
   public void run() {
    while (true) {
     Calendar c=new GregorianCalendar();
     int s = c.get(Calendar.SECOND);
     int m = c.get(Calendar.MINUTE);
     int hour =c.get(Calendar.HOUR_OF_DAY);
     //若时间是个位数,前面加零
     String se=s<10?"0":"";String mi=m<10?"0":"";String ho=hour<10?"0":"";
     
      
     String sb=new String();
     sb=" "+ho+hour+":"+mi+m+":"+se+s;
       
     jf.setTitle(sb);
     mp.repaint();

     try {
      Thread.sleep(1000);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }
  }).start();

  // ------------------------------------
  jf.setVisible(true);
 }

 public JFrame jft1(int w, int h, String title){
  
  
    JFrame frame = new JFrame();
  
  int width = Toolkit.getDefaultToolkit().getScreenSize().width;
  int height = Toolkit.getDefaultToolkit().getScreenSize().height;

  int leftX = (width - w) / 2;
  int leftY = (height - h) / 2;

  frame.setBounds(leftX, leftY, w, h);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  return frame;
 
 }

 private class MyPanel extends Panel {

  private static final long serialVersionUID = 1L;

  @Override
  public void paint(Graphics gg) {
   super.paint(gg);

   Graphics2D g = (Graphics2D) gg;
   // 获取圆心坐标
   int w = this.getSize().width;
   int h = this.getSize().height;
   int x = w / 2;
   int y = h / 2;

   int wh = Math.min(w, h);

   double radius = wh * 0.45;

   // 画细线 60根
   MF.drawLine(60, g, 3f, MF.getColor(), x, y, radius, 6, 12);
   // 画粗线 12根

   MF.drawLine(12, g, 4f, MF.getColor(), x, y, radius, 30, 20);
   // 调用画圆的方法画大圆
   MF.drawCircle(g, 5f, MF.getColor(), x, y, radius);

   Calendar cal = Calendar.getInstance();
   int hour = cal.get(Calendar.HOUR);
   int minute = cal.get(Calendar.MINUTE);
   int second = cal.get(Calendar.SECOND);

   // 画秒针
   double angle = second * 6 - 90;
   MF.drawPointer(g, Color.RED, 2.5f, wh / 3.0, angle, x, y);

   // 画分针
   angle = (minute + second / 60.0) * 6 - 90;
   MF.drawPointer(g, Color.green, 4.5f, wh / 5.0, angle, x, y);

   // 画时针
   angle = (hour + (minute * 60 + second) / 3600.0) * 30 - 90;


   MF.drawPointer(g, Color.blue, 6f, wh / 6.0, angle, x, y);

   // 画小圆
   MF.drawCircle(g, 10f, MF.getColor(), x, y, 5);

  }

 }
}
 

 

// 画针的方法

 public static void drawPointer(Graphics2D g, Color color, float stroke,
   double len, double angle, double x, double y) {
  g.setColor(color);
  g.setStroke(new BasicStroke(stroke));

  double rad = Math.toRadians(angle);
  double x2 = x + len * Math.cos(rad);
  double y2 = y + len * Math.sin(rad);
  Line2D line = new Line2D.Double(new Point2D.Double(x, y),
    new Point2D.Double(x2, y2));
  g.draw(line);

 }

 // 画圆的方法

 public static void drawCircle(Graphics2D g, float stroke, Color color,
   double x, double y, double radius) {

  g.setColor(color);
  g.setStroke(new BasicStroke(stroke));

  Ellipse2D c = new Ellipse2D.Double();
  c.setFrameFromCenter(new Point2D.Double(x, y), new Point2D.Double(x
    + radius, y + radius));
  g.draw(c);

 }

 // 画线的方法
 public static void drawLine(int n, Graphics2D g, float stroke, Color color,
   double x, double y, double radius, double angle, double length) {
  g.setColor(color);
  g.setStroke(new BasicStroke(stroke));

  double len = radius - length;

  for (int i = 0; i < n; i++) {
   double rad = Math.toRadians(i * angle);

   double x1 = x + len * Math.cos(rad);
   double y1 = y + len * Math.sin(rad);
   double x2 = x + radius * Math.cos(rad);
   double y2 = y + radius * Math.sin(rad);

   Line2D line = new Line2D.Double(new Point2D.Double(x1, y1),
     new Point2D.Double(x2, y2));
   g.draw(line);
  }

 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值