0

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());
}
}
Microsoft Developer
  • 5,229
  • 22
  • 93
  • 142

2 Answers2

1

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.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1
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
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • What does "../images/Beagle.jpg" mean in picture = getImage(getCodeBase(), "../images/Beagle.jpg"); What is .. before/imges – Microsoft Developer Oct 10 '11 at 05:26
  • `..` in an URL means 'parent directory`. The codebase is not specified in the HTML so it defaults to the same as the document base (the `applet` directory). We need to retrieve the image from the `images` directory which is 'beside' the `applet` directory. See the list structure at the bottom of the post, it shows the directory/file structure that would work for the code shown. – Andrew Thompson Oct 10 '11 at 05:31
  • @ Andrew Thompson Still not clear with the explanation. Do you mean that I need to store the image directory in the same folder as of applet directory? I want to display image from any directory of my local disk. Can i do this with this code?? Sorry but I am new in JAVA.. – Microsoft Developer Oct 10 '11 at 06:30
  • The local disk is no good. Only a signed and trusted applet can load files from the local file-system, and then it is the local file system of the computer that the *end user* is using. If the image & clip is to be used in a sand-boxed applet, it must be somewhere on the *same site* as the applet. – Andrew Thompson Oct 10 '11 at 06:38
  • .You mean to say I need to store .class file and image directory in the same folder?? – Microsoft Developer Oct 10 '11 at 06:42
  • Folders are more something that relates to the local disks. `C:`, `D:` etc. Where are you hosting the applet? What URL? – Andrew Thompson Oct 10 '11 at 07:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4130/discussion-between-andrew-thompson-and-chirag-fanse) – Andrew Thompson Oct 10 '11 at 07:06