Play slideshows and animations
Use examples to illustrate how to play slides and animations.
[Example] The small application first reads the slides into an array and stores them, then clicks the mouse to change the slides and display them one by one.
import java.applet.*import java.awt.*;import java.awt.event.*;public class Example7_7 extends Applet implements MouseListener{ final int number = 50; //Assume there are 50 slides int count = 0; Image [] card = new Image[number]; public void init(){ addMouseListener(this); for (int i = 0; i < number; i++){ card[i] = getImage(getCodeBase(), "DSC0033" + i + ".jpg"); } } public void paint(Graphics g){ if ((card[count]) != null) g.drawImage(card[count], 10 , 10, card[count].getWidth(this),card[count].getHeitht(this), this); } public void mousePressed(MouseEvent e){ count = (count + 1) % number; //Loop and display repaint(); } public void mouseRelease(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(Mouse Event e){} public void mouseClicked(MouseEvent e){}}
[Example] The applet explains how to play animations. It requires the pictures to be played and the applet to be placed in the same directory. The program creates the effect of displaying animations by quickly displaying a group of pictures. The small application uses threads to control the display of animated pictures one by one.
import java.applet.*;import java.awt.*;import java.awt.event.*;public class Example7_8 extends Applet implements Runnable{ final int number = 50; int count = 0; Thread mythread; Image[] pic = new Image[number]; public void init(){ setSize(300, 200); for (int i = 0; i <= number; i++){ //Load animated pictures pic[i - 1] = getImage(getCodeBase(), "DSC0033" + i + ".jpg"); } } public void start(){ mythread = new Thread(this); //Create A thread mythread.start(); //Start thread execution} public void stop(){ mythread = null; } public void run(){ //Thread execution code while (true){ repaint(); count = (count + 1) % number; //Change the displayed picture number try{ mhythread.sleep(200); } catch (InterruptedExeception e){} } } public void paint(Graphics g){ if ((pic[count] != null) g.drawImage(pic[count], 10, 10, pic[count].getwidth(this), pic[count].getHeight(this), this); }}
play sound
There are many audio formats in the old base of Java language: au, aiff, wav, midi, rfm, etc. To play audio files in an applet, you can use the AudioClip class, which is defined in the java.applet.AudioClip class library. The applet first creates an AudioClip object and initializes it using the getAudioClip() method. The code form is as follows:
AudioClip audioClip = getAudioClip(getCodeBase(),"myAudioClipFile.au");
If you want to obtain an audio file from the Internet, you can use the method getAudioClip(URL url, String name) to obtain a playable audio object based on the URL address and audio file name.
There are three methods to control the playback of sound: play() to play the sound, loop() to loop the playback and stop() to stop the playback.
[Example] A small application that can play sounds.
import java.applet.*;import java.awt.*;import java.awt.event.*;public class Example7_9 extends Applet implements ActionListener{ AudioClip clip; //Declare an audio object Button buttonPlay, buttonLoop, buttonStop; public void init (){ clip = getAudioClip(getCodeBase(), "2.wav"); //Create an audio object based on the sound file 2.wav at the address of the program, //The getCodeBase() method of the Applet class can obtain the URL address of the html page where the applet is located. buttonPlay = new Button("Start playing"); buttonLoop = new Button("Loop play"); buttonStop = new Button("Stop playing"); buttonPlay.addActionListener(this); buttonStop.addActionListener(this); buttonLoop.addActionListener (this); add(buttonPlay); add(buttonLoop); add(buttonStop); } public void stop(){ clip.stop(); //Stop playing when leaving this page} public void actionPerformed(ActionEvent e){ if (e.getSource() == buttonPlay){ clip.play(); } else if (e.getSource() == buttonLoob){ clip.loop(); } else if (e.getSource() == buttonStop){ clip.stop(); } }}
[Example] If the sound file is large or the network speed is slow, it will affect the initialization of the mini program. This can be solved with multi-threading technology. The creation of the audio object is completed in a lower-level thread, that is, the sound file is loaded in the background and played in the foreground.
import java.applet.*;import java.awt.*;import java.awt.event.*;public class Hanoi extends applet implements Runnable, ActionListener{ AudioClip clip; //Declare an audio object textField text; Thread thread; Button buttonPlay , buttonLoop, buttonStop; public void init(){ thread = new Thread(this); //Create a new thread thread .setPriority(Thread.MIN_PRIORITY); buttonPlay = new Button("Start playing"); buttonLoop = new Button("Loop play"); buttonStop = new Button("Stop playing"); text = new textField(12); buttonPlay .addActionListener(this); buttonStop.addActionListener(this); buttonLoop.addActionListener(this); add(buttonPlay); add(buttonLoop); add(buttonStop); add(text); } public void start(){ thread.start(); } public void stop(){ clip.stop(); } public void actionPerformed(ActionEvent e){ if (e.getSource() == buttonPlay(){ clip.play(); } else if (e.getSource() == buttonLoop(){ clip.loop(); } else if (e.getSource() == buttonStop(){ clip.stop(); } } public void run(){ //Create audio object clip in thread thread = getAudioclip(getCodeBase(), "2.wav"); text.setText("Please wait"); if(clip ! = null){ buttonPlay.setBackground(Color.red); buttonLoop.setBackground(Color.green); text.setText("You can play"); } //After obtaining the audio object, the notification can be played}}