3
public class GUI
{
    JFrame frame;
    JPanel squares[][];


    /* Constructor credited to stackoverflow user ranzy
        http://stackoverflow.com/questions/2535417/chess-board-in-java */
    public GUI()
    {
        frame = new JFrame("Chess");
        squares = new JPanel[8][8];
        frame.setSize(500, 500);
        frame.setLayout(new GridLayout(8, 8));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                squares[i][j] = new JPanel();

                if ((i + j) % 2 == 0) {
                    squares[i][j].setBackground(Color.white);
                } else {
                    squares[i][j].setBackground(Color.orange);
                }
                frame.add(squares[i][j]);
            }
        }

        ImageIcon pawnW = new ImageIcon(getClass().getResource("/images/pawnW.png"));
        ImageIcon knightW = new ImageIcon("images/knightW.png");
        ImageIcon bishopW = new ImageIcon("/images/bishopW.png");
        ImageIcon rookW = new ImageIcon("/images/rookW.png");
        ImageIcon queenW = new ImageIcon("/images/queenW.png");
        ImageIcon kingW = new ImageIcon("/images/kingW.png");

        ImageIcon pawnB = new ImageIcon("/images/pawnB.png");
        ImageIcon knightB = new ImageIcon("/images/knightB.png");
        ImageIcon bishopB = new ImageIcon("/images/bishopB.png");
        ImageIcon rookB = new ImageIcon("/images/rookB.png");
        ImageIcon queenB = new ImageIcon("/images/queenB.png");
        ImageIcon kingB = new ImageIcon("/images/kingB.png");

        squares[0][0].add(new JLabel(rookW));
        squares[1][0].add(new JLabel(knightW));
        squares[2][0].add(new JLabel(bishopW));
        squares[3][0].add(new JLabel(queenW));
        squares[4][0].add(new JLabel(kingW));
        squares[5][0].add(new JLabel(bishopW));
        squares[6][0].add(new JLabel(knightW));
        squares[7][0].add(new JLabel(rookW));

        squares[0][7].add(new JLabel(rookB));
        squares[1][7].add(new JLabel(knightB));
        squares[2][7].add(new JLabel(bishopB));
        squares[3][7].add(new JLabel(queenB));
        squares[4][7].add(new JLabel(kingB));
        squares[5][7].add(new JLabel(bishopB));
        squares[6][7].add(new JLabel(knightB));
        squares[7][7].add(new JLabel(rookB));

        for (int i = 0; i < 8; i++)
        {
            squares[i][1].add(new JLabel (pawnW));
            squares[i][6].add(new JLabel (pawnB));
        }

    }

}

I'm not able to get the icons to display. I've looked through multiple tutorials on this as well as looked at others' code.

enter image description here

I tried three different ways:

ImageIcon pawnW = new ImageIcon(getClass().getResource("/images/pawnW.png"));
ImageIcon knightW = new ImageIcon("images/knightW.png");
ImageIcon bishopW = new ImageIcon("/images/bishopW.png");
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Collin
  • 1,777
  • 4
  • 26
  • 42
  • How are you compiling and building your code? With intellij? – Sotirios Delimanolis Feb 12 '14 at 06:11
  • Have you tried with the full path? e.g. c:/users/collin/ ... – ahmedalkaff Feb 12 '14 at 06:13
  • 2
    @ahmedalkaff The `Class#getResource(..)` method expects a path relative to the classpath. – Sotirios Delimanolis Feb 12 '14 at 06:14
  • 1
    The first should work, unless the `images` was never built into the bin. Check the `bin` or its equivalent in Intellij (I have no clue what that is) and see if the `images` is there. – Paul Samsotha Feb 12 '14 at 06:15
  • @SotiriosDelimanolis I meant as a test he could try it out and if it worked then conclusions can be made about the cause. – ahmedalkaff Feb 12 '14 at 06:17
  • @ahmedalkaff There is no _point_ testing it. Read the javadoc. It will not work. – Sotirios Delimanolis Feb 12 '14 at 06:18
  • Do you have any exception? `getClass().getResource("/images/pawnW.png")` must work for getting resource from classpath. – alex2410 Feb 12 '14 at 06:19
  • 1
    It's likely that intellij does not include the `src` directory within it's class path for running application. Have you compiled and built the project? Do you have a Jar file? If you do, unzip and see what's included... – MadProgrammer Feb 12 '14 at 06:21
  • So.. it looks like the `/chess/src` folder contains only the .java files. If I navigate to the `out` folder, it contains the class files. I stuck the images folder in both the folder containing the class files as well as it's parent directory. No success. I don't have a jar file/not sure where it would be. – Collin Feb 12 '14 at 06:25
  • @SotiriosDelimanolis Then I must be doing something magic because it works for me. – ahmedalkaff Feb 12 '14 at 06:31
  • @ahmedalkaff You're probably using something other than `Class#getResource(..)`. – Sotirios Delimanolis Feb 12 '14 at 06:32
  • @SotiriosDelimanolis Here is an example: ImageIcon knightW = new ImageIcon("c:/images/knightW.png"); this is probably not the best way but I meant as a test. – ahmedalkaff Feb 12 '14 at 06:42
  • @ahmedalkaff I feel like I'm repeating myself. `The Class#getResource(..) method expects a path relative to the classpath.` You aren't using that method, so your test is irrelevant to that comment. – Sotirios Delimanolis Feb 12 '14 at 06:44
  • @SotiriosDelimanolis please check the question, In the last paragraph stated other ways were tried. What I suggested was an additional one. – ahmedalkaff Feb 12 '14 at 06:47
  • @ahmedalkaff My point is that you should exactly clarify what you are talking about. I made a clarification to your comment. You should do the same. – Sotirios Delimanolis Feb 12 '14 at 06:51

