나는 얼마 동안이 사이트를 탐색 해왔다. 그러나 이것은 나의 첫번째 공식 게시물이다.애플릿이 브라우저에서 실행되지만 IDE에서 실행되지 않는 이유는 무엇입니까?
자바 (애플릿을 처음 사용하는 것)에 상당히 익숙하며 할당 된 애플릿을 브라우저에서 실행하는 데 문제가 있습니다. 이클립스에서는 모든 것이 잘 돌아 가지 만, .html 파일을 열면 빈 공간 만 남을 것이다.
내가 아래에있는 것을 살펴보고 전문 지식을 제공 할 수 있다면 크게 감사하겠습니다. 나는 멍청한 실수를하고 아직 그것을 찾을 수 없었다고 확신합니다. 감사.
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 코드에서
<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>
어떤 브라우저입니까? 애플릿은 쓸모없는 기술이며 현재의 일부 브라우저 (예 : Chrome)에서 지원되지 않습니다. Javascript는 Java와 아무 관련이 없습니다. –
Chrome, Safari에서 시험해 보았습니다. Safari의 사용자 에이전트를 IE 11으로 변경했는데 아무 것도 작동하지 않습니다. 애플릿 태그가 다른 곳에서는 거의 사용되지 않는다고 들었습니다. 나는 그것을 객체로 정의하려고 시도했는데 그것도 작동하지 않았다. – floyd10325
1) 애플릿을 코딩하는 이유는 무엇입니까? 교사가 지정했기 때문에 [CS 교사가 ** Java 애플릿 교육 **을 중단해야하는 이유] (http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should?hl=ko)를 참조하십시오. -stop-teaching-java-applets /)를 사용합니다. 2) [Java Plugin support deprecated] (http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) 및 [Plugin-Free Web로 이동] (https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). 3) 더 빨리 도움을 받으려면 [MCVE] 또는 [짧은, 자기 포함, 올바른 예] (http://www.sscce.org/)를 게시하십시오. .. –