-2

我一直在 eclipse 中开发一个 java web applet(或者至少我认为是一个 java web applet)。在我的计算机上成功测试后,我尝试将其嵌入 HTML 文档中。接下来我知道,我被错误淹没了。根据我的研究,我现在知道这些错误意味着我正在使用某种与 Web 小程序不兼容的代码。不幸的是,我不知道什么代码与小程序兼容或不兼容,因此不知道如何解决这个问题。坦率地说,我什至不知道从哪里开始。我的代码非常复杂:

import java.applet.Applet;
import java.awt.*; 
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;


public class DerpAHerp extends Applet implements KeyListener, MouseListener, Runnable {

int speed = 3;
int andelay = 2;
Thread animator;
Point velocity = new Point(0,0);
Point pos = new Point(50,50);
Point ppos = new Point(0,0);
int imgCount = 1;
Image[] person_S = makeAnimation(6,"person_S-","png");
Image[] person_E = makeAnimation(6,"person_E-","png");
Image[] person_W = makeAnimation(6,"person_W-","png");
Image[] person_N = makeAnimation(6,"person_N-","png");
Image personDefault = person_S[0];
Image person = personDefault;
Image offImage;
Graphics offGraphics;
Boolean moving = false;

/**
 * 
 */
private static final long serialVersionUID = 1L;


/**
 * @param args
 */

    public void getConnection(){
        String asdf = "";

        try {

              URL ourURL = new URL("http://dannyflax.antserve.com/d/"); //Coding Forums RSS Feed
              HttpURLConnection huc = (HttpURLConnection)ourURL.openConnection();            
              huc.setRequestMethod("GET");
              huc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; JVM)");                        
              huc.setRequestProperty("Pragma", "no-cache");
              huc.connect();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(huc.getInputStream()));

              String line = null;
              while ((line = reader.readLine()) != null) {
                  // Either do your parsing here, or append it to a StringBuffer for later use

              asdf = asdf.concat(line);

              }
        }
              catch(IOException ioe)
              {
                  ioe.printStackTrace();
               }
              catch(Exception e)
              {
                 System.err.println("General Exception " + e);
                 e.printStackTrace();
               }


        }
    public void paintFrame(Graphics g) {

        g.setColor(getBackground());
        g.fillRect(0, 0, person.getWidth(null),person.getHeight(null));

        Graphics2D g2 = (Graphics2D) g;


        if(velocity.x!=0 || velocity.y!=0){

            if(velocity.x>0){
            //East  
                if(imgCount%andelay == 0){
                    person = person_E[(imgCount/andelay)%person_S.length];
                    }
            }
            else if(velocity.x<0){
            //West  
                if(imgCount%andelay == 0){
                    person = person_W[(imgCount/andelay)%person_S.length];
                    }
            }
            else if(velocity.y>0){
            //South 
                if(imgCount%andelay == 0){
                    person = person_S[(imgCount/andelay)%person_S.length];
                    }
            }
            else if(velocity.y<0){
            //North 
                if(imgCount%andelay == 0){
                    person = person_N[(imgCount/andelay)%person_S.length];
                    }
            }




            imgCount++;
        }
        else{
            person = personDefault;
            imgCount = 0;
        }

        g2.drawImage(person, 0, 0, this);

        g2.finalize();


    }
    public void update(Graphics g) {


        if (offGraphics == null){
            if(person.getWidth(null) > 0 && person.getHeight(null) > 0)
            offImage = createImage(person.getWidth(null),person.getHeight(null));
            else{
                offImage = createImage(1,1);    
            }
            offGraphics = offImage.getGraphics();
            paintFrame(offGraphics);
        }


        Color bg = getBackground();




        paintFrame(offGraphics);


        g.setColor(bg);
        g.fillRect(ppos.x-(person.getWidth(null)/2), ppos.y-(person.getHeight(null)/2), person.getWidth(null),person.getHeight(null));







                g.drawImage(person, pos.x-(person.getWidth(null)/2),     pos.y-(person.getHeight(null)/2), this);

                g.finalize();

    }
     public void paint(Graphics g) {
        update(g);


    }
     public void init() {
         //Execute a job on the event-dispatching thread; creating this applet's GUI.

         addKeyListener( this );
          addMouseListener( this );

     }




