// Invoked by timer when interval ends @Override public void actionPerformed(ActionEvent event) { Font font = panel.getFont(); // Create a new attributed string with the new time Date current = new Date(); Calendar cal = panel.getCalendar(); cal.setTime(current); long blink = ((System.currentTimeMillis() - startTime) / (BLINK_INTERVAL)) % 2; // To format current time using specific pattern. SimpleDateFormat df; // //////////////////////////////////// // Draw standard digital-type clock // // //////////////////////////////////// // Hour24 mode df = new SimpleDateFormat( "HH" + panel.getPadding1() + "mm" + panel.getPadding1() + "ss", panel.getLocale()); String str = df.format(current); String temp = ""; int tmpLen1 = 0; df.applyPattern("SSS"); temp = " " + panel.getPadding2() + df.format(current).substring(0, 3); tmpLen1 = temp.length(); str += temp; temp = ""; // ////////////// // Draw AM/PM // // ////////////// int tmpLen2 = 0; // mode df.applyPattern("a"); temp = " " + df.format(current); tmpLen2 = temp.length(); str += temp; temp = ""; // timezone df.applyPattern("Z"); StringBuffer sb = new StringBuffer("GMT"); sb.append(df.format(current)); sb.insert(6, ":"); df.applyPattern("zzzz"); temp = " " + df.format(current) + " (" + df.getTimeZone().getID() + ", " + sb + ")"; String str2 = temp; temp = ""; // ////////////////////////// // Format the time string // // ////////////////////////// timeString = null; timeString = new AttributedString(str); // Render main time area timeString.addAttribute(TextAttribute.FONT, font, 0, 6 + 2 * panel.getPadding1().length()); timeString.addAttribute(TextAttribute.FOREGROUND, panel.getForeground()); // Do blinking if reach alarm point timeString.addAttribute( TextAttribute.FOREGROUND, panel.getForeground(), 0, 6 + 2 * panel.getPadding1().length()); // Render padding1, do blinking timeString.addAttribute( TextAttribute.FOREGROUND, blink == 0 ? panel.getForeground() : panel.getBackground(), 2, 2 + panel.getPadding1().length()); timeString.addAttribute( TextAttribute.FOREGROUND, blink == 0 ? panel.getForeground() : panel.getBackground(), 2 + panel.getPadding1().length() + 2, 4 + 2 * panel.getPadding1().length()); // Render stopwatch timeString.addAttribute( TextAttribute.FONT, new Font( font.getName(), font.getStyle(), font.getSize() - SHRINK > 1 ? font.getSize() - SHRINK : 1), 6 + 2 * panel.getPadding1().length(), 6 + 2 * panel.getPadding1().length() + tmpLen1); // Render am/pm area timeString.addAttribute( TextAttribute.FONT, new Font( font.getName(), font.getStyle(), font.getSize() - SHRINK - 4 > 1 ? font.getSize() - SHRINK - 4 : 1), str.length() - tmpLen2, str.length()); // Create a new text layout and signal the panel that it needs // repainting textLayout = null; textLayout = new TextLayout(timeString.getIterator(), Clock.frc); // Render timezone, drawing at another line. timezoneString = null; timezoneString = new AttributedString(str2); timezoneString.addAttribute( TextAttribute.FONT, new Font( font.getName(), font.getStyle(), font.getSize() - SHRINK - 4 > 1 ? font.getSize() - SHRINK - 4 : 1)); timezoneString.addAttribute(TextAttribute.FOREGROUND, panel.getForeground()); textTimezoneLayout = null; textTimezoneLayout = new TextLayout(timezoneString.getIterator(), Clock.frc); // To keep the clock size fit for // actual size in time. panel.setSize(getPreferredSize(panel)); panel.repaint(); current = null; df = null; }