本文介绍了Java小程序无法找到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉,如果这是一个重复的,但我已经读过几十个关于这个问题的帖子,并且我仍然无法解决这个问题。我工作的一个项目中使用的Java IDE的IntelliJ(它实际上是本场比赛的并似乎在这个论坛上配发)。我的问题是,Java小程序无法找到任何资源(图片和文字)。的getImage()方法斜面加载图像以../data/image.png,也不是/data/image.png,也不是普通的数据/ image.png。同样适用于文件阅读器。我也试过getDocumentBase(),而不是得到codeBase的()。目前,我存储在本地的网站(IIS的locahost)的图像,但事实并非解决方案,此外,我不能使用map1.txt这样(我必须使用本地路径)。林索里,如果我忽略任何重要的东西。

I am sorry if this is a repeat but I already read dozens of posts regarding the issue and I still cant solve it. I am working on a project in java with Intellij IDE (it's actually this game http://www.kilobolt.com/unit-2-creating-a-game-i.html and it appeared on this forum allot). My issue is that the java applet cannot locate any resources (images and texts). getImage() method cant load images with "../data/image.png", nor "/data/image.png", nor the regular "data/image.png". same goes for file reader. I also tried getDocumentBase() instead getCodeBase(). Currently I'm storing images on a local website (IIS locahost) but that's not the solution and besides, i cant use map1.txt that way(i have to use local path). Im sory if i omitted anything of importance.

注:Resources.IMAGE是在一个单独的类静态字段与值这样的:的http://localhost/folder/image.png

Note: "Resources.IMAGE" are static fields in a separate class with values as such: "http://localhost/folder/image.png"

code:

package sinisa;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import sinisa.framework.Animation;

public class StartingClass extends Applet implements Runnable, KeyListener {

    //TODO: Create constructor for this, and a calculator for speed
    private static final int X_SCREEN = 800;
    private static final int Y_SCREEN = 480;

    private Robot robot;
    private Hellboy hellb0;
    private Hellboy hellb1;

    private Image image;
    private Image character;
    private Image character2;
    private Image character3;
    private Image characterDown;
    private Image characterJumped;
    private Image characterWalkLeft;
    private Image characterWalkRight;
    private Image background;
    private Image currentSprite;
    private Image hellboy;
    private Image hellboy2;
    private Image hellboy3;
    private Image hellboy4;
    private Image hellboy5;

    public static Image tileDirt;
    public static Image tileGrassTop;
    public static Image tileGrassBot;
    public static Image tileGrassLeft;
    public static Image tileGrassRight;
    public static Image tileOcean;

    private URL base;
    private Graphics second;
    private Animation anim;
    private Animation hanim;
    private Animation wanim;

    private static Background bg1;
    private static Background bg2;

    private ArrayList<Tile> tileArray = new ArrayList<Tile>();

    int i = 0; int j = 0;
    @Override
    public void init() {
        setSize(X_SCREEN, Y_SCREEN);
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("Game - Alpha 0.1");

        try {
            base = getCodeBase();
        } catch (Exception e) {
            e.printStackTrace();
        }

        character = getImage(base, Resources.CHARACTER);
        character2 = getImage(base, Resources.CHARACTER2);
        character3 = getImage(base, Resources.CHARACTER3);
        characterDown = getImage(base, Resources.CHARACTER_DOWN);
        characterJumped = getImage(base, Resources.CHARACTER_JUMPED);
        characterWalkLeft = getImage(base, Resources.CHARACTER_WALK_LEFT);
        characterWalkRight = getImage(base, Resources.CHARACTER_WALK_RIGHT);
        background = getImage(base, Resources.BACKGROUND);
        tileDirt = getImage(base, Resources.TILEDIRT);
        tileOcean = getImage(base, Resources.TILEOCEAN);
        hellboy = getImage(base, Resources.HELLBOY);
        hellboy2 = getImage(base, Resources.HELLBOY2);
        hellboy3 = getImage(base, Resources.HELLBOY3);
        hellboy4 = getImage(base, Resources.HELLBOY4);
        hellboy5 = getImage(base, Resources.HELLBOY5);

        anim = new Animation();
        anim.addFrame(character, 1250);
        anim.addFrame(character2, 50);
        anim.addFrame(character3, 50);
        anim.addFrame(character2, 50);

        hanim = new Animation();
        hanim.addFrame(hellboy, 100);
        hanim.addFrame(hellboy2, 100);
        hanim.addFrame(hellboy3, 100);
        hanim.addFrame(hellboy4, 100);
        hanim.addFrame(hellboy5, 100);
        hanim.addFrame(hellboy4, 100);
        hanim.addFrame(hellboy3, 100);
        hanim.addFrame(hellboy2, 100);

        wanim = new Animation();
        wanim.addFrame(characterWalkLeft, 50);
        wanim.addFrame(character, 50);
        wanim.addFrame(characterWalkRight, 50);


        currentSprite = anim.getImage();
    }


    @Override
    public void start() {
        bg1 = new Background(0, 0);
        bg2 = new Background(2160, 0);

        try {
            loadMap("../data/map1.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }

        hellb0 = new Hellboy(340, 360);
        hellb1 = new Hellboy(700, 360);

        robot = new Robot();

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

    }

    @Override
    public void stop() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void run() {

        while(true) {
            if(robot.isJumped()) {
                currentSprite = characterJumped;
            }
            else if(robot.isMovingLeft() || robot.isMovingRight()) {
                currentSprite = wanim.getImage();
            }
            else if(!robot.isJumped() && !robot.isDucked()) {
                currentSprite = anim.getImage();
            }
            robot.update();
            ArrayList projectiles = robot.getProjectiles();
            for(int i = 0; i < projectiles.size(); i++) {
                Projectile p = (Projectile) projectiles.get(i);
                if(p.isVisible()) {
                    p.update();
                }
                else {
                    projectiles.remove(i);
                }
            }
            bg1.update();
            bg2.update();

            updateTiles();
            hellb0.update();
            hellb1.update();
            animate();
            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(background, bg1.getBgX(), bg1.getBgY(), this);
        g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
        g.drawImage(hanim.getImage(), hellb0.getCenterX()-48, hellb0.getCenterY()-48, this);
        g.drawImage(hanim.getImage(), hellb1.getCenterX()-48, hellb1.getCenterY()-48, this);
        paintTiles(g);
        g.drawImage(currentSprite, robot.getCenterX() - 61, robot.getCenterY() - 63, this);
        ArrayList projectiles = robot.getProjectiles();

        for (int i = 0; i < projectiles.size(); i++) {
            Projectile p = (Projectile) projectiles.get(i);
            g.setColor(Color.YELLOW);
            g.fillRect(p.getX(), p.getY(), 10, 5);
        }
    }

    public void animate() {
        anim.update(10);
        hanim.update(50);
        wanim.update(10);
    }

    private void updateTiles() {

        for (int i = 0; i < tileArray.size(); i++) {
            Tile t = (Tile) tileArray.get(i);
            t.update();
        }
    }

    private void paintTiles(Graphics g) {
        for (int i = 0; i < tileArray.size(); i++) {
            Tile t = (Tile) tileArray.get(i);
            g.drawImage(t.getTileImage(), t.getTileX(), t.getTileY(), this);
        }
    }

    private void loadMap(String filename) throws IOException {
        ArrayList lines = new ArrayList();
        int width = 0;
        int height = 0;

        BufferedReader reader = new BufferedReader(new FileReader(filename));
        while (true) {
            String line = reader.readLine();
            // no more lines to read
            if (line == null) {
                reader.close();
                break;
            }

            if (!line.startsWith("!")) {
                lines.add(line);
                width = Math.max(width, line.length());

            }
        }
        height = lines.size();

        for (int j = 0; j < 12; j++) {
            String line = (String) lines.get(j);
            for (int i = 0; i < width; i++) {
                System.out.println(i + "is i ");

                if (i < line.length()) {
                    char ch = line.charAt(i);
                    Tile t = new Tile(i, j, Character.getNumericValue(ch));
                    tileArray.add(t);
                }

            }
        }

    }

    @Override
    public void keyPressed(KeyEvent e) {

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

            case KeyEvent.VK_DOWN:
                currentSprite = characterDown;
                if (!robot.isJumped()){
                    robot.setDucked(true);
                    robot.setSpeedX(0);
                }
                break;

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

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

            case KeyEvent.VK_SPACE:
                robot.jump();
                break;

            case KeyEvent.VK_CONTROL:
                if(!robot.isDucked()) {
                robot.shoot();
                }
                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:
                currentSprite = anim.getImage();
                robot.setDucked(false);
                break;

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

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

            case KeyEvent.VK_SPACE:
                break;

        }

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    public static Background getBg1() {
        return bg1;
    }

    public static Background getBg2() {
        return bg2;
    }
}

项目文件夹层次:

Project folder hierarchy:

推荐答案

我有同样的问题。我通过改变方法来检索基本URL固定它。

I had the same issue... I fixed it by changing the method to retrieve the base URL.

    try {
        base = StartingClass.class.getResource("/data/background.png");
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Image Setups
    character = getImage(base, "character.png");
    characterDown = getImage(base, "down.png");
    ...

这篇关于Java小程序无法找到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 11:49
查看更多