2016-07-19 6 views
0

나는 얼마 동안이 사이트를 탐색 해왔다. 그러나 이것은 나의 첫번째 공식 게시물이다.애플릿이 브라우저에서 실행되지만 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> 
+1

어떤 브라우저입니까? 애플릿은 쓸모없는 기술이며 현재의 일부 브라우저 (예 : Chrome)에서 지원되지 않습니다. Javascript는 Java와 아무 관련이 없습니다. –

+0

Chrome, Safari에서 시험해 보았습니다. Safari의 사용자 에이전트를 IE 11으로 변경했는데 아무 것도 작동하지 않습니다. 애플릿 태그가 다른 곳에서는 거의 사용되지 않는다고 들었습니다. 나는 그것을 객체로 정의하려고 시도했는데 그것도 작동하지 않았다. – floyd10325

+0

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/)를 게시하십시오. .. –

답변

0

, 당신은 applet 태그의 codebase 속성을 추가 놓쳤다했습니다. Codebase는 애플릿이 들어있는 디렉토리를 가리 킵니다. 그래서 해당 응용 프로그램이 HTML 페이지과 동일한 디렉토리에 있지 않으면 applet 태그 안에 codebase 속성을 추가해야합니다.

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

BurleighWatchMe.class 디렉토리 E에 존재한다고 가정 : 그것은 아래의 양식을 걸릴 것 프로젝트 \. BurleighWatchMe.class 파일이 HTML 페이지와 같은 디렉토리에 존재하고 당신은 여전히 ​​빈 페이지가 표시되는 경우

그러나, 그것은 아마 발생했을 수있는 시스템의 JRE 때문에 알 수없는 애플릿을 허용하지 않습니다. 로컬 애플릿을 허용하도록 설정을 편집하면됩니다. 그렇게하려면 Start > Configure Java으로 가십시오. 자바 설정 대화 상자의 Security 탭으로 이동하십시오. 창 하단에서 사이트 예외 목록을 볼 수 있습니다. 그 옆에 사이트 목록 편집 버튼이 있습니다 .... 해당 버튼을 클릭하십시오. 애플릿을 실행할 수있는 사이트 목록이 표시됩니다. 을 클릭하여을 추가하여 새 위치를 추가하십시오. 새 필드에 file:///을 입력하고 Enter 키를 누릅니다. 경고가 표시됩니다. 계속을 클릭하십시오. 이제 확인을 클릭하고 Java 구성 프로그램을 종료하십시오.

다음으로 브라우저를 다시 시작하십시오. 옵션을 선택해야하는 애플릿로드시 대화 상자가 표시됩니다. 이제 브라우저에서 애플릿을 실행할 수 있습니다.

Google 크롬은 더 이상 애플릿을 지원하지 않습니다. 지원 애플릿 중 내가 아는 유일한 브라우저는 입니다. FirefoxInternet Explorer입니다. 나는 그것을 테스트했고이 두 브라우저에서 작동했습니다.

문제가 해결되는지 확인하십시오.

+1

매력처럼 작동했습니다. 방금 Java Intro to Intro에서 A와 B의 차이를 만들었습니다. 정말 고마워! – floyd10325

+0

케이스 @ floyd10325의 문제는 정확히 무엇입니까? – progyammer

+1

정확히 JRE가 알 수없는 애플릿을 허용하지 않는다고 생각했던 것입니다. 보안 설정을 편집하고 나면 Firefox에서 정상적으로 작동합니다. – floyd10325