Mercurial > hg > mep
view Clock.java @ 39:796b3e3e053f
Changed MidiPlayer.java to allow the use of external sequencer programs (e.g. Garageband).
Changed Experiment.java to allow user to specify in commandline whether to use default MidiDevice (true), specified MidiDevice (0<variable<MidiDevs.length), or list the available devices on start up.
Changed ExperimentGui.java and SubjectDataPanel.java to add familiarity question if familiarity question isn't included at the presentation of each stimuli.
author | CBussey |
---|---|
date | Fri, 31 May 2013 17:40:59 +0100 |
parents | 014c83185b2a |
children |
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; } private boolean debug; /* Create a new Clock at time midnight. */ public Clock(boolean d) { this.setBackground(BACKGROUND); this.setPreferredSize (new Dimension (WIDTH, WIDTH)); debug = d; } 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(); } showClock = false; showFullClock = false; if (debug) System.out.println("RUN CLOCK"); reset(); revalidate(); 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() { if (debug) System.out.println("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); } //just ensure that java knows we want to draw new things revalidate(); } private void paintFixationPoint(Graphics g) { //System.out.println("paintFixationPoint"); 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) { //System.out.println("paintClock"); 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); if (debug) 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); } } } }