画圆的刻度代码

来源:互联网 发布:海澜之家 淘宝 编辑:程序博客网 时间:2024/04/29 12:02
  1.   public void paintComponent(Graphics g) {
  2.             //确保父类完成自己的绘制工作
  3.             super.paintComponent(g);
  4.             Graphics2D g2 = (Graphics2D) g;
  5.             //定义变量
  6.             double X = 200.00, Y = 200.00, R = 150, Len, x1, x2, y1, y2, Rad;
  7.             //表盘
  8.             g2.setPaint(Color.BLUE);
  9.             Ellipse2D circle = new Ellipse2D.Double();
  10.             circle.setFrameFromCenter(X, Y, X + R, Y + R);
  11.             g2.draw(circle);
  12.             //刻度
  13.             g2.setPaint(Color.RED);
  14.             for (int i = 1; i <= 60; i++) {
  15.                 Rad = Math.toRadians(6* i);
  16.                 if (i % 5 == 0) {
  17.                     Len = 15;
  18.                 } else {
  19.                     Len = 5;
  20.                 }
  21.                 x1 = X + R * Math.cos(Rad);
  22.                 x2 = x1 - Len * Math.cos(Rad);
  23.                 y1 = Y + R * Math.sin(Rad);
  24.                 y2 = y1 - Len * Math.sin(Rad);
  25.                 System.out.println(x1+","+x2+","+y1+","+y2);
  26.                 g2.draw(new Line2D.Double(x1, y1, x2, y2));
  27.             }
  28.             //获取系统当前时间
  29.             Date dt = new Date(5);
  30.             DateFormat df = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
  31.             String NowTime = df.format(dt);
  32.             //指针
  33.             int hh = Integer.parseInt(NowTime.substring(9, 11));
  34.             int mm = Integer.parseInt(NowTime.substring(12, 14));
  35.             if (hh > 12) {
  36.                 hh = hh - 12;
  37.             }
  38.             int ss = Integer.parseInt(NowTime.substring(15, 17));
  39.             //时针
  40.             g2.setPaint(Color.GREEN);
  41.             Rad = Math.toRadians(30 * hh - 90 + mm / 2);
  42.             Len = 0.6 * R;
  43.             x2 = X + Len * Math.cos(Rad);
  44.             y2 = Y + Len * Math.sin(Rad);
  45.             g2.draw(new Line2D.Double(X, Y, x2, y2));
  46.             //分针
  47.             g2.setPaint(Color.BLUE);
  48.             Rad = Math.toRadians(6 * mm - 90);
  49.             Len = 0.8 * R;
  50.             x2 = X + Len * Math.cos(Rad);
  51.             y2 = Y + Len * Math.sin(Rad);
  52.             g2.draw(new Line2D.Double(X, Y, x2, y2));
  53.             //秒针
  54.             g2.setPaint(Color.RED);
  55.             Rad = Math.toRadians(6 * ss - 90);
  56.             Len = 0.9 * R;
  57.             x2 = X + Len * Math.cos(Rad);
  58.             y2 = Y + Len * Math.sin(Rad);
  59.             g2.draw(new Line2D.Double(X, Y, x2, y2));
  60.             //定义字体
  61.             Font f1 = new Font("Serif", Font.BOLD, 14);
  62.             g2.setFont(f1);
  63.             g2.setColor(Color.RED);
  64.             g2.drawString(NowTime, 130, 25);
  65.         }
原创粉丝点击