how to get a image with using java appletviewer?

saravanatamil

saravanatamil

@saravanatamil-6vBdKR Oct 20, 2024
hi,i complete my java program 😔no error for compiling section😔but applet window was started there are no image result😔
/*<applet code="imageA" width=248 height=146>
<param name="img" value="C:\Documents and Settings\All Users.WINDOWS\Documents\My Pictures\Sample Pictures\Sunset.jpg">
</applet>
*/
import java.awt.*;
import java.applet.*;
public class imageA extends Applet
{
Image img;
public void init()
{
img=getImage(getDocumentBase(),getParameter("img") );
}
public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
}
}

give me the correct solution😕

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • dipen30

    dipen30

    @dipen30-hGOPpa Aug 24, 2009

    put image in the folder where imageA.java file is saved and try the program.
  • sookie

    sookie

    @sookie-T06sFW Aug 24, 2009

    Hi saravantamil,

    The reason why your above program is not working is simple your imageA.java is in different directory as compared to the path of the directory where your image exists.

    getDocumentBase() - This method of #-Link-Snipped-# gives you the URL of the document where your imageA.java exists( For e.g. in my local it is at I:\JavaSrcCode)

    getParameter("img") - This gives the parameter specified in APPLET tag. It takes path "C:\Documents and Settings\All Users.WINDOWS\Documents\My Pictures\Sample Pictures\Sunset.jpg"


    Now what getImage(URL url,String name) method does? It first looks at the path returend by getDocumentBase() which is I:\>JavaSrcCode and then using path retreived from getParameter("img") looks at the location relative to getDocumentBase() [e.g. I:\JavaSrcCode] so as a result , it do not find the image. Quite obvious because it is getting wrong path location.

    Now to solve the problem. One way is you just directly copy the image from your "C:\Documents and Settings\All Users.WINDOWS\Documents\My Pictures\Sample Pictures\Sunset.jpg" location to the location where your imageA.java exists and change the value of "img" parameter in APPLET code as "Sunset.jpg" [Note- give only the name of the image] .Other better way that I would always like to do is

    Step 1: Make an images folder in the location where my Java classes exist [I:\JavaSrcCode\images] and then copy my image in that folder
    Step 2: Change the value of "img" parameter in APPLET code.
    Step 3: So now program that would work will look something like below:

    /*<applet code="imageA" width=248 height=146>
    <param name="img" value="images\Sunset.jpg">
    </applet>
    */
    import java.awt.*;
    import java.applet.*;
    public class imageA extends Applet
    {
    Image img;
    public void init()
    {
    img=getImage(getDocumentBase(),getParameter("img"));
    
    }
    public void paint(Graphics g)
    {
    g.drawImage(img,0,0,this);
    }
    }
    
    Output: An applet window opens with the specified image in it.

    PS: Always make the first letter of Java classes as CAPITAL.

    Thanks !