view Clock.java @ 2:1fe7ac28a3ca

Save results after each presentation. Ignore-this: 5f7d744b55a2f8919f7807bf69fa3d1a
author Marcus Pearce <m.pearce@gold.ac.uk>
date Mon, 14 Jun 2010 13:28:18 +0100
parents 4031cbb02f08
children 5080b65e6963
line wrap: on
line source
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

/** Clocks displayed in a panel. */
public class Clock extends JPanel implements Runnable {

    /* The default colours */ 
    static final Color BACKGROUND = Color.black; 
    static final Color FOREGROUND = Color.white; 

    /* Panel width, height and border. */
    public static final int WIDTH = 450;
    public static final int BORDER = 50;

    /* Minutes in a day. */
    public static final int ONE_DAY = 24 * 60;

    /* Show the clock? */ 
    public Boolean showClock = false; 
    public Boolean showFullClock = false; 

    /* minutes since midnight, range 0 to (ONE_DAY - 1) inclusive */ 
    private int minutes = 0;

    /* Milliseconds per tick of the minute hand */ 
    private long millisecondsPerTick = 1000; 
    public void  setMillisecondsPerTick(long n) { millisecondsPerTick = n; }

    /* Create a new Clock at time midnight. */
    public Clock() {
        this.setBackground(BACKGROUND); 
        this.setPreferredSize (new Dimension (WIDTH, WIDTH));
    }

    public void run() { 
        reset(); 
        if (showClock == false) { 
            showClock = true; 
            repaint(); 
        } 
        for (int i = 0; i < 60; i++) {
            try { Thread.sleep (millisecondsPerTick); } 
            catch (InterruptedException e) {}
            tick(1);
            repaint();
        }
    } 
    
    /* Move time on by 1 second. */
    public void tick(int n) {
	minutes = (minutes + n) % ONE_DAY;
    }
    
    /* Reset the clock to 00:00. */ 
    public void reset() { 
        minutes = 0; 
    } 

    /* Draw the clock or fixation point. */
    public void paintComponent (Graphics g) {
	super.paintComponent (g);
        
        if (showClock == true) { 
            paintClock(g); 
        } else { 
            paintFixationPoint(g); 
        }
    }
    
    private void paintFixationPoint(Graphics g) { 
	int left_edge = BORDER;
	int top = BORDER;
	int diameter = WIDTH - (BORDER * 2);
	int centrex = left_edge + (diameter / 2); 
	int centrey = top + (diameter / 2); 

        int linelength = diameter / 20;  

        g.setColor(FOREGROUND);
        g.drawLine(centrey, centrex - linelength, centrey, centrex + linelength);
        g.drawLine(centrey - linelength, centrex, centrey + linelength, centrex);
    } 

    private void paintClock (Graphics g) { 
	int minutes_angle = 90 - ((minutes % 60) * 6);
	int hours_angle = 90 - ((minutes / 60) * 30);
	int left_edge = BORDER;
	int top = BORDER;
	int diameter = WIDTH - (BORDER * 2); 
        int radius = diameter / 2; 
	int centrex = left_edge + radius; 
	int centrey = top + radius; 

	g.setColor(FOREGROUND);

	// draw the clock as a black circle:
	g.drawOval(left_edge, top, diameter, diameter);

	// draw the hour numbers 
        int offset = 0; 
	for(int i = 1; i <= 12; i++) {
	    int hours_pos = 90 - (i * 30); 
	    int x = (centrex - offset)  + 
                (int)((radius - 5) * Math.cos(hours_pos * Math.PI / 180));
	    int y = (centrey + offset) - 
                (int)((radius - 5) * Math.sin(hours_pos * Math.PI / 180));
	    //g.drawString(String.valueOf(i), x, y); 
            g.drawOval(x, y, diameter / 200, diameter / 200); 
	}

	// get the coordinate of the line_ends 		
	int minutesx = (int)((radius - 5) * 
                             Math.cos(minutes_angle * Math.PI / 180)); 
	int minutesy = (int)((radius - 5) * 
                             Math.sin(minutes_angle * Math.PI /180)); 
        
        // draw the minutes' hand
        g.drawLine(centrex, centrey, centrex + minutesx, centrey - minutesy);

        int arc_angle = minutes_angle - 90; 
        Graphics2D g2 = (Graphics2D)g;
        g2.setPaint(FOREGROUND);
        
        //System.out.println("minutes_angle = " + minutes_angle + 
        //                   "; arc_angle = " + arc_angle); 

        if (arc_angle == 0 & showFullClock == true) { 
            g2.fillOval(left_edge, top, diameter, diameter);
            showFullClock = false; 
        } else { 
            showFullClock = true; 
            if (arc_angle >= -180) { 
                Arc2D pie = new Arc2D.Float(centrex - radius, centrey - radius, 
                                            2 * radius, 2 * radius, 
                                            90, arc_angle,
                                            Arc2D.PIE);
                g2.fill(pie);
            } else { 
                Arc2D pie1 = new Arc2D.Float(centrex - radius, centrey - radius, 
                                             2 * radius, 2 * radius, 
                                             90, -180, Arc2D.PIE);
                Arc2D pie2 = new Arc2D.Float(centrex - radius, centrey - radius, 
                                             2 * radius, 2 * radius, 
                                             270, arc_angle + 180,
                                             Arc2D.PIE);
                g2.fill(pie1);
                g2.fill(pie2);
            }
        }
    }
}