在Java Applet中运行游戏

时间:2011-11-29 19:15:36

标签: java applet

我在网上找到的不同代码片段的帮助下写了一个小的2d卷轴。原始包作为JFrame应用程序运行,但我试图将其转换为applet。当我在Eclipse中运行程序时,我没有收到任何调试错误只是一个空白的applet查看器...我不认为我从我从不同的applet创建源读取的内容中遗漏了任何东西,但也许它非常简单。 / p>

框架类

package OurGame;

import java.awt.*;
import javax.swing.*;

public class Frame extends JApplet {

public Frame() {
    JPanel frame = new JPanel();
    frame.add(new Board()); 
    // frame.setTitle("2D PLATFORMER");
    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(700,365);
    frame.setVisible(true);
    //frame.setLocationRelativeTo(null);
    //setContentPane(frame);
}
// public static void main(String[] args){
public void init() {
    new Frame();
}
 }

我已经注释掉了只能在Jframe中使用的容器。

Dude class

package OurGame;

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

public class Dude {
int x, dx, y,nx,nx2,left, dy;
Image still,jump,reverse;

ImageIcon s = new ImageIcon("redirect.png");
ImageIcon j= new ImageIcon("redirect.png");
ImageIcon l = new ImageIcon("redirect.png");

public Dude() {
    x = 75;
    left = 150;
    nx = 0;
    nx2= 685;
    y = 172;
    still = s.getImage();


}

public void move() {
    if (dx != -1){
        if (left + dx <= 150)
            left+=dx;
        else{
    x = x + dx;

    nx2= nx2+dx;
        nx = nx + dx;
}}
    else
{
    if (left+dx >0)
    left = left + dx;
}
    }

public int getX() {
    return x;
}

public int getnX() {
    return nx;
}

public int getnX2() {
    return nx2;
}

public int getdx() {
    return dx;
}

public Image getImage() {
    return still;
}

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_LEFT)
    {       dx = -1;
    still = l.getImage();       }

    if (key == KeyEvent.VK_RIGHT)
        {dx = 1;
    still = s.getImage();   
        }

    if (key == KeyEvent.VK_UP)
        {dy = 1;
        still = j.getImage();
        }           }

public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT)
        dx = 0;

    if (key == KeyEvent.VK_RIGHT)
        dx = 0;

    if (key == KeyEvent.VK_UP)
        {dy = 0;
        still = s.getImage();}
        }
}

董事会成员

package OurGame;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel implements ActionListener, Runnable {
Dude p;
public Image img;
Timer time;
int v = 172;
Thread animator;

boolean a = false;
boolean done2 = false;

public Board() {
    p = new Dude();
    addKeyListener(new AL());
    setFocusable(true);
    ImageIcon i = new ImageIcon("redirect.jpg");
    img = i.getImage();
    time = new Timer(5, this);
    time.start();
}

public void actionPerformed(ActionEvent e) {
    p.move();
    repaint();
}

public void paint(Graphics g) {
    if (p.dy == 1 && done2 == false) {
        done2 = true;
        animator = new Thread(this);
        animator.start();
    }

    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    if ((p.getX() - 590) % 2400 == 0)// p.getX() == 590 || p.getX() == 2990)
        p.nx = 0;
    if ((p.getX() - 1790) % 2400 == 0)// p.getX() == 1790 || p.getX() == 4190)
        p.nx2 = 0;

    g2d.drawImage(img, 685 - p.getnX2(), 0, null);
    if (p.getX() > 590) {
        g2d.drawImage(img, 685 - p.getnX(), 0, null);
    }
    g2d.drawImage(p.getImage(), p.left, v, null);

    if (p.getdx() == -1) {
        g2d.drawImage(img, 685 - p.getnX2(), 0, null);
        g2d.drawImage(p.getImage(), p.left, v, null);
    }
}

private class AL extends KeyAdapter {
    public void keyReleased(KeyEvent e) {
        p.keyReleased(e);
    }

    public void keyPressed(KeyEvent e) {
        p.keyPressed(e);
    }
}

boolean h = false;
boolean done = false;

public void cycle() {

    if (h == false)
        v--;
    if (v == 125)
        h = true;
    if (h == true && v <= 172) {
        v++;
        if (v == 172) {
            done = true;
        }
    }
}

public void run() {

    long beforeTime, timeDiff, sleep;

    beforeTime = System.currentTimeMillis();

    while (done == false) {

        cycle();

        timeDiff = System.currentTimeMillis() - beforeTime;
        sleep = 10 - timeDiff;

        if (sleep < 0)
            sleep = 2;
        try {
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            System.out.println("interrupted");
        }

        beforeTime = System.currentTimeMillis();
    }
    done = false;
    h = false;
    done2 = false;
}

}

经过大量研究后,我有点磕磕绊绊。我想Eclipse可能不会认识到我有多个类文件,但我通过编写一个html页面来显示我的applet运行正常但是完全是空的,从而证明了理论错误。

1 个答案:

答案 0 :(得分:4)

  

为什么applet viewer在运行时甚至会在eclipse中空白..

applet容器中没有添加任何内容。要向applet添加内容,需要覆盖applet init()方法并调用add(new Board());。 (这也可以在构造函数中完成,但在init()方法中构建applet GUI更为常见。)


其他注释

paint(Graphics)

由于Board是不属于顶级容器的Swing类,因此应使用paintComponent(Graphics)方法而不是paint(Graphics)自定义绘制。

命名法

JPanel frame = new JPanel();

哇!选择不当的属性名称。你对JFramepanel个实例ImageIcon s = new ImageIcon("redirect.png"); 的称呼是什么?

申请资源

Applet

这对applet无效,对部署的应用无效。有必要通过URL访问图像。 JPanel frame = new JPanel(); frame.add(new Board()); frame.setSize(700,365); frame.setVisible(true); 类具有加载图像的特定方法。

构造函数的剩余行

Board

不需要构造函数的第一行,可以将下一行中创建的frame.setSize(700,365); 直接添加到applet。这留下了两条未注释掉的行。

frame.setVisible(true);

applet的大小应该由HTML设置。

Timer

添加到可见组件的任何内容本身都会变得可见。因此,这也是多余的。

Swing Timer

因为我在代码中指出了很多错误,我想应该补充说动画似乎是正确完成的 - 使用Swing {{1}}。 :)