package com.github.nanaze.piclock;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.time.LocalTime;

public class ClockCanvas extends Canvas {
  public ClockCanvas() {
    setBackground(Color.BLACK);
  }


  public void paint(Graphics g) {
    g.setColor(Color.WHITE);

    LocalTime time = LocalTime.now();
    String timeString = String.format("%d:%02d:%02d", time.getHour(), time.getMinute(), time.getSecond());

    drawString(timeString, g);
  }

  private void drawString(String timeString, Graphics g) {
    Dimension dim = getSize();

    int fontSize = 1;
    Font font = new Font(Font.MONOSPACED, Font.PLAIN, fontSize);
    Font biggestFont = font;

    // Figure out the biggest font that fits.
    while (true) {
      font = new Font(Font.MONOSPACED, Font.PLAIN, fontSize);
      FontMetrics fm = g.getFontMetrics(font);
      Rectangle2D rect = fm.getStringBounds(timeString, g);

      if (rect.getWidth() <= dim.width &&
        rect.getHeight() <= dim.height) {
        biggestFont = font;
      } else {
        break;
      }

      fontSize++;
    }

    // Draw the string.
    g.setFont(biggestFont);
    Rectangle2D rect = g.getFontMetrics().getStringBounds(timeString, g);
    g.drawString(timeString,
      (int) (dim.width - rect.getWidth()) / 2,
      (int) (dim.height - rect.getHeight()) / 2 + (int) Math.abs(rect.getY()));
  }
}

@@ -99,65 +99,340 @@
 va.t
-ime.LocalTime;%0A%0Apublic class ClockCanvas extends Canvas %7B
+ext.DateFormat;%0Aimport java.time.LocalTime;%0Aimport java.time.format.DateTimeFormatter;%0Aimport java.time.format.FormatStyle;%0Aimport java.util.Locale;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0Apublic class ClockCanvas extends Canvas %7B%0A%0A  private static final Pattern TIME_PATTERN =  Pattern.compile(%22%5B%5C%5Cd:%5D+%22);%0A
 %0A  p
@@ -592,16 +592,106 @@
 e.now();
+%0A%0A    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
 %0A    Str
@@ -711,22 +711,20 @@
 g = 
-String
+time
 .format(
 %22%25d:
@@ -723,106 +723,282 @@
 mat(
-%22%25d:%2502d:%2502d%22, time.getHour(), time.getMinute(), time.getSecond()
+formatter);%0A%0A    drawString(extractTime(timeString), g);%0A  %7D%0A%0A  private static String extractTime(String formattedTime) %7B%0A    Matcher matcher = TIME_PATTERN.matcher(formattedTime
 );%0A
-%0A
     
-drawString(timeString, g
+boolean patternFound = matcher.find();%0A    assert patternFound;%0A    return matcher.group(
 );%0A 

update code to generate time from locale