@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub


}


@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void keyPressed(KeyEvent arg0) {
    // TODO Auto-generated method stub

      int code = arg0.getKeyCode();


      switch(code){
      case 37:
          //West

          velocity.y = 0;
          velocity.x = -1 * speed;
          personDefault = person_W[0];

          break;
      case 38:
          //North

          velocity.x = 0;
          velocity.y = -1 * speed;
          personDefault = Toolkit.getDefaultToolkit().getImage("person_N-0.png");

          break;
      case 39:
          //East
          velocity.y = 0;
          velocity.x = speed;
          moving = true;
          personDefault = person_E[0];

          break;
      case 40:
          //South
          velocity.x = 0;
          velocity.y = speed;
          personDefault = person_S[0];

          break;




      }
}


@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub
    int code = arg0.getKeyCode();


      switch(code){
      case 37:
          //Left
          velocity.x = 0;


          break;
      case 38:
          //Up
          velocity.y = 0;


          break;
      case 39:
          //Right
          velocity.x = 0;


          break;
      case 40:
          //Down
          velocity.y = 0;


          break;
      }

}


@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

public void start() {
    animator = new Thread(this);
    animator.start();
    }


@Override
public void run() {
    // TODO Auto-generated method stub
    while (Thread.currentThread() == animator) {
        // Display the next frame of animation.
        ppos.x = pos.x;
        ppos.y = pos.y;
        pos.x = pos.x + velocity.x;
        pos.y = pos.y + velocity.y;
        repaint();

        // Delay for a while
        try {
        Thread.sleep(50);
        } catch (InterruptedException e) {
            System.out.println("Failed");
        break;
        }
}

}

public Image[] makeAnimation(int size, String name, String extension){
    Image[] imgAr = new Image[size];
    while(size>0){
        String src = name + size + "." + extension;
        imgAr[size-1] = Toolkit.getDefaultToolkit().getImage(src);
        size--;
    }
    return imgAr;
}


public void stop() {
    animator = null;
    }
}

我想知道是否有任何东西会立即显示为错误或不兼容。如果有人可以专门帮助我处理这段代码,或者至少让我知道如何开始使这段代码与网络小程序兼容,那将不胜感激。我已经查看了 java applet 文档,但它非常令人困惑,尤其是到目前为止我已经编写了多少代码,因为我在清除代码中的特定内容时遇到了很多麻烦。

给大家:

这是html:

<html>
<head>
</head>
<body>
<applet
    alt="Aplikacja klienta"
    code=DerpAHerp.class
    archive='DerpAHerp.jar'
    width=500
    height=500></applet>
</body>
</html>

错误:

当我加载网页时,我收到一条错误消息,如果我点击它,我会得到: http: //puu.sh/FZmu

控制台说

CacheEntry[http://XXXXXXXXXXXXXX.com/DerpAHerp.jar]:updateAvailable=true,lastModified=Thu Jul 05 00:50:39 EDT 2012,length=16290

这是海报的朋友。他去睡觉了。=\

编辑(来自海报):我得到的错误日志如下: http ://puu.sh/G6us 不知道这是否有帮助。

4

2 回答 2

0

您的小程序没有init方法,因此无法启动。您应该阅读有关小程序的信息,因为我们无法在此处提供教程。Hiere 是小程序的官方 Oracle 页面

您的 html 页面引用了一个 jar,但您的代码独立于其他类。你不需要使用罐子。

产生较小的输出可能很方便,但对于测试类文件来说很好。

于 2012-07-06T01:33:06.283 回答
0

问题已解决: 我从头开始创建了一个新项目,并逐字逐句地
遵循本教程,发现 java 小程序不支持 Toolkit.getDefaultToolkit().getImage() 方法,但 getImage() 方法支持。我将它们换掉,然后将我的图像加载代码移至 init() 方法,发现我的程序在 eclipse 和 chrome 中都可以正常工作。感谢您的所有建议和批评。我意识到我发布的代码在某些方面非常不成熟,下次我发布问题时,我会确保牢记您的意见。

于 2012-07-06T13:36:44.820 回答