I`m trying to write a sample program in java, but the book doesn`t explain how I should write my html code to execute it, I don`t even know what I should use to write a HTML code, I looked online, but didn`t find much.

this is the java code:

import java.awt.Graphics;
import javax.swing.JApplet;

class WelcomeApplet extends JApplet
{
	public void paint (Graphics g)
	{
		super.paint (g);
		g.drawString ("Welcome to Java Programming!", 25, 25);
		
	}
}

and this is the HTML:

<html>
<applet code = "WelcomeApplet.class" width - "300" height - "45">
</applet>
</html>

Thanks

>I looked online, but didn`t find much.

Read this doc - http://java.sun.com/docs/books/tutorial/deployment/applet/index.html

Please make correction:

1. Applet subclass must be public.

.....
public class WelcomeApplet extends JApplet
{
    ....
    ...	
}

2. Create html document using any text editor.

sample.htm

<html>
<applet code="WelcomeApplet" width="300" height="45">
</applet>
</html>
import java.awt.Graphics;
import javax.swing.JApplet;


class WelcomeApplet extends JApplet {

	public void paint (Graphics g) {

		super.paint (g);
		g.drawString ("Welcome to Java Programming!", 25, 25);
	}
}
<html>
<applet code="WelcomeApplet" width="300" height="45"></applet>
</html>

The above html code works fine. For viewing the output of the above applet you have to create an html page. You can give any name to this html file but that html file must be saved in the same folder in which your .class file resides. Here your .class file is WelcomeApplet.class. Then you can open the html file in a web browser and see the output.

Or else

import java.awt.Graphics;
import javax.swing.JApplet;

//<applet code="WelcomeApplet.class" width="300" height="45">
class WelcomeApplet extends JApplet {

	public void paint (Graphics g) {

		super.paint (g);
		g.drawString ("Welcome to Java Programming!", 25, 25);
	}
}

You will save the above code as WelcomeApplet.java and will compile it to create the WelcomeApplet.class file. Now to see the output of this file: (1) Take Command Prompt (2) go to the folder where the WelcomeApplet.class file is there in the command prompt (3) type the command

appletviewer WelcomeApplet.java

and press enter. Here you will see the output. If you find any problem please let me know. Have a great day!!

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.