1 Answers1

6

Before anything, as a side note, setVisible should be the last thing you do after adding all components

Also check the below

Works fine for me using getClass().getResource()

 String path = "/images/stackoverflow2.png";

 ImageIcon pawnW = new ImageIcon(getClass().getResource(path));

Check all below, then when correct it, build it, then run it.

enter image description here

File Structure after Build. Images should get copied to class path

enter image description here

enter image description here

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GUI
{
    JFrame frame;
    JPanel squares[][];

    public GUI()
    {
        frame = new JFrame("Chess");
        squares = new JPanel[8][8];
        frame.setSize(500, 500);
        frame.setLayout(new GridLayout(8, 8));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                squares[i][j] = new JPanel();

                if ((i + j) % 2 == 0) {
                    squares[i][j].setBackground(Color.white);
                } else {
                    squares[i][j].setBackground(Color.orange);
                }
                frame.add(squares[i][j]);
            }
        }

        String path = "/images/stackoverflow2.png";

        ImageIcon pawnW = new ImageIcon(getClass().getResource(path));
        ImageIcon knightW = new ImageIcon(getClass().getResource(path));
        ImageIcon bishopW = new ImageIcon(getClass().getResource(path));
        ImageIcon rookW = new ImageIcon(getClass().getResource(path));
        ImageIcon queenW = new ImageIcon(getClass().getResource(path));
        ImageIcon kingW = new ImageIcon(getClass().getResource(path));

        ImageIcon pawnB = new ImageIcon(getClass().getResource(path));
        ImageIcon knightB = new ImageIcon(getClass().getResource(path));
        ImageIcon bishopB = new ImageIcon(getClass().getResource(path));
        ImageIcon rookB = new ImageIcon(getClass().getResource(path));
        ImageIcon queenB = new ImageIcon(getClass().getResource(path));
        ImageIcon kingB = new ImageIcon(getClass().getResource(path));

        squares[0][0].add(new JLabel(rookW));
        squares[1][0].add(new JLabel(knightW));
        squares[2][0].add(new JLabel(bishopW));
        squares[3][0].add(new JLabel(queenW));
        squares[4][0].add(new JLabel(kingW));
        squares[5][0].add(new JLabel(bishopW));
        squares[6][0].add(new JLabel(knightW));
        squares[7][0].add(new JLabel(rookW));

        squares[0][7].add(new JLabel(rookB));
        squares[1][7].add(new JLabel(knightB));
        squares[2][7].add(new JLabel(bishopB));
        squares[3][7].add(new JLabel(queenB));
        squares[4][7].add(new JLabel(kingB));
        squares[5][7].add(new JLabel(bishopB));
        squares[6][7].add(new JLabel(knightB));
        squares[7][7].add(new JLabel(rookB));

        for (int i = 0; i < 8; i++)
        {
            squares[i][4].add(new JLabel (pawnW));
            squares[i][6].add(new JLabel (pawnB));
        }

        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new GUI();
            }
        });
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • @Alex2410 I figure I at least show my effort so it wasn't wasted. Maybe the OP missed something. Wasn't expecting any praise (up-votes) for it :D – Paul Samsotha Feb 12 '14 at 06:55
  • it was the `set visible` thing... Could you edit your answer to reflect that? – Collin Feb 12 '14 at 07:02