user3180553
user3180553

Reputation:

Applet won't run (Says not initialized)

So basically, I've mentioned the HTML code in the java file, But um the applet wont execute for some reason, Help me

import java.awt.*;
import java.applet.*;
/*
<applet code = "demo.java" width=400 height=200>
<param name="txt" value ="Hey">
 </applet> 
*/

class demo extends Applet {
    public void paint(Graphics g)
    {
        String string = getParameter("txt");
        g.drawString(string, 29, 40);
        start();
    }

}

Upvotes: 0

Views: 735

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168825

<applet code = "demo.java" width=400 height=200>
<param name="txt" value ="Hey">
</applet> 

That code parameter is incorrect. It should be the fully qualified class name. Or..

<applet code = "demo" width=400 height=200>
<param name="txt" value ="Hey">
</applet> 

To compile & launch it in applet viewer from the command line, do something like:

prompt> javac demo.java
prompt> appletviewer demo.java // (see Note)

Note: Yes I do mean the .java extension. AppletViewer can launch an applet from that comment embedded in the source code. See the Applet info. page (at To compile and launch:) for another example.

Questions/Comments

Debugging

  1. Ensure the Java Console is configured to show for applets & JWS apps. If there is no output at the default level, raise it and try again.
  2. Copy/paste all error & exception output the console provides.

Code

  1. The applet code itself would be better to declare a String txt that is declared as a class attribute and initialized in the init() method, like this txt = getParameter("txt");. the paint(Graphics) method might be called many times.
  2. Any time a paint(..) method is overridden, it should immediately call super.paint(..) (for the BG color, if nothing else).

Questions

  1. Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.
  2. Why AWT rather than Swing? See my answer on Swing extras over AWT for many good reasons to abandon using AWT components.

Upvotes: 1

RKC
RKC

Reputation: 1874

you should give the class name not java file name.Go through applet tutorials for good understanding.

Try this,

import java.awt.*;
import java.applet.*;

public class demo extends Applet {

    public void run(){
        repaint();
    }   

    public void paint(Graphics g)
    {
        String string = getParameter("txt");
        g.drawString(string, 29, 40);

    }
}

/*
<html>
<applet code = "demo.java" width=400 height=200>
<param name="txt" value ="Hey">
 </applet> 
</html>
*/

Upvotes: 0

Related Questions