Java アナログ時計 ソースコード
2022-08-17


    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();

続きを読む
戻る

��コメント(全0件)


�セ記事を書く
�� powered by ASAHIネット