小程序不显示图像 [英] Applet not showing image

查看:34
本文介绍了小程序不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里

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

and I downloaded its source code and ran but the image is not showing.

这是结果

我期待结果是这样的与教程中的结果相同.

I was expecting that the result would be like this same as the result in the tutorial.

代码如下:启动类.java

Here is the code: 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
    }
}

机器人.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 中的文件结构

and here is my file structure in intelij

代码有什么问题??我尝试了../data/character.png"和../src/data/character.png",但没有用.

Whats wrong with the code?? I tride the "../data/character.png" and "../src/data/character.png" but it didnt work.

推荐答案

  • applet.html 加载小程序的页面.
  • data(目录)
    • Character.png
      • applet.html the page loading the applet.
      • data (directory)
        • Character.png
        • 如果这是服务器的结构,图像将通过:

          If that is the structure of the server, the image will be available by:

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

          我在上面强调了服务器,因为这显然不是您的 IDE 的设置方式.

          I stressed server above since that is apparently not how your IDE is set up.

          你能详细说明一下吗?

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

          You opened the src/kilobolt path to show the locations of the source files, but it you expand the bin folder and trace down, you'll probably find the .class files in the bin/kilobolt directory.

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

          An IDE typically won't use an HTML file for loading the applet, but if IntelliJ did, it would probably put it in the bin directory so it has direct access to the class files. The path from there to the image would be ../data/character.png, but instead of using that path, suggest you get the IDE to copy the image into the bin.

          在这个阶段,它已经成为关于 IntelliJ 的,所以你有任何进一步的问题,将需要关于 IDE 和它使用的运行时类路径.

          At this stage it has become about IntelliJ so any further questions you have, will need to be about the IDE and the run-time class-path it uses.

          这篇关于小程序不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