📄 soundslideshow.java
字号:
/*
Slide Show with Sound
February 1999
*/
/*
* @(#)SoundSlideShow.java 1.0b 99/07/14 Clayton Roberts
*
* Copyright (c) 1999 Clayton Roberts. All rights reserved.
* Permission to use, modify, and distribute this software and its
* documentation for all purposes and without fee
* is hereby granted provided that this copyright notice appears.
*
* Free-Applets.com make no representations or warranties
* about the suitability of the software including
* but not limited to the implied warranties of merchantability, fitness,
* or non-infirngement. Clayton Roberts and Free-Applets.com
* shall not bw liable for any damages suffered by licensee.
*/
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.applet.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.applet.AudioClip;
public class SoundSlideShow extends Applet implements Runnable {
/**
* Vector of sound AudioClip objects
*/
public Vector soundList = new Vector();
/**
* Sound list to be preloaded
*/
private String sounds;
/**
* Index of the next sound in the sound string to be preloaded.
*/
private int soundIndex=0;
/**
* The absolute or relative URL of the audio file.
*/
private String audioFileURL;
/**
* The source of this applet. Must be "www.Free-Applets.com"
*/
private String appletSourceURL;
/**
* The current audio clip. Null when not playing any sound.
*/
private AudioClip theClip = null;
/**
* Report on the image components.
*/
private MediaTracker LoadImages;
/**
* The array of images to be viewed.
*/
private Image slides[];
/**
* Relative URLs to images file separated by |.
*/
private String imageList;
/**
* Title of the Slide Show applet.
*/
private String caption;
/**
* Titles for the Next and Previous Buttons.
*/
private String buttonNextTitle, buttonPreviousTitle;
/**
* Foreground and Background colors for applet components.
*/
private Color bgcolorApplet, fgcolorApplet;
/**
* Thread used to enable the automatic showing of slides.
*/
private Thread autoShow;
/**
* Number of images to be loaded, millisecond delay for display.
*/
private int imagesNum, delay;
/**
* Number of sounds to be loaded.
*/
private int soundsNum;
/**
* The current index of the image and sound files.
*/
private int index;
/**
* CheckBox components.
*/
private Checkbox auto, sound;
/**
* Label component for the title of the applet.
*/
private Label title;
/**
* The current sound file.
*/
private AudioClip clip;
/**
* The main panel and its subpanel.
*/
private Panel marquee, control;
/**
* The button components.
*/
private Button forward, backward;
/**
* Flag to indicate that all sound files are now loaded.
*/
private boolean soundsLoaded=false;
/**
* Relative URL of the current sound string.
*/
private String soundURL;
//Monitor Checkboxes and Buttons for Actions
public boolean action(Event evt, Object what) {
if (evt.target == sound) { //Sound CheckBox?
if (sound.getState() == true) play(index);
return true;
} else if (evt.target == backward) { //Previous Button?
if (index !=0) { //Yes... Previous Button.
backward.enable(); //Enable provided not at first slide.
forward.enable();
index--;
if(index == 0) backward.disable();
repaint(); //Update the applet display to show buttons
return true;
}
backward.disable(); //Disable Previous Button if at first slide.
return true;
} else if (evt.target == forward) { //Next Button pressed?
if(index > soundsNum - 2) {return true;}
forward.enable();
backward.enable();
index++; //Move to next slide
if (index > soundsNum - 2) forward.disable();
repaint(); //update the applet display
return true;
} return false;
}
/**
* Method to terminate the applet.
*/
public void destroy( ) {
removeAll();
return;
}
/**
* Method to get the applet info.
*/
public String getAppletInfo ( ) {
return "SoundSlideShowV1.0\n" +
"\n" +
"Written and copyrighted 1999 by Clayton Roberts.\n" +
"http://www.Free-Applets.com.\n";
}
/**
* Method to get the applet's parameter fields.
*/
public String[][] getParameterInfo() {
String[][] info = {
{"appletSourceURL", "string", "www.Free-Applets.com"},
{"bgcolorApplet", "string",
"Background color of applet wherein #RRGGBB are hexadecimals."},
{"fgcolorApplet", "string",
"Foreground color of applet wherein #RRGGBB are hexadecimals."},
{"caption", "string", "Title of the Slide Show"},
{"bgcolorButtons", "string",
"Background color of Buttons wherein #RRGGBB are hexadecimals."},
{"fgcolorButtons", "string",
"Foreground color of Buttons wherein #RRGGBB are hexadecimals."},
{"buttonNextTitle", "string", "Button Next title"},
{"buttonPreviousTitle", "string", "Button Previous title"},
{"imageList", "string",
"Relative URLs to images file separated by |."},
{"soundList", "string",
"Relative URLs to sound file separated by |."},
{"images", "int", "Total number of image files"},
{"delay", "int", "Delay in between images (in ms.)"},
};
return info;
}
/**
* The applet's initialization method.
*/
public void init() {
super.init();
theClip=null;
index=0;
// Parse the parameters from the HTML File
if(getParameter("appletSourceURL").equalsIgnoreCase("www.Free-Applets.com")) {
// required URL is proper
sounds = getParameter("soundlist");
if (sounds == null) sounds = "";
// list of sounds to be preloaded
LoadImages = new MediaTracker(this);
setBackground(parseColorString(getParameter("bgcolorApplet"), Color.black));
setForeground(parseColorString(getParameter("fgcolorApplet"), Color.white));
caption = getParameter("caption");
if (caption == null) caption = " www.Free-Applets.com";
buttonNextTitle = getParameter("buttonNextTitle");
if (buttonNextTitle == null) buttonNextTitle="NEXT";
buttonPreviousTitle = getParameter("buttonPreviousTitle");
if (buttonPreviousTitle == null) buttonPreviousTitle="PREVIOUS";
imageList = getParameter("imageList");
delay = Integer.valueOf(getParameter("delay")).intValue();
delay = (delay > 0) ? (delay * 1000) : 5000;
imagesNum = Integer.valueOf(getParameter("images")).intValue();
soundsNum = imagesNum;
slides = new Image[imagesNum];
startLoadingImages(); //Start loading the image files.
//Create the SlideShow layout
setLayout(new BorderLayout());
forward = new Button(buttonNextTitle);
forward.setBackground(parseColorString(getParameter("bgcolorButtons"),
Color.black));
forward.setForeground(parseColorString(getParameter("fgcolorButtons"),
Color.white));
backward = new Button(buttonPreviousTitle);
backward.setBackground(parseColorString(getParameter("bgcolorButtons"),
Color.black));
backward.setForeground(parseColorString(getParameter("fgcolorButtons"),
Color.white));
backward.disable();
auto = new Checkbox("AutoCycle Images");
auto.setForeground(parseColorString(getParameter("fgcolorButtons"),
Color.white));
auto.setState(false);
sound = new Checkbox("Sound On");
sound.setForeground(parseColorString(getParameter("fgcolorButtons"),
Color.white));
sound.setState(false);
title = new Label(caption);
Panel marquee = new Panel();
marquee.setLayout(new BorderLayout());
marquee.add("North", title);
Panel control = new Panel();
control.setLayout(new FlowLayout());
control.setBackground(parseColorString(getParameter("marqueeColor"),
parseColorString(getParameter("bgcolorApplet"), Color.white)));
control.add(auto);
control.add(sound);
control.add(backward);
control.add(forward);
setFont(new Font("Helvetica", Font.BOLD, 18));
add("South", marquee);
setFont(new Font("Helvetica", Font.PLAIN, 14));
marquee.add("South", control);
validate(); //validate the components
startLoadingSounds(soundsNum); //start loading the sound files
LoadImages.checkID(1,true); //Wait for the first image is loaded.
repaint();
}
}
//Paint the slide image on the screen
//And Account for missing images
public void paint(Graphics g) {
g.setColor(parseColorString(getParameter("bgcolorApplet"),
Color.black));
g.fillRect(0, 0, size().width, size().height);
if (LoadImages.isErrorAny()) return;
g.drawImage(slides[index], 80, 50, this);
if(sound.getState() == true) play(index);
}
/**
* This method returns a color value using RRGGBB hexadecimal parameters.
*/
private Color parseColorString(String colorString, Color dflt)
{
Color color;
try{
colorString = colorString.replace('#',' ').trim();
int R = Integer.valueOf(colorString.substring(0,2),16).intValue();
int G = Integer.valueOf(colorString.substring(2,4),16).intValue();
int B = Integer.valueOf(colorString.substring(4,6),16).intValue();
color = new Color(R,G,B);
}catch(Exception e){
color = dflt;
}
return color;
}
/**
* Play the sound file. The sound file URL is obtained from the
* "soundFileURL" parameter.
*/
public void play(int index) {
theClip = (AudioClip)soundList.elementAt(index);
if (theClip != null) theClip.play();
}
/**
* Threads run method used to enable the automatic showing of slides.
*/
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
Thread running = Thread.currentThread();
while (autoShow==running) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
break;
}
if (auto.getState() == true) {
if (index == (slides.length - 1))
index = 0;
else
index++;
forward.enable();
backward.enable();
repaint();
}
}
}
/**
* Threads start method used to initialize thread.
*/
public void start() {
Thread running = Thread.currentThread();
if (autoShow!=running) {
autoShow = new Thread(this);
autoShow.start();
}
}
/**
* This method loads the image clips.
*/
private void startLoadingImages ( ) {
showStatus("Start Loading Image Files...");
if (imageList != null) {
int indexStart;
int imageIndex=0;
String imageURL=null;;
int i=0;
while (imageList.indexOf('|', imageIndex) >=0) {
indexStart = imageIndex;
if ((imageIndex = imageList.indexOf('|', imageIndex)) <0) {
imageURL = imageList.substring(indexStart);
imageIndex = imageIndex + imageURL.length();
} else {
imageURL = imageList.substring(indexStart, imageIndex++);}
if (imageURL.length() > 0) {
slides[i] = getImage(getDocumentBase(), imageURL);
if (i > 0) LoadImages.addImage(slides[i], 1);
else LoadImages.addImage(slides[i], 0);
showStatus(imageURL + " Image Loaded");
i++;
}
}
imageURL = imageList.substring(imageIndex);
slides[i] = getImage(getDocumentBase(), imageURL);
LoadImages.addImage(slides[i], 1);
showStatus(imageURL + " Image Loaded");
try {
LoadImages.waitForID(1);
} catch (InterruptedException e) {
System.out.println("Image Loading Failed!");
}
}
return;
}
/**
* This method creates and loads the sounds.
*/
private void startLoadingSounds (int soundsNum ) {
showStatus("Start Loading Sound Files...");
int index=0;
int soundIndex=0;
int indexStart=0;;
while (sounds.indexOf('|', soundIndex) >=0) {
indexStart = soundIndex;
if ((soundIndex = sounds.indexOf('|', soundIndex)) <0) {
soundURL = sounds.substring(indexStart);
soundIndex = soundIndex + soundURL.length();
} else {
soundURL = sounds.substring(indexStart, soundIndex++);
}
if (soundURL.length() > 0) {
theClip = getAudioClip(getDocumentBase(), soundURL);
soundList.insertElementAt(theClip, index++);
showStatus(soundURL + " Sound Loaded");
}
}
soundURL = sounds.substring(soundIndex);
theClip = getAudioClip(getDocumentBase(), soundURL);
soundList.insertElementAt(theClip, index++);
showStatus(soundURL + " Sound Loaded");
}
/**
* applet's stop method will stop both the current sound clip and auto display.
*/
public void stop() {
try { //Attempt to stop the current sound clip.
if (theClip != null) theClip.stop();
} catch (Exception e) {
}
theClip = null; //And set the current sound clip to null.
autoShow= null;
auto.setState(false);
}
/**
* Update is called by the repaint.
*/
public void update(Graphics g) {
try {
paint(g);
} catch (ArrayIndexOutOfBoundsException e) {
if(index < 0) index = 0;
else if (index > imagesNum-1) index = imagesNum-1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -