Remove, Scramble, and Swap the Sub Images
Learn to remove, revalidate, scramble, and swap the images when they are clicked.
Adding and removing components
Normally, you add all the components to the window, pack the window, then display the window. If you add or remove components later, the layout may need to be recalculated and components redisplayed. To do that, revalidate the container that holds the components.
Removing tiles before adding new ones
Currently divideImage()
adds tile buttons to the center panel. But what if divideImage()
gets called a second time? Adding tile buttons again will result in too many buttons!
Remove tile buttons in divideImage()
before adding new ones. After adding or removing components in a panel, SlidingTiles will need to revalidate the panel.
- Remove all the components from
centerPanel
. Use JPanel’sremoveAll()
method. - Revalidate
centerPanel
. Use JPanel’srevalidate()
method.
...
private void divideImage() {
centerPanel.setLayout(new GridLayout(gridSize, gridSize));
add(centerPanel, BorderLayout.CENTER);
centerPanel.______________;
int imageId = 0;
for(int row=0; row<gridSize; row++) {
...
}
centerPanel.____________();
}
...
Get hands-on with 1200+ tech skills courses.