链接Java程序中的图像

时间:2014-11-09 23:05:24

标签: java image swing chess

快速问题我有一个名为Board.java的java文件,它创建并放置棋子,但我有一个问题,目前它只制作棋盘本身但不分配棋子

我认为我没有链接png的正确

以下是我的设置的屏幕截图我使用Netbeans构建游戏我已截屏它,这样您就可以看到图像的存储位置以及我到目前为止编写的程序

此外,在你问我运行程序时我没有收到错误之前,它只会弹出一个没有碎片的空棋盘

enter image description here

此处还有Java文件

Board.java

package chessgame;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class Board extends JFrame  {

    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;
    int startX;
    int startY;
    int initialX;
    int initialY;
    JPanel panels;
    JLabel pieces;

    public  Board(){
        Dimension boardSize = new Dimension(700, 700);

        //  This is a Layered Pane for this application
        layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(boardSize);

        //Add a chess board to the Layered Pane
        chessBoard = new JPanel();
        layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
        chessBoard.setLayout( new GridLayout(8, 8) );
        chessBoard.setPreferredSize( boardSize );
        chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        for (int i = 0; i < 64; i++) {
            JPanel square = new JPanel( new BorderLayout() );
            chessBoard.add( square );

            int row = (i / 8) % 2;
            if (row == 0)
                square.setBackground( i % 2 == 0 ? Color.white : Color.gray );
            else
                square.setBackground( i % 2 == 0 ? Color.gray : Color.white );
        }
        // Setting up the Initial Chess board.
        for(int i=8;i < 16; i++){
            pieces = new JLabel( new ImageIcon("WhitePawn.png") );
            panels = (JPanel)chessBoard.getComponent(i);
            panels.add(pieces);
        }
        pieces = new JLabel( new ImageIcon("WhiteRook.png") );
        panels = (JPanel)chessBoard.getComponent(0);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKnight.png") );
        panels = (JPanel)chessBoard.getComponent(1);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKnight.png") );
        panels = (JPanel)chessBoard.getComponent(6);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteBishup.png") );
        panels = (JPanel)chessBoard.getComponent(2);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteBishup.png") );
        panels = (JPanel)chessBoard.getComponent(5);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKing.png") );
        panels = (JPanel)chessBoard.getComponent(3);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteQueen.png") );
        panels = (JPanel)chessBoard.getComponent(4);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteRook.png") );
        panels = (JPanel)chessBoard.getComponent(7);
        panels.add(pieces);
        for(int i=48;i < 56; i++){
            pieces = new JLabel( new ImageIcon("BlackPawn.png") );
            panels = (JPanel)chessBoard.getComponent(i);
            panels.add(pieces);
        }
        pieces = new JLabel( new ImageIcon("PieceImages/BlackRook.png") );
        panels = (JPanel)chessBoard.getComponent(56);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKnight.png") );
        panels = (JPanel)chessBoard.getComponent(57);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKnight.png") );
        panels = (JPanel)chessBoard.getComponent(62);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackBishup.png") );
        panels = (JPanel)chessBoard.getComponent(58);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackBishup.png") );
        panels = (JPanel)chessBoard.getComponent(61);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKing.png") );
        panels = (JPanel)chessBoard.getComponent(59);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackQueen.png") );
        panels = (JPanel)chessBoard.getComponent(60);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackRook.png") );
        panels = (JPanel)chessBoard.getComponent(63);
        panels.add(pieces);
    }
}

感谢您阅读任何帮助

3 个答案:

答案 0 :(得分:5)

是的,您错误地添加了片段图像。

    new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhitePawn.png"));

您应该为您必须添加的每个部分使用该格式。它的工作原理是因为当您将程序打包到JAR中时,它必须能够从INSAR中读取JAR。这允许我们从指示的包中获取类资源(“chessgame.PieceImages”)。

您可能还想考虑使用NetBeans Swing Designer,这可能是一种更简单的方式来实现您的国际象棋程序。

答案 1 :(得分:1)

ImageIcon(String)假设您加载的图像包含在磁盘上的平面文件中,根据屏幕截图,您的图像是应用程序上下文中的嵌入资源(它们会被放入生成的Jar中)你的代码)。

在这种情况下,您需要使用Class#getResource加载它们,例如

new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteRook.png"));

我还鼓励您使用ImageIO ImageIcon,因为ImageIO如果在加载图片时出现问题会抛出异常(就像它可以&#39;}找到或读取)并将阻止,直到图像完全实现/加载。

有关详细信息,请参阅Reading/Loading an Image

答案 2 :(得分:-1)

您使用的路径应该相对于您的根源目录:

pieces = new JLabel( new ImageIcon("chessgame/PieceImages/BlackRook.png") );

您可以使用文件资源管理器(不是eclipse,它会将其隐藏)浏览到&#34; bin&#34;文件夹,以了解如何在此处生成文件。