how to get a image with using java appletviewer?

hi,i complete my java program 😔no error for compiling section😔but applet window was started there are no image result😔
/*


*/
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

  • dipen30
    dipen30
    put image in the folder where imageA.java file is saved and try the program.
  • sookie
    sookie
    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:

    /*
    
    
    */
    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 !

You are reading an archived discussion.

Related Posts

😒Please suggest me some project under title "variable frequency drives at higher frequency"
hai i have complete my multi threading java program .compiling section 2 errors class ThreadA extends Thread { public void run() { try { for(int i=0; i< 5; i++) {...
I've been wondering whether I need to install a desktop mail client to manage all my email. To tell you frankly, I'm thinking I should stick to all web-mail instead...
hey frnz. m in final year of electrical engineering, nd i need to submit my major project as soon possible. so plz, if any1 among u hav any idea, plz...
Original Article Link: https://www.crazyengineers.com/know-your-cean-–-ms_cs/ Your questions, comments in this thread 😀