Question

I am trying to display an ImageIcon of an object of class bishop. ImageIcon is retrieved using getImage(). Returned ImageIcon is stored in reference m but it is not getting displayed and another ImageIcon h, which is loaded directly, is getting displayed. What mistake I am making?

import javax.swing.*;

//Game.java

public class Game {

    public static void main(String[] args) {
        board b = new board();
        bishop bis1 = new bishop();
        bis1.setLocation(0, 0);
        ImageIcon m = bis1.getImage();
        b.squares[0][1].add(new JLabel(m));
        ImageIcon h = new ImageIcon("rook.png");
        b.squares[0][0].add(new JLabel(h));
    }
}

//bishop.java
import javax.swing.*;
import java.awt.*;

public class bishop {
    private ImageIcon img;
    private int row;
    private int col;

    public void bishop() {
        img = new ImageIcon("bishop.png");
    }

    public void setLocation(int i, int j) {
        row = i;
        col = j;
    }

    public int getX() {
        return row;
    }

    public int getY() {
        return col;
    }

    public ImageIcon getImage() {
        return img;
    }
}

// board.java
import javax.swing.*;
import java.awt.*;

public class board {
public JFrame frame;
public JPanel squares[][] = new JPanel[3][3];

public board() {
frame = new JFrame("Simplified Chess");
frame.setSize(900, 400);
frame.setLayout(new GridLayout(2,3));

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

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

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
  }

}
Was it helpful?

Solution

You defined your constructor wrong way - with an unnecessary void. Therefore your Bishop class calls default empty constructor so your variable img is never set correctly. Remove it so that your constructor would be called correctly:

Instead of this:

public void bishop() {
        img = new ImageIcon("bishop.png");
    }

Define it without void:

public bishop() {
            img = new ImageIcon("bishop.png");
        }

OTHER TIPS

There isn't enough of your code shown for me to tell exactly. I need to see the board class (btw: class names should be capitalised in Java: Board.java)

But I'm guessing that it is to do with the way the board class does the layout of your board.

Can you load and display only the bishop? That will determine whether the problem is in finding and loading the bishop. The following code will do that, which will help eliminate possible causes:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new JLabel(new ImageIcon("bishop.png")));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

What exactly is board? I am assuming it is something that might extend a Swing component such as a JFrame?

All GUI related events should take place on the Event Dispatcher Thread (EDT). This thread takes care of updating the GUI. In cases where you need to update the GUI from another class, you need to use SwingUtilities.invokeLater():

public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                board b = new board();
                bishop bis1 = new bishop();
                bis1.setLocation(0, 0);
                ImageIcon m = bis1.getImage();
                b.squares[0][1].add(new JLabel(m));
                ImageIcon h = new ImageIcon("rook.png");
                b.squares[0][0].add(new JLabel(h));
            }
        });
    }

Easiest Solution: Upload the images into your project folder. You can use a JLabel for example to input images. then write your code as the following sample:

 JLabel lblNewLabel = new JLabel("New Label");
 lblNewLabel.setIcon(new ImageIcon("Name of your image"));
 panel.add(lblNewLabel);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top