user1628311
user1628311

Reputation: 186

browser doesn't show java applet

I'm just starting with java applets and found this website that gives a tutorial. I entered all the code but the applet didn't show up in my browser: HelloWorld.java

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

public class HelloWorld extends Applet
{
    public void paint (Graphics g)
    {
    g.drawString("Hello World!", 300, 300);
    }
}

And this is my HTML file

<HTML>
<HEAD>
<TITLE>Hello World Applet</TITLE>
</HEAD>
<BODY>
<H1>Hello World Applet</H1>
<applet code="HelloWorld.class" WIDTH=300 HEIGHT=300></applet>
</BODY>
</HTML>

I have compiled the java file (HelloWorld.class) and saved all the files on my desktop. I'm using the latest version of safari on mac osx mountain lion. I tried it in google chrome and it said to install a plugin and i did but nothing more happened?

Upvotes: 3

Views: 4022

Answers (3)

Azim
Azim

Reputation: 1724

I know that this is already answered but I think the better answer is that the tag applet is deprecated and object should be used.

See the original answer here

Upvotes: 0

jatin3893
jatin3893

Reputation: 856

(Just Note: These things take place in windows and a few in Ubuntu. Just check the following if any of the following applies to your problem)Try the following things:
1st- In HTML file, change the body tag to:

<body bgcolor = red>

SO that you know your applet is running on the browser if you find a white rectangle of the dimensions you mentioned but the applet is not running as expected because of some problem with the browser/jre.

2nd- Browsers that we use are generally 32 bit browsers. Do check if you have the 32 bit version of java installed in case your browser is a 32 bit-one.

3rd- To check whether your applet is actually created, try running the html file using appletviewer using the terminal.

run the following either in cmd/terminal whichever you use:

appletviewer filename.html

or if you are using eclipse/netbeans, they usually have a appletViewer integrated along with the IDE so they run the appletviewer directly.

Upvotes: 3

Rob Andren
Rob Andren

Reputation: 88

It looks like the problem is that your x and y coordinates in your drawstring method are the same as the height and width of your applet, so the text is not allowed to appear when rendered in a browser.

In your html, change the height and width to (for example) 400 each, and you should see the text.

Upvotes: 1

Related Questions