小程序没有显示图像

时间:2013-04-29 03:33:30

标签: java applet

我正在关注本教程here

我下载了源代码并运行但图像没有显示。

这是结果 enter image description here

我期待结果会是这样的 enter image description here 与教程中的结果相同。

这是代码: StartingClass.java

package kiloboltgame;

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 StartingClass extends Applet implements Runnable, KeyListener {

    private Robot robot;
    private Image image, character;
    private Graphics second;
    private URL base;

    @Override
    public void init() {

        setSize(800, 480);
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("Q-Bot Alpha");
        try {
            base = getDocumentBase();

        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.toString());
        }

        // Image Setups
        character = getImage(base, "data/character.png");
        System.out.println(" "+base);

    }

    @Override
    public void start() {
        robot = new Robot();

        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void run() {
        while (true) {
            robot.update();
            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());
            second = image.getGraphics();
        }

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

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

    }

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

    }

    @Override
    public void keyPressed(KeyEvent e) {

        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            System.out.println("Move up");
            break;

        case KeyEvent.VK_DOWN:
            System.out.println("Move down");
            break;

        case KeyEvent.VK_LEFT:
            robot.moveLeft();
            break;

        case KeyEvent.VK_RIGHT:
            robot.moveRight();
            break;

        case KeyEvent.VK_SPACE:
            System.out.println("Jump");
            robot.jump();
            break;

        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            System.out.println("Stop moving up");
            break;

        case KeyEvent.VK_DOWN:
            System.out.println("Stop moving down");
            break;

        case KeyEvent.VK_LEFT:
            robot.stop();
            break;

        case KeyEvent.VK_RIGHT:
            robot.stop();
            break;

        case KeyEvent.VK_SPACE:
            System.out.println("Stop jumping");
            break;
        }

    }

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

Robot.java

package kiloboltgame;

import java.awt.Graphics;

public class Robot {

    private int centerX = 100;
    private int centerY = 382;
    private boolean jumped = false;

    private int speedX = 0;
    private int speedY = 1;


    public void update() {

        // Moves Character or Scrolls Background accordingly.
        if (speedX < 0) {
            centerX += speedX;
        } else if (speedX == 0) {
            //System.out.println("Do not scroll the background.");

        } else {
            if (centerX <= 150) {
                centerX += speedX;
            } else {
                //System.out.println("Scroll Background Here");
            }
        }

        // Updates Y Position
        centerY += speedY;
        if (centerY + speedY >= 382) {
            centerY = 382;
        }

        // Handles Jumping
        if (jumped == true) {
            speedY += 1;

            if (centerY + speedY >= 382) {
                centerY = 382;
                speedY = 0;
                jumped = false;
            }

        }

        // Prevents going beyond X coordinate of 0
        if (centerX + speedX <= 60) {
            centerX = 61;
        }
    }

    public void moveRight() {
        speedX = 6;
    }

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

    public void stop() {
        speedX = 0;
    }

    public void jump() {
        if (jumped == false) {
            speedY = -15;
            jumped = true;
        }

    }

    public int getCenterX() {
        return centerX;
    }

    public int getCenterY() {
        return centerY;
    }

    public boolean isJumped() {
        return jumped;
    }

    public int getSpeedX() {
        return speedX;
    }

    public int getSpeedY() {
        return speedY;
    }

    public void setCenterX(int centerX) {
        this.centerX = centerX;
    }

    public void setCenterY(int centerY) {
        this.centerY = centerY;
    }

    public void setJumped(boolean jumped) {
        this.jumped = jumped;
    }

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

    public void setSpeedY(int speedY) {
        this.speedY = speedY;
    }

}

这是我在intelij中的文件结构

enter image description here

代码有什么问题?我试着“../data/character.png”和“../src/data/character.png”,但它没有用。

4 个答案:

答案 0 :(得分:1)

  • applet.html加载小程序的页面。
  • data(目录)
    • Character.png

如果这是服务器的结构,则图片将可通过以下方式获取:

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

我强调上面的服务器,因为显然不是你的IDE设置方式。


  

你能详细说明吗?

您打开了src/kilobolt路径以显示源文件的位置,但是您展开bin文件夹并向下追踪,您可能会在其中找到.class个文件bin/kilobolt目录。

IDE通常不会使用HTML文件来加载applet,但如果IntelliJ这样做,它可能会将其放在bin目录中,以便它可以直接访问类文件。 从那里到图像的路径是../data/character.png,但是建议您使用IDE将图像复制到bin中,而不是使用该路径。

在这个阶段,它已成为关于IntelliJ的问题,因此您需要了解IDE以及它使用的运行时类路径。

答案 1 :(得分:0)

这似乎是一个图像问题。计算机无法找到图像的位置,或者正在小程序下绘制图像。

如果你使用的是linux / Mac / unix机器,大部分时间,我都必须从根文件夹开始,例如/Users/.....到源文件夹,或者使用的目录是更接近,只需在它前面使用'/'。例如:

你正在使用一个名为src的目录,里面有一个'img'文件夹。要获得'img'内容,您有两种选择:

// ...... SRC / IMG

/ SRC / IMG /....

希望有所帮助

答案 2 :(得分:0)

将数据文件夹复制到bin文件夹中。 清理项目并运行。

它会起作用。

答案 3 :(得分:0)

@Luiggi Mendoza我遇到了同样的问题,并且能够通过右键单击&#39; character.png&#39;来解决问题。然后选择属性,然后从根目录复制图像的位置。就我而言,它是&#34; /Users/macbookpro/NetBeansProjects/Kilobolt/src/data/character.png"和宾果机器人出现在小程序窗口中。

是的,我在3年前就在同一个网站上学习游戏