Home>

Play slideshows and animations

Use examples to show how to play slideshows and animations.

[Example] The small application first reads the slides into an array and stores them.Click the mouse to change the slideshow,Show 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;//Assuming 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;//cyclically display one by one
    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 the method of playing animation,Images and applets that require playback are placed in the same directory,The program creates an animation effect by quickly displaying a group of pictures.Applets use threads to control the progressive display of animated pictures.

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 the animation image
      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's 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 the Java language:au, aiff, wav, midi, rfm, etc. Applets want to play audio files,You can use the class audioclip, which is defined in the java.applet.audioclip class library.The applet first creates an audioclip object and initializes it with the getaudioclip () method.The code form is as follows:

 audioclip audioclip=getaudioclip (getcodebase (), "myaudioclipfile.au");

If i want to get audio files from the web,You can use the method getaudioclip (url url, string name) to obtain a playable audio object according to the url address and audio file name.

There are 3 ways to control sound playback:play () plays sound,loop () loop playback and stop () stop playback.

[Example] A small application that can play sound.

import java.applet. *;
import java.awt. *;
import java.awt.event. *;
public class example7_9 extends applet implements actionlistener {
  audioclip clip;//Declares 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,    //getcodebase () method of the applet class can get the url address of the html page where the applet is located.
    buttonplay=new button ("Start Play");
    buttonloop=new button ("loop play");
    buttonstop=new button ("Stop playback");
    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 applet.This can be solved with multi-threading technology.Create audio objects in a lower-level thread,The sound file is loaded from the background,Play at the front desk.

import java.applet. *;
import java.awt. *;
import java.awt.event. *;
public class hanoi extends applet implements runnable, actionlistener {
  audioclip clip;//Declares 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 Play");
    buttonloop=new button ("loop play");
    buttonstop=new button ("Stop playback");
    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 in thread
    clip=getaudioclip (getcodebase (), "2.wav");
    text.settext ("Please wait");
    if (clip!=null) {
      buttonplay.setbackground (color.red);buttonloop.setbackground (color.green);text.settext ("You can play now");
    } //Notification can be played after getting the audio object
  }
}
  • Previous Example explanation of Validation application of form validation plugin
  • Next Simple example of js implementing click to switch checkbox background image