简体   繁体   English

Java摆动图像路径

[英]java swing image path

The class is extracted from a game project. 该类是从游戏项目中提取的。 Everything works as it should, I can play the game, start a new game, etc. Only the .gif pics are not displayed. 一切正常,我可以玩游戏,开始新游戏,等等。仅显示.gif图片。 Where in my project do I have to store the .gif pics, so that the image path works? 我必须在项目的哪里存储.gif图片,以便图像路径有效? I hope the code is understandable. 我希望代码是可以理解的。

class Field extends JPanel {
//fields in x direction
static final int max_x = 10;
//fields in y direction
static final int max_y = 10;
//array for field
JLabel[][] label = new JLabel [max_x][max_y];
//home-directory
String img_source = MainWindow.getDirectory();
//separator
String separator = MainWindow.getSeparator();
//stringarray for different viewing direction
String [] viewing_direction = {"player.gif", "player1.gif", "player2.gif", "player3.gif", "player4.gif", null, "player6.gif", "player7.gif", "player8.gif", "player9.gif", "loss.gif", "win.gif"};
//integer for last viewing direction
int viewing_direction_Player = 0;


Field() {
    super();
    this.setLayout(new GridLayout(max_x, max_y));
    //set theme "game"
    img_source = img_source + "image" + separator + "Game";
    addJLabels();
}

//creates field with alternating background
private void addJLabels() {
    for (int i = 0; i < max_y; i++) {
        for (int k = 0; k < max_x; k++) {
            label[i][k] = new JLabel();
            if((i+k) % 2 == 0) {    //modulo operation for alternating background
                label[i][k].setBackground(Color.BLACK);
            }
            else {
                label[i][k].setBackground(Color.WHITE);
            }
            label[i][k].setPreferredSize(new Dimension(50, 50));
            label[i][k].setOpaque(true);
            this.add(label[i][k]);
        }
    }
}



//method to get last viewing direction
public void setLastDirection(int lastDirection) {
    viewing_direction_Player = lastDirection;
}


//method that draws all pawns
public void drawAllPawns(AbstractPawn[] pawns) {
    for(int i = 0; i < (int) pawns.length; i++) {
        if ((pawns[i].getX() < 0) || (pawns[i].getY() < 0)) {

        } else if (pawns[i] instanceof Player) {
            label[pawns[i].getY()][pawns[i].getX()].setIcon(new ImageIcon(img_source + separator + viewing_direction[viewing_direction_Player]));
        } else if(pawns[i] instanceof Opponent) {
            label[pawns[i].getY()][pawns[i].getX()].setIcon(new ImageIcon(img_source + separator + "opponent.gif"));
        } else if(pawns[i] instanceof Vortex) {
            label[pawns[i].getY()][pawns[i].getX()].setIcon(new ImageIcon(img_source + separator + "vortex.gif"));
        } else if(pawns[i] instanceof Obstacle) {
            label[pawns[i].getY()][pawns[i].getX()].setIcon(new ImageIcon(img_source + separator + "obstacle.gif"));
        } else if(pawns[i] instanceof Destruction) {
            label[pawns[i].getY()][pawns[i].getX()].setIcon(new ImageIcon(img_source + separator + "destruction.gif"));
        } else if(pawns[i] instanceof Ammo) {
            label[pawns[i].getY()][pawns[i].getX()].setIcon(new ImageIcon(img_source + separator + "ammo.gif"));
        }
    }
}

Here is the getDirectory from my mainwindow class 这是我的mainwindow类中的getDirectory

public static String getDirectory() {
    String home = System.getProperty("user.dir");
    return (home + getSeparator());
}

Simple way to use image 使用图像的简单方法

First drag and drop image in src folder 首先将图像拖放到src文件夹中

than type this code 比键入此代码

ImageIcon ic = new ImageIcon(getResource().getClass("Example.jpg")); // image name instead of Example.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM