Reputation: 59
i'm quite new in java and i'm trying create my own scrabble game. I created my own classes Board and Tile both JPanels. while im drawing tiles on my Board :
Tile tile = new Tile(currentlyChosenLetter, jump);
board.add(tile);
tile.setBounds(x * jump + 1, y * jump + 1, jump - 2, jump - 2);
when im doing like this everything seems working fine :
but after adding :
board.revalidate();
board.repaint();
tiles are misplaced, i need to repaint in case of removing Tiles.
x and y im getting from my mouse position :
int jump = board.getHeight() / 15;
int x = (e.getX() / jump);
int y = (e.getY() / jump);
where e is MouseEvent.
Upvotes: 0
Views: 291
Reputation: 324108
board.revalidate();
board.repaint();
The revalidate() statement invokes the layout manager so the child components are given a size and location based on the rules of the layout manager. The default layout manager for a JPanel
is the FlowLayout
so the components appear on a single line.
So don't use setBounds(...)
. Instead use a proper layout manager like the GridLayout
and add components to each square of the grid.
I would suggestion you might want to a JLabel
to each grid. Then you can add and Icon
to each label with the default icon for a given square. Then as a letter is added you replace the Icon with the text.
Upvotes: 1