Java アナログ時計 ソースコード2022年08月17日 10:09

// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// https://ateraimemo.com/Swing/AnalogClock.html

//package example;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.time.LocalTime;
import java.time.ZoneId;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    add(new AnalogClock());
    setPreferredSize(new Dimension(250, 250));
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("AnalogClock");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

class AnalogClock extends JPanel {
  protected LocalTime time;// = LocalTime.now(ZoneId.systemDefault());

  protected AnalogClock() {
    super();
    new Timer(100, e -> {
      time = LocalTime.now(ZoneId.systemDefault());
      repaint();
    }).start();
  }

  @Override protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Rectangle rect = SwingUtilities.calculateInnerArea(this, null);
    g2.setColor(Color.BLACK);
    g2.fill(rect);
    float radius = Math.min(rect.width, rect.height) / 2f - 10f;
    // g2.fill(new Ellipse2D.Double(rect.getCenterX() - radius, rect.getCenterY() - radius, radius * 2f, radius * 2f));
    g2.translate(rect.getCenterX(), rect.getCenterY());

    // Drawing the hour markers
    float hourMarkerLen = radius / 6f - 10f;
    Shape hourMarker = new Line2D.Float(0f, hourMarkerLen - radius, 0f, -radius);
    Shape minuteMarker = new Line2D.Float(0f, hourMarkerLen / 2f - radius, 0f, -radius);
    AffineTransform at = AffineTransform.getRotateInstance(0d);
    g2.setStroke(new BasicStroke(2f));
    g2.setColor(Color.WHITE);
    for (int i = 0; i < 60; i++) {
      if (i % 5 == 0) {
        g2.draw(at.createTransformedShape(hourMarker));
      } else {
        g2.draw(at.createTransformedShape(minuteMarker));
      }
      at.rotate(Math.PI / 30d);
    }

    //文字12 3 6 9 rectは横250,縦250 たてよこのセンターが原点0,0
    Font font = new Font("Arial", Font.BOLD, 14);
    g2.setFont(font);
    g2.drawString("12", radius * 0.75f * (int)Math.cos(Math.PI / 2f) - 7f, -radius * 0.75f * (int)Math.sin(Math.PI / 2f));
    g2.drawString("3", radius * 0.75f * (int)Math.cos(0f), -radius * 0.75f * (int)Math.sin(0f) + 7f);
    g2.drawString("6", radius * 0.75f * (int)Math.cos(-Math.PI / 2f) - 7f, -radius * 0.75f * (int)Math.sin(-Math.PI / 2f) + 7f);
    g2.drawString("9", radius * 0.75f * (int)Math.cos(Math.PI) - 7f, -radius * 0.75f * (int)Math.sin(Math.PI) + 7f);

    double miriRot = time.getNano() * Math.PI / 30000000000d;//OK!
    double miriSecondRot = time.getSecond() * Math.PI / 30d + miriRot;
    double secondRot = time.getSecond() * Math.PI / 30d;
    double minuteRot = time.getMinute() * Math.PI / 30d + secondRot / 60d;
    // double hourRot = time.getHour() * Math.PI * 2d / 12d + time.getMinute() * Math.PI * 2d / (12d * 60d);
    double hourRot = time.getHour() * Math.PI / 6d + minuteRot / 12d;

    // Drawing the hour hand
    float hourHandLen = radius / 1.5f;
    Shape hourHand = new Line2D.Float(0f, 0f, 0f, -hourHandLen);
    g2.setStroke(new BasicStroke(8f));
    g2.setPaint(Color.cyan);    //LIGHT_GRAY);
    g2.draw(AffineTransform.getRotateInstance(hourRot).createTransformedShape(hourHand));

    // Drawing the minute hand
    float minuteHandLen = 5f * radius / 6f;
    Shape minuteHand = new Line2D.Float(0f, 0f, 0f, -minuteHandLen);
    g2.setStroke(new BasicStroke(4f));
    g2.setPaint(Color.green);  //WHITE);
    g2.draw(AffineTransform.getRotateInstance(minuteRot).createTransformedShape(minuteHand));

    // Drawing the second hand
    float r = radius / 6f;
    float secondHandLen = radius - r;
    Shape secondHand = new Line2D.Float(0f, r, 0f, -secondHandLen);
    g2.setPaint(Color.RED);
    g2.setStroke(new BasicStroke(1f));
    g2.draw(AffineTransform.getRotateInstance(miriSecondRot).createTransformedShape(secondHand));
    g2.fill(new Ellipse2D.Float(-r / 4f, -r / 4f, r / 2f, r / 2f));

    g2.dispose();
  }
}



コメント

トラックバック

このエントリのトラックバックURL: http://tukasa.asablo.jp/blog/2022/08/17/9518353/tb

<< 2022/08
01 02 03 04 05 06
07 08 09 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

このブログについて

ネットで見つけたいろいろ雑記

バックナンバー

RSS