0

我应该运行我正在尝试创建的游戏的 jar 文件有问题。这基本上是加载图像的一个主要问题,因此它可以在 Jar 以及 Netbeans 的环境中工作。

我目前正在使用

imgIcon = new ImageIcon(Core.classLoader.getResource("Images/Boss1.png"));
img = imgIcon.getImage();

这很好用 4 我所有的敌人水平背景和射击我的各种物体。但是如果我尝试在我的播放器类上使用它,JAR文件就会变成unexecutablem,即使它在 NetBeans 中仍然可以完美运行。我是否在播放器或其他地方搞砸了,因为图像似乎加载了这个特定的代码,并且只有当它也在播放器中时,jar 才能使用。

在这里可以看到播放器类(我认为这一定是我一遍又一遍忽略的问题):

package Entity;

import Shots.PlayerShot;
import bullethellreloaded.Core;
import bullethellreloaded.Screen;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;
import javax.swing.ImageIcon;



public class Player extends Entity{
public int x,y; // public used to make an easy mouse controll;
int xDirection, yDirection;
int health;
private Image img;
private ImageIcon imgIcon;
private Rectangle hitbox;
public static ArrayList shots;

public Player(){
    x = 250;
    y = 400;
    shots = new ArrayList();
    health = 5;
    imgIcon = new ImageIcon(Core.classLoader.getResource("Images/spaceship           (Redshrike,Stephen Challener).png"));
    img = imgIcon.getImage();
}



@Override
public void move(){

    x += xDirection;
    y += yDirection;
    // Wallcollision detection, needs to be ajusted for the size of the object.
    if(x <= 0)
        x = 0;
    if(x >= Screen.getScreenWidth())
        x = Screen.getScreenWidth();
    if(y <= 0)
        y = 0;
    if(y >= Screen.getScreenHeight())
        y = Screen.getScreenHeight();

}

public void setXDirection(int xdir){
    xDirection = xdir;
}
public void setYDirection(int ydir){
    yDirection = ydir;
}


@Override
public Image getImage(){
    return img;
}

@Override
public ImageIcon getImageIcon(){
    return imgIcon;
}

@Override
public int getX(){
    return x;
}

@Override
public int getY(){
    return y;
}

@Override
public Rectangle getHitbox(){
    return hitbox;
}

public static ArrayList getShots(){
    return shots;
}

public void fire(){
    PlayerShot shot = new PlayerShot(getX(), getY()-getImageIcon().getIconHeight()/2, 0, 1, 1,Core.classLoader.getResource("Images/PlayerShot.png"));
    shots.add(shot);
}



@Override
public void removeHitbox(){
    hitbox = null;
}

@Override
public Rectangle setHitbox(){
    int width = getImageIcon().getIconWidth();
    int height = getImageIcon().getIconHeight();
    hitbox = new Rectangle(getX()+width/2-5, getY()+height/2-5, 1, 1);
    return hitbox;
}

public void takeDamage(int dmg){
    health -= dmg;
}
public int getHealth(){
    return health;
}

在我的屏幕类中绘制,它是paintComponent中由paint调用的扩展JFrame(双缓冲和东西^^)

在以下代码段中:

    }else{
        g.drawImage(testlevel.getImage(),0,0,this);
        g.drawImage(player.getImage(),player.getX(),player.getY(),this);

        // painting the Score
        g.setColor(Color.white);
        g.setFont(new Font("Arial",Font.BOLD,14));
        g.drawString("Score: "+ Score.getScore(), getScreenWidth()-100, 50);
        // painting out the content of the ArrayLists shots, enemies, enemyShots
        try{
            for (int i = 0; i < Player.shots.size(); i++){
                PlayerShot s = (PlayerShot) Player.shots.get(i);      
                if (s.getDeleted() != true){
                    g.drawImage(s.getImage(), s.getX(), s.getY(), this);
                }
            }
            for (int i = 0; i < Enemy.enemies.size(); i++){
                Enemy e = (Enemy) Enemy.enemies.get(i);      
                if (e.getDestroied() != true){
                    g.drawImage(e.getImage(), e.getX(), e.getY(), this);
                }
            }
            for (int i = 0; i < Enemy.enemyShots.size(); i++){
                EnemyShot es = (EnemyShot) Enemy.enemyShots.get(i);      
                if (es.getDeleted() != true){
                    g.drawImage(es.getImage(), es.getX(), es.getY(), this);
                }
            }
        }catch(Exception e){}
    }
    repaint();
}

我希望这是足够的信息,如果不是这样,请告诉我。

编辑:

问题更像是有另一种方法可以与我的程序一起使用,或者我搞砸了什么来得到这个甚至没有启动的无用 JAR。

改变这个(这个路径只在netbeans中有效,jar在这个路径中找不到它)

    imgIcon = new ImageIcon("src/Images/spaceship           (Redshrike,Stephen Challener).png");

对此:

    imgIcon = new ImageIcon(Core.classLoader.getResource("Images/spaceship           (Redshrike,Stephen Challener).png"));

使 JAR 无法运行(甚至不是后台进程),而它在旧路径下工作得很好,除了玩家没有图像这一事实。

4

2 回答 2

0

您应该将资源打包到最终的 Jar 中。

对于 Eclipse,有一个名为 FatJar 的插件可以做到这一点(在较新的 Eclipse 版本中,它嵌入为选项 Export>Runnable Jar File)。

通常编译器不会将资源打包到包中。

于 2013-08-26T22:20:22.227 回答
0

我自己发现了,这很荒谬。图像的名称似乎是长 4 类加载器,如果它在一个 jar 中使用缩短图像的名称让我们运行程序 ^^

AnyWays 感谢那些试图帮助我的人和 JAR 文件,它提醒我研究简单的东西 1。

于 2013-08-27T18:29:38.707 回答