import javax.swing.*;
import java.awt.*;
public class ImageObsApplet extends JApplet implements Runnable
{
DrawingPanel fDrawingPanel;
Image fImg;
int fImageNum=0;
String fMessage="Loading..";
boolean fDoneLoadingImage=false;
public void init()
{
Container content_pane=getContentPane();
fDrawingPanel=new DrawingPanel(this);
content_pane.add(fDrawingPanel);
fImg=getImage(getCodeBase(),"Sunset.jpg");
}
public void start()
{
Thread thread=new Thread(this);
thread.start();
}
public void run()
{
int width=fImg.getWidth(this);
if(width>=0)
{
fDoneLoadingImage=true;
repaint();
return;
}
while(!fDoneLoadingImage)
{
try
{
Thread.sleep(500);
}
catch(InterruptedException ie)
{
}
repaint();
}
}
public boolean imageUpdate(Image img,int info_flags,int x,int y,int w, int h)
{
if(info_flags!=ALLBITS)
return true;
else
{
fDoneLoadingImage=true;
return false;
}
}
}
class DrawingPanel extends JPanel
{
ImageObsApplet fParent=null;
DrawingPanel(ImageObsApplet parent)
{
fParent=parent;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(fParent.fDoneLoadingImage)
g.drawImage(fParent.fImg,10,10,this);
else
g.drawString(fParent.fMessage,10,10,this);
}
}
Friends this code compiles successfully.....But when i execut giving command java ImageObsApplet it throws error in exception main...can any of u rectify it please...

stop doubting the language and start learning it. The Sun Java tutorial is a good place to start.

Member Avatar for electron33

Hi. Your applet runs perfect now. By the way. You can not run Applet with Java call.
Example: Wrong -> c:\java AppletExample. You will get en exception. Try put it inside
<Applet></Applet> tags in an HTML page.

Here is the HTML code

<HTML>
<HEAD>
   <TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>

<!--
*** GENERATED applet HTML launcher - DO NOT EDIT IN 'BUILD' FOLDER ***

If you need to modify this HTML launcher file (e.g., to add applet parameters), 
copy it to where your applet class is found in the SRC folder. If you do this, 
the IDE will use it when you run or debug the applet.

Tip: To exclude an HTML launcher from the JAR file, use exclusion filters in 
the Packaging page in the Project Properties dialog.

For more information see the online help.
-->

<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>

<P>
<APPLET codebase="classes" code="AppletExample.class" width=350 height=200></APPLET>
</P>

<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>
</BODY>
</HTML>

and here is the Applet

/**
 *
 * @author Kjell
 */
import javax.swing.*;
import java.awt.*;

public class AppletExample extends JApplet implements Runnable {
    ImageApplet    fDrawingPanel;
    Image           fImg;
    int             fImageNum=0;
    String          fMessage="Loading..";
    boolean         fDoneLoadingImage=false;
    
    public void init() {
        Container content_pane = getContentPane();
        fDrawingPanel = new ImageApplet(this);
        content_pane.add(fDrawingPanel);
        fImg = getImage(getCodeBase(),"Vinter.jpg");
    }
    public void start() {
        Thread thread = new Thread(this);
        thread.start();
    }
    public void run() {
        int width = fImg.getWidth(this);
        if(width>=0) {
            fDoneLoadingImage=true;
            repaint();
            return;
        }
        while(!fDoneLoadingImage) {
            try {
                Thread.sleep(500);
            } catch(InterruptedException ie) {
            }
            repaint();
        }
    }
    public boolean imageUpdate(Image img,int info_flags,int x,int y,int w, int h) {
        if(info_flags!=ALLBITS)
            return true;
        else {
            fDoneLoadingImage=true;
            return false;
        }
    }
}

class ImageApplet extends JPanel {
    AppletExample fParent=null;
    ImageApplet(AppletExample parent) {
        fParent = parent;
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(fParent.fDoneLoadingImage){
            g.drawImage(fParent.fImg,10,10,this);
        }
        else{
            g.drawString(fParent.fMessage,10,10);
        }
    }
}

Keep up with the good work

kjell is right!
mark the removal of 'this' in the drawString method.
amazing that your compiler allowed that...

maybe it's an idea to download jGrasp (www.jgrasp.com), an easy-to-use developing environment for java, with somewhat less overhead than netbeans.
It offers the possibility to run an applet without having to embed it in HTML.

good luck,
peter

Hi. Your applet runs perfect now. By the way. You can not run Applet with Java call.
Example: Wrong -> c:\java AppletExample. You will get en exception. Try put it inside
<Applet></Applet> tags in an HTML page.

Here is the HTML code

<HTML>
<HEAD>
   <TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>

<!--
*** GENERATED applet HTML launcher - DO NOT EDIT IN 'BUILD' FOLDER ***

If you need to modify this HTML launcher file (e.g., to add applet parameters), 
copy it to where your applet class is found in the SRC folder. If you do this, 
the IDE will use it when you run or debug the applet.

Tip: To exclude an HTML launcher from the JAR file, use exclusion filters in 
the Packaging page in the Project Properties dialog.

For more information see the online help.
-->

<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>

<P>
<APPLET codebase="classes" code="AppletExample.class" width=350 height=200></APPLET>
</P>

<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>
</BODY>
</HTML>

and here is the Applet

/**
 *
 * @author Kjell
 */
import javax.swing.*;
import java.awt.*;

public class AppletExample extends JApplet implements Runnable {
    ImageApplet    fDrawingPanel;
    Image           fImg;
    int             fImageNum=0;
    String          fMessage="Loading..";
    boolean         fDoneLoadingImage=false;
    
    public void init() {
        Container content_pane = getContentPane();
        fDrawingPanel = new ImageApplet(this);
        content_pane.add(fDrawingPanel);
        fImg = getImage(getCodeBase(),"Vinter.jpg");
    }
    public void start() {
        Thread thread = new Thread(this);
        thread.start();
    }
    public void run() {
        int width = fImg.getWidth(this);
        if(width>=0) {
            fDoneLoadingImage=true;
            repaint();
            return;
        }
        while(!fDoneLoadingImage) {
            try {
                Thread.sleep(500);
            } catch(InterruptedException ie) {
            }
            repaint();
        }
    }
    public boolean imageUpdate(Image img,int info_flags,int x,int y,int w, int h) {
        if(info_flags!=ALLBITS)
            return true;
        else {
            fDoneLoadingImage=true;
            return false;
        }
    }
}

class ImageApplet extends JPanel {
    AppletExample fParent=null;
    ImageApplet(AppletExample parent) {
        fParent = parent;
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(fParent.fDoneLoadingImage){
            g.drawImage(fParent.fImg,10,10,this);
        }
        else{
            g.drawString(fParent.fMessage,10,10);
        }
    }
}

Keep up with the good work

Thank u sir...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.