我已经浏览过这个网站一段时间了,但这是我的第一篇官方帖子。
我是Java的新手(刚刚开始使用applet),而且我在分配的applet在浏览器中运行时遇到了麻烦。 Eclipse中的所有内容都运行正常,但是当我打开.html文件时,只有空格。
如果有人能够查看下面的内容并提供他们的专业知识,我将非常感激。我确定我已经犯了一些noob错误,并且还没能找到它。感谢。
Java源代码:
// 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>
答案 0 :(得分:0)
在HTML代码中,您错过了添加codebase
标记的applet
属性。代码库是指包含applet的目录。所以如果APPLET与HTML PAGE不在同一目录,那么您必须在codebase
标记内添加applet
属性。它将采用以下形式:
<applet>
codebase="E:\Projects\" code="BurleighWatchMe.class" height="400" width="400"
</applet>
假设 E:\ Projects 目录中存在 BurleighWatchMe.class 。
但是,如果 BurleighWatchMe.class 文件与 HTML 页面位于同一目录中,并且您仍然看到一个空白页面,那么它可能会是因为您系统上的 JRE 不允许使用未知的applet。您需要做的是编辑设置以允许本地小程序。为此,请转到Start > Configure Java
。转到Java设置对话框的Security
选项卡。在窗口的下半部分,您可以看到网站例外列表。除此之外,还有一个名为编辑网站列表... 的按钮。单击该按钮。您将看到可以运行其小程序的站点列表。点击添加以添加新位置。在新字段中,键入file:///
并按Enter键。它会向你显示警告;点击继续。现在单击OK并退出Configure Java程序。
接下来,重新启动浏览器。加载小程序时,您会看到一个对话框,您必须选择运行选项。您现在应该可以在浏览器上运行applet了。
请注意谷歌浏览器不再支持小程序。我所知道的支持applet的唯一浏览器是 Firefox 和 Internet Explorer 。我测试了它,它适用于这两个浏览器。
看看它是否解决了你的问题。