import ifi.plot.DoubleBufferedCanvas; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Canvas; import java.awt.Polygon; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; /** * The RoundClock implements a simple round clock, which consits of * three hands and markings for every hour. * The clock does not update itself (see ClockAdmin). */ public class RoundClock extends DoubleBufferedCanvas{ // --------------- // Local variables // --------------- // Used to store the current time. Updated by updateClock. Date current_time = new Date(); // Default calandar uses Central European Time time. Calendar local_calendar = Calendar.getInstance(); /* * Draw the clock face and the three hands. */ public void drawClock(Graphics g){ // Variables that control the size and position of the clock. double radius; int center_x; int center_y; Dimension d = getSize(); if(d.width < d.height) radius = d.width/2-5; else radius = d.height/2-5; center_x = d.width/2; center_y = d.height/2; // Get size of the canvas, and compute the largest circle // that fits the canvas. Reduce the radius so that you get // a border which is 5 pixels large. Also find the center // of the cock. Put the results in radius, center_x and center_y // Erase old clock. g.clearRect(0, 0, d.width, d.height); // Draw clock face (circle and 12 hour marks). g.drawOval(5, 5, (int) d.width - 10, d.height - 10); for(double a = 0; a < 2 * Math.PI; a += Math.PI / 6) g.drawLine((int) (Math.cos(a) * (radius - 10) + center_x), (int) (Math.sin(a) * (radius - 10) + center_y), (int) (Math.cos(a) * (radius - 15) + center_x), (int) (Math.sin(a) * (radius - 15) + center_y)); // Get current time. local_calendar.setTime(current_time); int hour = local_calendar.get(Calendar.HOUR); int minute = local_calendar.get(Calendar.MINUTE); int second = local_calendar.get(Calendar.SECOND); // Draw hour hand double angle; Polygon p = new Polygon(); angle = ((double) hour + (double) minute / 60) * Math.PI / 6 - Math.PI / 2; p.addPoint((int)(Math.cos(angle) * (6 * radius / 10) + center_x), (int) (Math.sin(angle) * (6 * radius / 10) + center_y)); angle = ((double) hour + (double) minute / 60) * Math.PI / 6; p.addPoint((int)(Math.cos(angle) * (3.5 * radius / 100) + center_x), (int) (Math.sin(angle) * (3.5 * radius / 100) + center_y)); angle = ((double) hour + (double) minute / 60) * Math.PI / 6 - Math.PI; p.addPoint((int)(Math.cos(angle) * (3.5 * radius / 100) + center_x), (int) (Math.sin(angle) * (3.5 * radius / 100) + center_y)); g.fillPolygon(p); // Draw minute hand p = new Polygon(); angle = ((double) minute + (double) second / 60) * (Math.PI / 30) - Math.PI / 2; p.addPoint((int)(Math.cos(angle) * (8 * radius / 10) + center_x), (int) (Math.sin(angle) * (8 * radius / 10) + center_y)); angle = ((double) minute + (double) second / 60) * (Math.PI / 30); p.addPoint((int)(Math.cos(angle) * (3 * radius / 100) + center_x), (int) (Math.sin(angle) * (3 * radius / 100) + center_y)); angle = ((double) minute + (double) second / 60) * (Math.PI / 30) + Math.PI; p.addPoint((int)(Math.cos(angle) * (3 * radius / 100) + center_x), (int) (Math.sin(angle) * (3 * radius / 100) + center_y)); g.fillPolygon(p); // Draw second hand angle = second * Math.PI / 30 - Math.PI / 2; g.drawLine(center_x, center_y, (int) (Math.cos(angle) * (radius - 20) + center_x), (int) (Math.sin(angle) * (radius - 20) + center_y)); } /* * This method should update the current time, and * redraw the clock. */ public void updateClock(){ current_time = new Date(); updateCanvas(); } /* * Build a calendar object which represent the given time zone. */ public void setTimeZone(String zone){ local_calendar = Calendar.getInstance(TimeZone.getTimeZone(zone)); updateClock(); } /** * This method is required by Canvas. It is called when the * canvas receives a paint/update request from AWT. */ public synchronized void updateCanvas(Graphics g) { drawClock(g); } }