package Views; /* * AnalogDisplay.java * This file contains an analog display of a Stopwatch2 * Created on February 9, 2002 * * @author Stuart Hansen * @version */ import javax.swing.*; import java.awt.*; import java.awt.event.*; // The analog display shows the watch's time with sweep hands public class AnalogDisplay extends SWView { private Analog display; // the digital display private double minutesInRadians; // the number of radians to rotate the minute hand private double secondsInRadians; // the number of radians to rotate the second hand // The constructor public AnalogDisplay () { super("Analog Display"); // Set up the analog display display = new Analog(); display.setLocation(110, 25); Container c = getContentPane(); c.add(display); // Initial the the display to 0.0 minutesInRadians = 0.0; secondsInRadians = 0.0; // size and open the window setSize(300, 225); show(); } // setDisplay time converts its parameter to two different radian values and then // uses these values to repaint the stopwatch display protected void setDisplayTime (long timeArg) { secondsInRadians = timeArg/10.0 * 2.0*Math.PI/60.0; minutesInRadians = timeArg/600.0 * 2.0*Math.PI/60.0; display.repaint(); } // The actual display is in a JPanel class Analog extends JPanel { // the constructor public Analog() { setSize(170,170); setLayout(null); } // The paint routine is a bit ugly, but each part is understandable public void paintComponent (Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; // Display the background g2d.setColor(Color.gray); g2d.fillOval(0, 0, 170, 170); g2d.setColor(Color.black); g2d.fillOval(8, 8, 154, 154); g2d.setColor(Color.white); g2d.translate(10,10); g2d.fillOval(0, 0, 150, 150); g2d.setColor(Color.black); // Draw the numbers around the outside g2d.translate(75, 75); g2d.drawString((new Integer(0)).toString(), -4, -60); g2d.rotate(Math.PI/6.0); g2d.drawString((new Integer(5)).toString(), -4, -60); g2d.rotate(Math.PI/6.0); for (int i=10; i<60; i+=5) { g2d.drawString((new Integer(i)).toString(), -8, -60); g2d.rotate(Math.PI/6.0); } // Draw the ticks around the outside for (int i=0; i<60; i++) { if (i%5==0) g2d.setStroke(new BasicStroke(2)); else g2d.setStroke(new BasicStroke(1)); g2d.drawLine(0, -72, 0, -75); g2d.rotate(Math.PI/30.0); } // Draw some text on the face g2d.drawString ("UW-Parkside", -40, 22); g2d.drawString ("Events Are Us", -43, 34); // Place a splat in the middle to attach the second hand to g2d.fillOval(-5, -5, 10, 10); // display the second hand g2d.setStroke(new BasicStroke(3)); g2d.setColor(Color.black); g2d.rotate(secondsInRadians); g2d.drawLine(0, 10, 0, -70); g2d.rotate(-1.0*secondsInRadians); // display the minute hand g2d.setStroke(new BasicStroke(1)); g2d.drawOval(-16, -50, 32, 32); g2d.drawOval(-14, -48, 28, 28); g2d.translate(0, -34); g2d.rotate(minutesInRadians); g2d.drawLine(0, 0, 0, -15); g2d.rotate(-1.0*minutesInRadians); g2d.translate(0, 34); } } }