Sunday, 12 August 2012

Hi friends,
Many of beginners using netbeans find it hard to display a image in applet

following are questions which i found regularly on forums

1. i am not able to display a image in applet using netbeans
2. how to display a image in applet in netbeans
3. where to save the image file in netbeans  folder
++++

So here we go

1. start netbeans ide
2. create a new project choice -> Java Application
3. Name the project as WelcomeApplet
4. paste the following code in the class

----------------------------------------------------------------------------
package (your package name generated by netbeans);

import java.applet.Applet;
import java.util.Date;
import java.awt.*;

public class WelcomeApplet extends Applet {
    @Override
public void paint(Graphics g)
{
    Image img;
    Date d = new Date();
    g.drawString("Welcome to daily planner",150,40);
    g.drawString("daily planner fot xyz",150,60);
    g.drawString("www.xyz.com",150,80);
    g.drawString(d.toString(),150,100);
    img = getImage(getDocumentBase(),"image.gif");
    g.drawImage(img,50,40,this);
   
}
}
--------------------------------------------------------------------------------
5. Create a image and save it in the src or classes folder
6. Now just go ahead and run this applet you will see the text not image ?

it's confusing here is where we get confused so just do one thing

7. save the image in
C:\Documents and Settings\Java\My Documents\NetBeansProjects\WelcomeApplet\build

path may be different on your computer

8. Now run the applet the image is displayed in the applet

We are done
Here is a screen shot.... where to save the image