vito
vito

Reputation: 323

Image not showing on JLabel

I've been tasked to make a java replica of Candy Crush Saga. Right now im quite stuck on the GUI part. I've decided that each candy will be represented by a JLabel holding the candy icon, and a mouselistener to control the functionality. What happens is, after i finish running the screen shows, the mouse listeners respond but the image doesn't show, meaning i can press the labels get a response but cannot see the icons. I take this as the labels are on the panel but somehow not visible or the icon is not loaded correctly - although when checking the ImageIcon.toString it shows the path to the file. Any ideas?

Here is the code:

public class Board extends JPanel {
Candy[][] board;
static final int TILE_SIZE = 55;
static final int  TILES_MARGIN = 8;


public Board() {
    setFocusable(true);

    board = new Candy[13][13];
    Candy c;
    for (int i = 0; i < 13; i++)
        for (int j = 0; j < 13; j++) {
            if (i != 0 && i != 1 && j != 0 && j != 1 && i != 11 && i != 12 && j != 11 && j != 12) {
                Random rand = new Random();
                int randomNum = rand.nextInt((6 - 1) + 1) + 1;
                c = new Basic(randomNum, this);
            } else {
                c = new Basic(0, this);
            }
            setAt(i, j, c);
        }
    repaint();
}

public void drawCandy(Graphics g2, Candy candy, int x, int y) {
    Graphics2D g = ((Graphics2D) g2);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    int value = candy.getClr();
    int xOffset = offsetCoors(x);
    int yOffset = offsetCoors(y);
    ImageIcon myImg = candy.switchIcon();
    JLabel toAdd = new JLabel(myImg);
    toAdd.setIcon(myImg);
    toAdd.setLocation(xOffset,yOffset);
    toAdd.setSize(TILE_SIZE,TILE_SIZE);
    toAdd.addMouseListener(new ButtonPressed(x,y,candy));
    toAdd.setVisible(true);
    if (value != 0)
        add(toAdd);
}
private static int offsetCoors(int arg) {
    return (arg-2) * (TILES_MARGIN + TILE_SIZE) + TILES_MARGIN;
}

public void paint(Graphics g) {
    super.paint(g);
    removeAll();
    requestFocusInWindow();
    g.setColor(Color.black);
    g.fillRect(0, 0, this.getSize().width, this.getSize().height);

    for (int x = 2; x < 11; x++) {
        for (int y = 2; y < 11; y++) {
            drawCandy(g, board[x][y], x, y);
        }
    }

    validate();
}

and the JFrame :

public Game() {
    super("Candy Crush Game");
    setDefaultLookAndFeelDecorated(true);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new BorderLayout());
    setSize(600, 600);
    setResizable(false);
    setLocationRelativeTo(null);
    this.board = new Board();


    this.score = 0;
    this.moves = 20;

    this.getContentPane().add(board, BorderLayout.CENTER);
    setVisible(true);


     board.checkSquare(2, 2, 10, 10);


}

I'm quite frustrated, any help will be great!

Upvotes: 1

Views: 770

Answers (1)

Braj
Braj

Reputation: 46841

Instead of overriding paint() method use paintComponent() method for JPanel.

@Overrie
public void paintComponent(Graphics g) {
     super.paintComponent(g);
     //your custom painting here
}

Read more


There might be some issue in reading image icon. My another post might help you.

Instead of creating new JLabel simply change it's icon.

Upvotes: 3

Related Questions