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

📄 clockappletwithaudio.java

📁 此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码。
💻 JAVA
字号:
// ClockAppletWithAudio.java: Display a running clock on the applet
// with audio
import java.applet.*;
import java.awt.*;
import java.util.*;

public class ClockAppletWithAudio extends CurrentTimeApplet
{
  // Declare audio files
  protected AudioClip[] hourAudio = new AudioClip[12];
  protected AudioClip minuteAudio;
  protected AudioClip amAudio;
  protected AudioClip pmAudio;

  // Declare a clock
  ClockWithAudio clock;

  // Initialize the applet
  public void init()
  {
    super.init();

    // Create audio clips for pronouncing hours
    for (int i=0; i<12; i++)
      hourAudio[i] = getAudioClip(getCodeBase(),
        "timeaudio/hour"+i+".au");

    // Create audio clips for pronouncing am and pm
    amAudio = getAudioClip(getCodeBase(), "timeaudio/am.au");
    pmAudio = getAudioClip(getCodeBase(), "timeaudio/pm.au");
  }

  // Override the createClock method defined in CurrentTimeApplet
  public void createClock()
  {
    getContentPane().add(clock =
      new ClockWithAudio(locale, timeZone, this));
  }

  // Announce the current time at every minute
  public void announceTime(int s, int m, int h)
  {
    if (s == 0)
    {
      // Announce hour
      hourAudio[h%12].play();

      // Load the minute file
      minuteAudio = getAudioClip(getCodeBase(),
        "timeaudio/minute"+m+".au");

      // Time delay to allow hourAudio play to finish
      try
      {
        Thread.sleep(1500);
      }
      catch(InterruptedException ex)
      {
      }

      // Announce minute
      minuteAudio.play();

      // Time delay to allow minuteAudio play to finish
      try
      {
        Thread.sleep(1500);
      }
      catch(InterruptedException ex)
      {
      }

      // Announce am or pm
      if (h < 12)
        amAudio.play();
      else
        pmAudio.play();
    }
  }

  // Implement Applet's start method to resume the thread
  public void start()
  {
    clock.resume();
  }

  // Implement Applet's stop method to suspend the thread
  public void stop()
  {
    clock.suspend();
  }
}

⌨️ 快捷键说明

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