001 package com.croftsoft.apps.clock; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import java.awt.font.*; 006 import java.awt.geom.*; 007 import java.io.*; 008 import java.util.*; 009 import javax.swing.*; 010 011 import com.croftsoft.core.awt.font.FontLib; 012 import com.croftsoft.core.gui.FrameLib; 013 import com.croftsoft.core.lang.lifecycle.Lifecycle; 014 import com.croftsoft.core.text.DateFormatLib; 015 016 /********************************************************************* 017 * This digital clock applet/application displays the date and time. 018 * 019 * @version 020 * 2002-03-06 021 * @since 022 * 1996 023 * @author 024 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 025 *********************************************************************/ 026 027 public class DigitalClock 028 extends JApplet 029 implements ComponentListener, Lifecycle, Runnable 030 ////////////////////////////////////////////////////////////////////// 031 ////////////////////////////////////////////////////////////////////// 032 { 033 034 private static final String VERSION = "2002-03-06"; 035 036 private static final String TITLE = "CroftSoft Clock"; 037 038 private static final String INFO 039 = "\n" + TITLE + "\n" 040 + "Copyright 2002 CroftSoft Inc\n" 041 + "https://www.croftsoft.com/\n" 042 + "Version " + VERSION + "\n"; 043 044 private static final Dimension FRAME_SIZE = null; 045 046 private static final String FRAME_ICON_FILENAME = "/images/david.png"; 047 048 private static final String SHUTDOWN_CONFIRMATION_PROMPT 049 = "Close " + TITLE + "?"; 050 051 private static final String FONT_NAME = "TimesRoman"; 052 053 private static final int FONT_STYLE = Font.BOLD; 054 055 private static final String IDLE_DATE_TEXT = "CroftSoft"; 056 057 private static final String IDLE_TIME_TEXT = "www.CroftSoft.com"; 058 059 private static final String ACTIVE_DATE_TEXT = "0000-00-00"; 060 061 private static final String ACTIVE_TIME_TEXT = "00:00:00"; 062 063 // 064 065 private JLabel dateLabel; 066 067 private JLabel timeLabel; 068 069 private Thread thread; 070 071 private String resizeDateText; 072 073 private String resizeTimeText; 074 075 ////////////////////////////////////////////////////////////////////// 076 ////////////////////////////////////////////////////////////////////// 077 078 public static void main ( String [ ] args ) 079 ////////////////////////////////////////////////////////////////////// 080 { 081 System.out.println ( INFO ); 082 083 JFrame jFrame = new JFrame ( TITLE ); 084 085 try 086 { 087 FrameLib.setIconImage ( jFrame, FRAME_ICON_FILENAME ); 088 } 089 catch ( IOException ex ) 090 { 091 } 092 093 DigitalClock digitalClock = new DigitalClock ( ); 094 095 jFrame.setContentPane ( digitalClock ); 096 097 FrameLib.launchJFrameAsDesktopApp ( 098 jFrame, 099 new Lifecycle [ ] { digitalClock }, 100 FRAME_SIZE, 101 SHUTDOWN_CONFIRMATION_PROMPT ); 102 } 103 104 ////////////////////////////////////////////////////////////////////// 105 ////////////////////////////////////////////////////////////////////// 106 107 public String getAppletInfo ( ) { return INFO; } 108 109 ////////////////////////////////////////////////////////////////////// 110 ////////////////////////////////////////////////////////////////////// 111 112 public synchronized void init ( ) 113 ////////////////////////////////////////////////////////////////////// 114 { 115 resizeDateText = IDLE_DATE_TEXT; 116 117 resizeTimeText = IDLE_TIME_TEXT; 118 119 dateLabel = new JLabel ( IDLE_DATE_TEXT ); 120 121 dateLabel.setHorizontalAlignment ( SwingConstants.CENTER ); 122 123 timeLabel = new JLabel ( IDLE_TIME_TEXT ); 124 125 timeLabel.setHorizontalAlignment ( SwingConstants.CENTER ); 126 127 Container contentPane = getContentPane ( ); 128 129 contentPane.setBackground ( Color.WHITE ); 130 131 contentPane.setForeground ( Color.BLACK ); 132 133 contentPane.setLayout ( new GridLayout ( 2, 1 ) ); 134 135 contentPane.add ( dateLabel ); 136 137 contentPane.add ( timeLabel ); 138 139 timeLabel.addComponentListener ( this ); 140 } 141 142 public synchronized void start ( ) 143 ////////////////////////////////////////////////////////////////////// 144 { 145 thread = new Thread ( this ); 146 147 int priority = thread.getPriority ( ) - 1; 148 149 if ( priority >= Thread.MIN_PRIORITY ) 150 { 151 thread.setPriority ( priority ); 152 } 153 154 thread.setDaemon ( true ); 155 156 thread.start ( ); 157 } 158 159 public synchronized void stop ( ) 160 ////////////////////////////////////////////////////////////////////// 161 { 162 Thread thread = this.thread; 163 164 if ( thread != null ) 165 { 166 this.thread = null; 167 168 thread.interrupt ( ); 169 } 170 } 171 172 public synchronized void destroy ( ) 173 ////////////////////////////////////////////////////////////////////// 174 { 175 stop ( ); 176 } 177 178 ////////////////////////////////////////////////////////////////////// 179 ////////////////////////////////////////////////////////////////////// 180 181 public void run ( ) 182 ////////////////////////////////////////////////////////////////////// 183 { 184 dateLabel.setText ( " " ); 185 186 timeLabel.setText ( " " ); 187 188 resizeDateText = ACTIVE_DATE_TEXT; 189 190 resizeTimeText = ACTIVE_TIME_TEXT; 191 192 resizeFonts ( ); 193 194 Thread thread = Thread.currentThread ( ); 195 196 try 197 { 198 while ( thread == this.thread ) 199 { 200 String dateTime 201 = DateFormatLib.toIsoDateFormat ( new Date ( ) ); 202 203 dateLabel.setText ( 204 dateTime.substring ( 0, dateTime.length ( ) - 9 ) ); 205 206 timeLabel.setText ( 207 dateTime.substring ( dateTime.length ( ) - 8 ) ); 208 209 thread.sleep ( 100 ); 210 } 211 } 212 catch ( InterruptedException ex ) 213 { 214 } 215 finally 216 { 217 dateLabel.setText ( " " ); 218 219 timeLabel.setText ( " " ); 220 221 resizeDateText = IDLE_DATE_TEXT; 222 223 resizeTimeText = IDLE_TIME_TEXT; 224 225 resizeFonts ( ); 226 227 dateLabel.setText ( IDLE_DATE_TEXT ); 228 229 timeLabel.setText ( IDLE_TIME_TEXT ); 230 } 231 } 232 233 ////////////////////////////////////////////////////////////////////// 234 // interface ComponentListener methods 235 ////////////////////////////////////////////////////////////////////// 236 237 public void componentResized ( ComponentEvent componentEvent ) 238 ////////////////////////////////////////////////////////////////////// 239 { 240 resizeFonts ( ); 241 } 242 243 public void componentHidden ( ComponentEvent componentEvent ) 244 ////////////////////////////////////////////////////////////////////// 245 { 246 } 247 248 public void componentMoved ( ComponentEvent componentEvent ) 249 ////////////////////////////////////////////////////////////////////// 250 { 251 } 252 253 public void componentShown ( ComponentEvent componentEvent ) 254 ////////////////////////////////////////////////////////////////////// 255 { 256 } 257 258 ////////////////////////////////////////////////////////////////////// 259 ////////////////////////////////////////////////////////////////////// 260 261 private synchronized void resizeFonts ( ) 262 ////////////////////////////////////////////////////////////////////// 263 { 264 resizeFont ( dateLabel, resizeDateText ); 265 266 resizeFont ( timeLabel, resizeTimeText ); 267 } 268 269 private synchronized void resizeFont ( 270 Component component, 271 String text ) 272 ////////////////////////////////////////////////////////////////////// 273 { 274 Rectangle bounds = component.getBounds ( ); 275 276 FontLib.setMaxFont ( 277 component, 278 text, 279 FONT_NAME, 280 FONT_STYLE, 281 0.9 * bounds.width, 282 0.9 * bounds.height ); 283 } 284 285 ////////////////////////////////////////////////////////////////////// 286 ////////////////////////////////////////////////////////////////////// 287 }