Eclipse ADT Applet空白屏幕

时间:2014-03-09 04:24:56

标签: java eclipse applet awt

我正在尝试在我的applet上显示一些图像并让它们四处移动但是每当我通过applet运行我的项目时,我得到的都是一个空白/黑屏。我可以改变背景的颜色,但是我看不到任何图像。此外,当我尝试启动applet时,applet未初始化。这是代码:

package test;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;

public class MCTaRE extends Applet implements Runnable, KeyListener {

    private Character character;
    private Image image, robot;
    private Graphics graphics;
    private URL base;

    @Override
    public void init() {
        setSize(800, 480);
        setBackground(Color.BLACK);
        setFocusable(true);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("Test");

        robot = getImage(base, "data/character.png");
    }

    @Override
    public void start() {
        Thread thread = new Thread(this);
        thread.start();

        character = new Character();
    }

    @Override
    public void stop() {

    }

    @Override
    public void run() {
        while (true) {
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Graphics g) {
        if (image == null) {
            image = createImage(this.getWidth(), this.getHeight());
            graphics = image.getGraphics();
        }

        // graphics.setColor(getBackground());
        graphics.fillRect(0, 0, getWidth(), getHeight());
        // graphics.setColor(getForeground());
        paint(graphics);

        g.drawImage(image, 0, 0, this);

    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(robot, character.getInitX() - 61,
                character.getInitY() - 63, this);
    }

    public void KeyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_SPACE:
            System.out.println("Space");
            break;
        }
    }

    public void KeyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {

    }

}

package test;

public class Character {
    private int initX = 100;
    private int initY;

    private int speedX = 0;

    public void update(){
        //if (key listener is at the top){
                moveLeft();
    //}
                //else {
                    stop();
                //}

        initY = 200;
    }

    public int getInitX() {
        return initX;
    }

    public int getInitY() {
        return initY;
    }

    public int getSpeedX() {
        return speedX;
    }

    public void setInitX(int initX) {
        this.initX = initX;
    }

    public void setInitY(int initY) {
        this.initY = initY;
    }

    public void setSpeedX(int speedX) {
        this.speedX = speedX;
    }

    public void moveLeft(){
        speedX = -5;
    }

    public void stop(){
        speedX = 0;
    }
}

我非常感谢任何帮助,我需要做这项工作。我可以尝试使用AVD,但我认为这会使事情变得更复杂。并且JApplet不是一种选择。

1 个答案:

答案 0 :(得分:1)

  

“但我看不到任何图片。”

您有几个加载图像的选项。

我喜欢在从类路径加载时使用getClass().getResource("/path/to/image"),这是从嵌入式资源中使用的。

try {
    robot = ImageIO.read(getClass().getResource("/data/character.png"));
} catch (IOException ex) {
    ex. printStackTrace();
}

你可以使用ImageIcon.getImage做同样的事情,你可以避开try/catch但我更喜欢前者,所以如果路径不正确你可以得到异常

robot = new ImageIcon(getClass().getResource("/data/character.png")).getImage();

对于Applet,您可以使用getCodeBase() here

try {
    URL url = new URL(getCodeBase(), "data/character.png");
    robot = ImageIO.read(url);
} catch (IOException e) {
    e.printStackTrace();
}

注意:所有三个选项都适用于您的代码(仅测试全部),给定您的文件结构如下,data位于{ {1}}

src

旁注

  • 您没有在ProjectRoot src data character.png test MCTaRE.java 方法中调用super.paint,这会留下油漆工件。所以称之为

    paint
  • 为什么要首先编写Applet代码。如果它必须是Applet,为什么选择AWT @Override public void paint(Graphics g) { super.paint(g); 而不是Swing Applet。如果这是课堂作业,请让您的教授阅读Why CS teachers should stop teaching Java applets

  • 如果你想抛弃零(AWT)并与英雄(Swing)相处,请在Creating GUI with Swing

  • 阅读更多内容