Panel - Redrawing panel didn't seem to be working! [solved]

Hello guys.

I was following Software Architecture & Design on Youtube and up until then there is no problem. But now when I got to redraw the board it didn’t work. ???

Need help to solve this problem.

You can see all of my code on my Github Here

But here some of the code:

In the BoardPanel class


		public void drawBoard() {
			removeAll();
			
			for(TilePanel tilePanel : tiles) {
				final int ID = tilePanel.ID;
				tilePanel = new TilePanel(this,ID);
				add(tilePanel);
			}
			
			validate();
			repaint();
		}

In the TilePanel class


		public void drawTile() {
			assignTileColor();
			assignTileIcon(chessBoard);
			validate();
			repaint();
		}

Please check it out!

[Editorial comment: This would be so much easier to do with JavaFX, the newer GUI for Java. You just update the location properties of the items involved and drawing pretty much takes care of itself. It kind of hurts my head, trying to remember all the rules pertaining to managing redrawing with Swing.]

For those looking at the above code, it’s helpful to know that at least some of it is quoted code is from here.

Also, the two classes mentioned are both extended JPanels.

I recommend to the OP trying to make a more simplified example that illustrates the problem, as a way of increasing the odds of getting a good solution (or finding one in the process).

Thank you for the suggestion. I will try to look into JavaFX soon ;D

Yeah, exactly. Please try to put together a MCVE that demonstrates your problem in as few lines as possible. Half the time you’ll figure out your problem when coming up with a minimal example.

But just a guess: after you add a component to a container, you have to revalidate and repaint that container.

I used Graphics to draw the board.


	public void draw() {
		Graphics g = image.getGraphics();

		board.getAllTiles().forEach(tile -> {
			if (tile.getRow() % 2 == 0) {
				g.setColor(tile.getCol() % 2 == 0 ? LIGHT : DARK);
				g.fillRect(TILE_WIDTH * tile.getRow(), TILE_HEIGHT * tile.getCol(), TILE_WIDTH, TILE_HEIGHT);
			} else {
				g.setColor(tile.getCol() % 2 != 0 ? LIGHT : DARK);
				g.fillRect(TILE_WIDTH * tile.getRow(), TILE_HEIGHT * tile.getCol(), TILE_WIDTH, TILE_HEIGHT);
			}
			
			if(!tile.isOccupied()) return;
			
			 final Piece piece = tile.getPiece();
			 g.drawImage(CACHE_IMAGE.get(piece),TILE_WIDTH * tile.getRow(),
			 TILE_HEIGHT * tile.getCol(),null);
		});

		g.drawImage(CACHE_IMAGE.get(new Rook(Alliance.BLACK, -1)), 100, 100, null);
	}

But now I don’t know what’s wrong with the code ???

On this line

g.drawImage(CACHE_IMAGE.get(new Rook(Alliance.BLACK, -1)), 100, 100, null);

the code works.
But when I loop through all tiles and draw the pieces it didn’t seem to work. Why ???

The entire JPanel class is here on my pastebin. Thank you.

If I understand what you wrote, it works with a single image, but not as a loop.

Have you stepped through the loop using a debugger?

Sometimes when loops seem to be where things break down, there is a problem such as the same object being used multiple times when you think you have a unique instance. This sort of thing usually pops out really quickly as an issue when you step through and verify the contents of the variables.

When I was doing graphics with Swing, I would normally use the Graphics2D object provided by the JPanel’s paint or draw method (can’t recall exactly which it was). When drawing images contained in other classes, I would provide that Graphics object to those images as a parameter, so everything gets drawn on the same Graphics object. It’s just a thought, but if you are drawing to different Graphics objects, that could cause some confusion. Pure speculation on my part, and it doesn’t account for a single image working but looped images not working.

Oh I just find out where the problem is.

I declare the

CACHE_IMAGE

using a Piece that have position at -1.

In the meantime, the pieces logically have position other than -1. So, the Collection will use the default equals and hash method to check the equality of those pieces and found NOTHING.

That’s why the line

g.drawImage(CACHE_IMAGE.get(new Rook(Alliance.BLACK, -1)), 100, 100, null);

works because I explicitly create a Rook piece that located at position -1.

My mistake ;D