/* * Alec Jacobson * */ import java.awt.*; import java.util.Date; /** * Though the source of this code is open. The design of this clock very much * is not. You may not reproduce this design in any form. * @author Alec Jacobson alecjacobson@gmail.com */ public class GeometricClock extends BufferedApplet{ int w, h;//bounds of applet float alpha = 0.3f; boolean initialized = false; Color background_color = Color.WHITE; Color clock_color = Color.RED; Color hour_color = Color.GREEN; Color minute_color = Color.BLUE; public void render(Graphics g) { if(w!=bounds().width||h!=bounds().height){ w = bounds().width; h = bounds().height; } // SET CLOCK SIZE int r = (int)((1.0/Math.sqrt(2))*Math.min(w,h)/2); int cx = w/2; int cy = h/2; // GET DATE Date date = new Date(); double seconds = (double)date.getSeconds(); double minutes = (double)date.getMinutes() + seconds/60.0; double hours = (double)date.getHours() + minutes/60.0; // MINUTES double r_minute = r; double theta_minute = 2*Math.PI*(minutes%60.0)/60.0; double x_minute = r_minute*Math.sin(theta_minute); double y_minute = -r_minute*Math.cos(theta_minute); int[] minute_xs = { (int)(cx+r_minute*Math.sin(theta_minute+Math.PI/2)), (int)(cx+r_minute*Math.sin(theta_minute-Math.PI/2)), (int)(cx+r_minute*Math.sin(theta_minute-Math.PI/2)+x_minute), (int)(cx+r_minute*Math.sin(theta_minute+Math.PI/2)+x_minute)}; int[] minute_ys = { (int)(cy-r_minute*Math.cos(theta_minute+Math.PI/2)), (int)(cy-r_minute*Math.cos(theta_minute-Math.PI/2)), (int)(cy-r_minute*Math.cos(theta_minute-Math.PI/2)+y_minute), (int)(cy-r_minute*Math.cos(theta_minute+Math.PI/2)+y_minute)}; // HOURS double r_hour = Math.sqrt(4.0/5.0)*r; double theta_hour = 2*Math.PI*(hours%12.0)/12.0; double x_hour = r_hour*Math.sin(theta_hour); double y_hour = -r_hour*Math.cos(theta_hour); int[] hour_xs = { (int)(cx+r_hour*Math.sin(theta_hour+Math.PI/2)/2), (int)(cx+r_hour*Math.sin(theta_hour-Math.PI/2)/2), (int)(cx+r_hour*Math.sin(theta_hour-Math.PI/2)/2+x_hour), (int)(cx+r_hour*Math.sin(theta_hour+Math.PI/2)/2+x_hour)}; int[] hour_ys = { (int)(cy-r_hour*Math.cos(theta_hour+Math.PI/2)/2), (int)(cy-r_hour*Math.cos(theta_hour-Math.PI/2)/2), (int)(cy-r_hour*Math.cos(theta_hour-Math.PI/2)/2+y_hour), (int)(cy-r_hour*Math.cos(theta_hour+Math.PI/2)/2+y_hour)}; // turn on transparency capablity Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // CLEAR SCREEN //g2.clearRect(0,0,w,h); g2.setColor(background_color); g2.fillRect(0,0,w,h); // DRAW CLOCK g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); //g2.setColor(background_color); //g2.fillOval(cx-r,cy-r,2*r,2*r); g2.setColor(clock_color); g2.fillOval(cx-r,cy-r,2*r,2*r); g2.drawOval(cx-r,cy-r,2*r,2*r); // DRAW MINUTES RECT //g2.setColor(background_color); //g2.fillPolygon(minute_xs,minute_ys,minute_xs.length); g2.setColor(minute_color); g2.fillPolygon(minute_xs,minute_ys,minute_xs.length); g2.drawPolygon(minute_xs,minute_ys,minute_xs.length); // DRAW HOUR SQUARE //g2.setColor(background_color); //g2.fillPolygon(hour_xs,hour_ys,hour_xs.length); g2.setColor(hour_color); g2.fillPolygon(hour_xs,hour_ys,hour_xs.length); g2.drawPolygon(hour_xs,hour_ys,hour_xs.length); animating = true; } }