Java 小程序不显示图像

Java 小程序不显示图像,java,applet,Java,Applet,我正在学习这个教程 我下载了它的源代码并运行了,但是图像没有显示出来 结果如下 我原以为结果会是这样 与教程中的结果相同 代码如下: 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.ev

我正在学习这个教程

我下载了它的源代码并运行了,但是图像没有显示出来

结果如下

我原以为结果会是这样 与教程中的结果相同

代码如下: 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;
    }

}
打包游戏;
导入java.awt.Graphics;
公共类机器人{
专用int centerX=100;
私人int中心Y=382;
私有布尔值=false;
私有整数速度x=0;
私有int=1;
公共无效更新(){
//相应地移动角色或滚动背景。
如果(速度x<0){
centerX+=speedX;
}else if(speedX==0){
//System.out.println(“不要滚动背景”);
}否则{
如果(centerX=382){
centerY=382;
}
//手柄跳跃
如果(跳转==真){
速度+=1;
如果(中心+速度>=382){
centerY=382;
速度=0;
跳跃=假;
}
}
//防止超出0的X坐标

如果(centerX+speedX这似乎是图像问题。计算机无法找到图像的位置,或者图像正在小程序下绘制

如果您使用的是linux/Mac/unix计算机,大多数情况下,我必须从根文件夹(如/Users/…)开始到源文件夹,或者在使用更近的目录时,只需在其前面使用“/”。例如:

您正在使用名为src的目录,其中包含一个“img”文件夹。要访问“img”内容,您有两个选项:

//……src/img

/src/img/

希望这对任何事情都有帮助
  • applet.html
    加载小程序的页面
  • 数据
    (目录)
    • Character.png
  • 如果这是服务器的结构,则图像将通过以下方式提供:

    getImage(base, "data/character.png");
    
    我在上面强调了服务器,因为IDE显然不是这样设置的


    你能详细说明一下吗

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

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


    在这一阶段,它已经变成了关于IntelliJ的问题,因此您还有更多的问题,都需要关于IDE及其使用的运行时类路径。

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


    它会起作用。

    @Luiggi Mendoza我也有同样的问题,我可以通过右键单击“character.png”并选择属性,然后从其根目录复制图像的位置来解决它。在我的例子中,它是“/Users/macbookpro/NetBeansProjects/Kilobolt/src/data/character.png”宾果,机器人出现在小程序窗口中


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

    而不是学习AWT,你为什么不学习Swing?你可以从上的Swing教程开始。我看不出使用AWT和Swing有多大区别。因为我的目标是对游戏开发的工作原理有一个基本的了解^^^有很大的区别。绘画不同的做法是,使用不同的组件。(主题外)我可以知道你的ide是什么吗?ui看起来不错。@Drogba我在使用intellij 12。你能详细说明一下吗?我在eclipse中也尝试过,但问题相同。