Java applet和应用程序之间的区别

时间:2012-07-05 05:34:29

标签: java html browser applet web

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

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;
    }
}

我想知道是否有任何事情突然出现错误或不相容。如果有人可以专门帮助我使用这些代码,或者至少让我知道如何开始使这个代码与Web applet兼容,那将非常感激。我已经看过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 不知道这是否有帮助。

2 个答案:

答案 0 :(得分:0)

您的applet没有 init -method,因此无法启动。你应该阅读有关applet的内容,因为我们不能在这里给出一个教程。 Hiere is the official Oracle page to applets

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

生成较小的输出可能很方便,但是测试类文件很好。

答案 1 :(得分:0)

问题解决了:
我从头创建了一个新项目,并逐字跟踪this教程,发现java applet中不支持方法Toolkit.getDefaultToolkit()。getImage(),但getImage()方法是。我把它们换了出去,然后将我的图像加载代码移到了init()方法中,发现我的程序在eclipse和chrome中运行良好。感谢您的所有建议和批评。我发现我发布的代码在某些方面还不成熟,我会确保在下次发布问题时记住你的意见。