我遇到了應該運行我嘗試創建的遊戲的jar文件的問題。 這是一個基本的問題與加載圖像,以便它在Jar和Netbeans的環境中工作。加載圖像,以便它可以在JAR文件和NetBeans中運行
我目前使用
imgIcon = new ImageIcon(Core.classLoader.getResource("Images/Boss1.png"));
img = imgIcon.getImage();
這只是正常4所有我的敵人的級別背景和拍攝我的各種對象火。但是如果我嘗試在我的播放器類上使用它,即使認爲它在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擴展的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();
}
我希望這就是足夠的信息,如果這不是CAS:由油漆下面的代碼段(doublebuffering和東西^^)
調用請讓我知道。
編輯:
的問題更像是有可能與我的PROGRAMM或我是怎麼搞砸得到這個沒用的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不可運行(甚至沒有在後臺進行處理),但除了玩家沒有圖像的事實之外,它在舊路徑上運行良好。
你檢查過你的'jar'文件是否包含圖像? – Katona
是的圖像是在項目(src)文件夾內的一個名爲圖像的包中,因此該jar是與內部的圖像一起編譯的,但事實上,tit崩潰更是一個問題。因爲我認爲它的工作代碼似乎是正確的,但它導致了一個不可用的罐子,因爲我說 – Snake
你實際上解壓縮jar文件並檢查? – assylias