1

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!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
vito
  • 323
  • 1
  • 10
  • `removeAll(); requestFocusInWindow();` Don't call these methods from within a paint method. – Andrew Thompson Jun 14 '14 at 10:06
  • Please share a minimal testable code. Are you able to get the images? – Braj Jun 14 '14 at 10:13
  • I've tried removing both requestFocus and removeAll. Still doesnt show Images. Do i have to call removeAll? i mean the code gets called quite a few times because the board gets updated, is it necessary to remove all labels before adding them back? – vito Jun 14 '14 at 10:17

1 Answers1

3

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.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • That's great advice, but doesn't answer the question and Andrew hit on the main cause of the problem – MadProgrammer Jun 14 '14 at 10:10
  • The code is not complete, I can't test it. Meanwhile I am trying to investigate it. – Braj Jun 14 '14 at 10:11
  • Then you should have sake for more ;) Take close look at the paint method...and the fact that the OP doesn't understand how basic painting is done ;) – MadProgrammer Jun 14 '14 at 10:12
  • and instead of calling repaint() straightforward calling paintComponent() every time theres a change in the board? – vito Jun 14 '14 at 10:19
  • `repaint` internally call `paintComponent` – Braj Jun 14 '14 at 10:20
  • Thank you so much!! It really did the trick, replacing paint() with paintComponent() solved it! THANKS – vito Jun 14 '14 at 10:22
  • Oh another problem occurred. Now i'm getting all the boards painted. Meaning after every change at the board nothing gets deleted only added.. Is there a way around this? EDIT: ok, just added removeAll() inside the paintComponent() was good enough. – vito Jun 14 '14 at 10:25