Assasins
Assasins

Reputation: 1633

Java applet gives an error in the browser

I have been reading a book named "Teach Yourself Java in 21 days" by Laura Lemay.I guess the book is a bit old and has been written in early days of java.It describes about making applets as below.

import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

public class HelloAgainApplet extends java.applet.Applet {

    Font f = new Font("TimesRoman", Font.BOLD, 36);

    public void paint(Graphics g) {
        g.setFont(f);
        g.setColor(Color.red);
        g.drawString("Hello again!", 5, 50);
    }
}

This applet overrides paint(), one of the major methods described in the previous section. Because the applet doesn't actually do much (all it does is print a couple of words to the screen), and there's not really anything to initialize, you don't need a start() or a stop() or an init() method.

The HTML is as follows:

<HTML>
<HEAD>
<TITLE>Another Applet</TITLE>
</HEAD>
<BODY>
<P>My second Java applet says:
<APPLET CODE="HelloAgainApplet.class" WIDTH=200 HEIGHT=50>
</APPLET>
</BODY>
</HTML>

I have done the same to practice it, but my browser gives an error as below. Why is that?

enter image description here

Upvotes: 1

Views: 812

Answers (1)

Branislav Lazic
Branislav Lazic

Reputation: 14806

1) Put your html and class file in same folder.

2) Your html file should look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Another Applet</title>
</head>
<body>
<p>My second Java applet says:
    <applet code="HelloAgainApplet.class" width=200 height=50>
    </applet>
</body>
</html>

That should fix the problem.

Upvotes: 1

Related Questions