Microsoft Developer
Microsoft Developer

Reputation: 5459

Display image and play audio clip in applet

I want to display image in an applet and play audio clip. I wrote the following code but its not working. I have written code just for image but dont have any idea for audio clip. Please let me know where I am making mistake..

import java.applet.*;
import java.awt.*;
import java.net.*;
/*
<applet code="Showimage" width = 400 height = 400>
</applet>
*/
public class Showimage extends Applet
{
URL codb;
Image picture;
public void init()
{
    codb = getCodeBase();
    picture = getImage(codb, "Choti.jpg");
}
public String getAppletInfo()
{
    return "Hi...";
}
public void start() {   }
public void paint(Graphics g)
{
    g.drawImage(picture, 10, 10, this);
    showStatus(getAppletInfo());
}
}

Upvotes: 0

Views: 9369

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168835

import java.applet.*;
import java.awt.*;
import java.net.*;
/*
<applet code="Showimage" width = 400 height = 400>
</applet>
*/
public class Showimage extends Applet
{
    URL codb;
    Image picture;
    AudioClip clip;

    public void init()
    {
        picture = getImage(getCodeBase(), "../images/Beagle.jpg");

        clip  = getAudioClip(getDocumentBase(), "sound/woof.wav");
    }

    public String getAppletInfo()
    {
        return "Hi...";
    }

    public void start() { 
        clip.play();
        showStatus(getAppletInfo());
    }

    public void paint(Graphics g)
    {
        g.drawImage(picture, 10, 10, this);
    }
}

Would find the image & sound with this directory structure.

  • images
    • Beagle.jpg
  • applet
    1. sound
      • woof.wav
    2. applet.html

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94643

An applet class and image "Choti.jpg" must be in same folder.

Use getImage(java.net.URL) method to get an image from other location/folder and use play(URL,string) or play(URL) method to play audio clip. For more detail visit applet tutorial.

Upvotes: 1

Related Questions