0

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();
    }

}
Harshil P
  • 21
  • 5

2 Answers2

1
<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.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

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>
*/
RKC
  • 1,834
  • 13
  • 13
  • Still says, Not initialized. – Harshil P Mar 23 '14 at 07:49
  • @HarshilP Did you compile your source into a `.class` file? Did you create a separate HTML file? Because your example looks like you *didn't* compile your code and it also looks like you placed the HTML in a comment instead of in its own file. You should look at the tutorial I mentioned. – Jason C Mar 23 '14 at 07:54
  • 1
    @JasonC *"Did you create a separate HTML file? Because your example looks like .. you placed the HTML in a comment instead of in its own file."* That can work for applet viewer use. See my answer for more details. – Andrew Thompson Mar 24 '14 at 06:14