floyd10325 floyd10325 - 4 months ago 7
HTML Question

Why doesn't the applet run in browser but in IDE?

I've been browsing this site for some time, but this is my first official post.

I'm fairly new to Java (just getting started with applets), and I'm having trouble getting my assigned applet to run in a browser. Everything runs fine in Eclipse, but there is nothing but blank space when I open my .html file.

I'd greatly appreciate if anyone could look over what I have below and offer their expertise. I'm sure I've made some noob mistake and haven't been able to locate it yet. Thanks.

Java source code:

// Import necessary classes.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

class Eye extends Thread
{
public static int mouseXcoordinate;
public static int mouseYcoordinate;
private static final int EYEWIDTH = 50;
private static final int EYEHEIGHT = 75;
private static final int IRISSIZE = 30;
private static final int PUPILSIZE = 12;
private Color irisColor;
private static final int SMALLXRAD = (EYEWIDTH - IRISSIZE)/2;
private static final int SMALLYRAD = (EYEHEIGHT - IRISSIZE)/2;
private int x, y;
private double newX, newY;
private Graphics g;

// Constructor for a new eye.
public Eye(int x, int y, Graphics g)
{
this.g = g;
this.x = x;
this.y = y;

// Choose random colors for the iris of the eyes.
irisColor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
}

// Draw the eye, in detail.
private void draw()
{
synchronized(g)
{
// Erase any old eye color.
g.setColor(Color.white);
g.fillOval(x - EYEWIDTH/2, y - EYEHEIGHT/2, EYEWIDTH, EYEHEIGHT);
// Draw the iris and set the color.
g.setColor(irisColor);
g.fillOval((int)newX - IRISSIZE/2 + 1, (int)newY - IRISSIZE/2 + 1, IRISSIZE, IRISSIZE);
// Draw the pupil and set the color.
g.setColor(Color.black);
g.fillOval((int)newX - PUPILSIZE/2 + 1, (int)newY - PUPILSIZE/2 + 1, PUPILSIZE, PUPILSIZE);
// Draw the eye outline.
g.drawOval(x - EYEWIDTH/2, y - EYEHEIGHT/2, EYEWIDTH, EYEHEIGHT);
}
}

// Continuously calculate the current coordinates and redraw the eyes to follow the coordinates.
public void run()
{
for(;;)
{
updateCoordinates();
draw();
try
{
sleep(50);
}
catch (InterruptedException e)
{}
}

}

// Update the mouse coordinates.
private void updateCoordinates()
{

if (mouseXcoordinate == x)
{
newX = mouseXcoordinate;

if (Math.abs(y - mouseYcoordinate) >= SMALLYRAD)
{
if ( (y - mouseYcoordinate) > 0 )
newY = y - SMALLYRAD;
else
newY = y + SMALLYRAD;
}
else
newY = mouseYcoordinate;
return;
}

// Find intersection point of line to mouse with eye ellipse
double slope = (double)(mouseYcoordinate - y) / (double)(mouseXcoordinate - x);
double numerator = SMALLXRAD * SMALLXRAD * SMALLYRAD * SMALLYRAD;
double denominator = SMALLYRAD * SMALLYRAD + slope * slope * SMALLXRAD * SMALLXRAD;
newX = Math.sqrt(numerator / denominator);
newY = slope * newX;

// Choose appropriate intersection point
if (mouseXcoordinate < x)
newX = -Math.abs(newX);
else
newX = Math.abs(newX);

if (mouseYcoordinate < y)
newY = -Math.abs(newY);
else
newY = Math.abs(newY);

newX += x;
newY += y;

if ( (double)(mouseXcoordinate - x)*(mouseXcoordinate - x) / (SMALLXRAD * SMALLXRAD) + (double)(mouseYcoordinate - y)*(mouseYcoordinate - y) / (SMALLYRAD * SMALLYRAD) < 1 )
{
newX = mouseXcoordinate;
newY = mouseYcoordinate;
}
}
}

@SuppressWarnings("serial")
public class BurleighWatchMe extends Applet
{
static final int NUM_EYES = 50;
Eye[] eyes = new Eye[NUM_EYES];
int count = -1;
int width, height;

// Initializes the applet by loading coordinates and starting two eye threads.
public void init()
{
addMouseMotionListener( new MouseMotionListener()
{
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e)
{
Eye.mouseXcoordinate = e.getX();
Eye.mouseYcoordinate = e.getY();
repaint();
}
});
}

// Starts the eye threads.
public void start()
{
if (count == -1)
{
width = getSize().width;
height = getSize().height;
final Graphics g = getGraphics( );
eyes[++count] = new Eye(width/4, height/2, g);
eyes[count].start();
eyes[++count] = new Eye(3*width/4, height/2, g);
eyes[count].start();
}

repaint();
}

// Redraws a border around the applet.
public void update(Graphics g)
{
g.drawRect(0,0,width-1,height-1);
}
}


HTML:

<html>
<head>
<title>Watch Me Eyes</title>
</head>
<body>
Move your mouse pointer over these<br />eyes, and watch them follow it!
<p />
<applet code="BurleighWatchMe.class" width="400" height="400">
</applet>
</body>
</html>

Answer

In the HTML code, you've missed out on adding the codebase attribute of the applet tag. Codebase refers to the directory containing your applet. So IF THE APPLET IS NOT IN THE SAME DIRECTORY AS THE HTML PAGE then you have to add the codebase attribute inside the applet tag. It would take the form below:

<applet>
    codebase="E:\Projects\" code="BurleighWatchMe.class" height="400" width="400"
</applet>

assuming that the BurleighWatchMe.class exists in the directory E:\Projects.

However, if the BurleighWatchMe.class file exists in the same directory as the HTML page and you're still seeing a blank page, then it probably might have been caused because the JRE on your system doesn't allow unknown applets. What you have to do is edit the settings to allow local applets. To do that, go to Start > Configure Java. Go to the Security tab of the Java settings dialog box. In the lower half of the window, you can see a Site Exception List. Beside that, there's a button called Edit Site List.... Click on that button. You'll see a list of sites whose applets you can run. Click on Add to add a new location. In the new field, type file:/// and press enter,. It'll show you a warning; click on Continue. Now click on OK and quit the Configure Java program.

Next, restart the browser. You'll see a dialog box when the applet loads wherein you have to select the Run option. You should now be able to run the applet on your browser.

Please note that Google Chrome no longer supports applets. The only browsers I know of that support applets are Firefox and Internet Explorer. I tested it and it worked on these two browsers.

See if it solves your problem.

Comments