⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 telltime.java

📁 java 完全探索的随书源码
💻 JAVA
字号:
// TellTime.java

import java.awt.*;
import java.net.*;
import java.applet.*;
import java.awt.event.*;

class TellTime extends Frame implements ItemListener
{
   Choice hour, minute;
   clockSurface s;

   // Do not make either minuteColor or hourColor black.

   final static Color minuteColor = Color.orange;
   final static Color hourColor = Color.cyan;

   TellTime (String title)
   {
      // Pass the title to the frame superclass, so this title can be
      // displayed in its title bar.

      super (title);

      // Create a window listener (based on an anonymous inner class
      // that subclasses WindowAdapter) to listen for window closing
      // events (when the user clicks the close box on the frame).
      // Exit TellTime when this happens.

      addWindowListener (new WindowAdapter ()
                         {
                             public void windowClosing (WindowEvent e)
                             {
                                System.exit (0);
                             }
                         });

      // Create a panel that will contain a newly created clock
      // surface canvas.  Add this panel to the center of the
      // frame.

      Panel p = new Panel ();
      p.add (s = new clockSurface (hourColor, minuteColor));
      add (p, BorderLayout.CENTER);

      // Create a specialized panel for containing Hour/Minute choice
      // components.  This panel provides a small blank border that
      // results in a more pleasing display when the Minute choice
      // component is shown.  Furthermore, a grid layout manager is
      // assigned to ensure that each component added to the panel
      // has the same size.

      p = new myPanel ();
      p.setBackground (Color.black);
      p.setLayout (new GridLayout (2, 2, 0, 5));

      // Add an Hour label and associated choice to the panel.

      Label l = new Label ("Hour");
      l.setForeground (hourColor);
      p.add (l);

      hour = new Choice ();
      hour.addItemListener (this);
      hour.add ("12");
      for (int i = 1; i < 12; i++)
           hour.add ("" + i);
      p.add (hour);

      // Add an Minute label and associated choice to the panel.

      l = new Label ("Minute");
      l.setForeground (minuteColor);
      p.add (l);

      minute = new Choice ();
      minute.addItemListener (this);
      for (int i = 0; i < 60; i++)
           minute.add ("" + i);
      p.add (minute);

      // Add the panel to the south part of the frame.

      add (p, BorderLayout.SOUTH);

      // Set the frame's size to 300 by 300 pixels and ensure that
      // users cannot resize this frame by dragging its borders.

      setSize (300, 300);
      setResizable (false);

      // Ensure that the frame is visible.

      setVisible (true);

      // Attempt to load and play an initial sound clip.

      try
      {
         Applet.newAudioClip (new URL ("file:welcome.au")).play ();

         Thread.sleep (1000);
      }
      catch (Exception e) { System.out.println (e); }
   }

   // The following method is called when the user selects either a
   // new hour or a new minute from the respective choice component.

   public void itemStateChanged (ItemEvent e)
   {
      int h = hour.getSelectedIndex ();
      
      int m = minute.getSelectedIndex ();

      // Redraw the clock using the newly chosen hour and minute.

      s.updateClock (h, m);
   }

   public static void main (String [] args)
   {
      // Create the TellTime GUI and start the program.

      new TellTime ("Tell Time");
   }
}

// Objects created from the following Canvas subclass represent
// analogue clock displays.

class clockSurface extends Canvas
{
   final static Color faceColor = Color.blue;
   final static Color borderColor = Color.black;

   private Color minuteColor;
   private Color hourColor;

   private int hour, minute;

   public clockSurface (Color hourColor, Color minuteColor)
   {
      this.hourColor = hourColor;
      this.minuteColor = minuteColor;
   }

   public void updateClock (int hour, int minute)
   {
      this.hour = hour;
      this.minute = minute;

      // Tell the windowing toolkit to call the clockSurface object's
      // update method.

      repaint ();
   }

   public Dimension getPreferredSize ()
   {
      // Ensure that the clockSurface occupies a 200 by 200 pixel
      // area.

      return new Dimension (200, 200);
   }

   public void paint (Graphics g)
   {
      // Draw the clock's face and border.

      g.setColor (faceColor);
      g.fillOval (40, 40, 120, 120);

      g.setColor (borderColor);
      g.drawOval (40, 40, 120, 120);

      // Draw the tick marks around the border - along with numbers.

      g.setColor (Color.black);
      for (int h = 0; h < 12; h++)
      {
           double angle = Math.PI / 2 - h * Math.PI / 6;
           double x = Math.cos (angle);
           double y = Math.sin (angle);

           g.drawLine (100 + (int) (55 * x), 100 - (int) (55 * y),
                       100 + (int) (65 * x), 100 - (int) (65 * y));

           g.drawString (h == 0 ? "12" : "" + h,
                         95 + (int) (80 * x),
                         ((h > 3 && h < 9) ? 110 : 100)
                         - (int) (80 * y));
      }
           
      // Draw the minute hand.

      double angle = Math.PI / 2 - minute * Math.PI / 30;

      g.setColor (minuteColor);
      g.drawLine (100, 100, 100 + (int) (Math.cos (angle) * 55),
                  100 - (int) (Math.sin (angle) * 55));

      // When you look at an analogue clock, you'll notice that
      // the hour hand drifts closer to the next hour as the
      // number of minutes passes.  To make this clock more
      // accurate, drift is taken into account.  For every 12
      // minutes, the hour hand will be drawn 1 minute closer
      // to the following hour.  For example, 11:00 would show
      // the hour hand straight on the 11 position.  At 11:12,
      // the hour hand would appear 1 minute closer to the 12
      // o'clock position.

      double drift = Math.PI / 30 * (minute / 12);

      // Draw the hour hand.

      angle = Math.PI / 2 - hour * Math.PI / 6 - drift;

      g.setColor (hourColor);
      g.drawLine (100, 100, 100 + (int) (Math.cos (angle) * 40),
                  100 - (int) (Math.sin (angle) * 40));
   }

   // The following method is called by the windowing toolkit in
   // response to a repaint.  The default version clears the
   // background of Canvas and its subclass components.  To prevent
   // the resulting flicker, update is overridden to just call paint
   // - and not clear the background.

   public void update (Graphics g)
   {
      paint (g);
   }
}

// Objects created from the following class represent specialized
// panels that leave a small amount of space between the panel's
// border and the rest of the panel.

class myPanel extends Panel
{
   public Insets getInsets ()
   {
      return new Insets (5, 5, 7, 5);
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -