I am trying to shuffle pieces for a 15 piece sliding puzzle when you click the shuffle button. Unfortunately, I don't know how to "grab" the JPanel buttons because they are dynamic and don't get "passed" to the Action Listener: Here's my for loop that creates & adds buttons:
pos = new int[][] {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11},
{12, 13, 14, 15}
};
centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(4, 5, 0, 0));
add(Box.createRigidArea(new Dimension(0, 5)), BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);
int counter = 0;
for ( int i = 0; i < 4; i++) {
for ( int j = 0; j < 4; j++) {
if ( j == 3 && i == 3) {
label = new JLabel("");
centerPanel.add(label); // EMPTY LABEL
} else {
counter++;
button = new JButton();
button.setText(String.valueOf(counter));
button.addActionListener(this);
centerPanel.add(button);
}
}
}
Then I add the Shuffle button:
southPanel = new JPanel(new FlowLayout());
JButton shuffleButton = new JButton("Shuffle");
southPanel.add(shuffleButton);
shuffleButton.addActionListener(this);
Then this is the Shuffle code I have 4 random moves I have so far (and I know this is awful and I don't know better). It moves the label but pushes all the buttons down in order :(
public void actionPerformed(ActionEvent e) {
//GET EMPTY LABEL LOCATION
int labelX = label.getX();
int labelY = label.getY();
int labelPosX = labelX / sizeLabel.width;
int labelPosY = labelY / sizeLabel.height;
int labelIndex = pos[labelPosX][labelPosY];
// GET button location
buttonX = label.getX() - size.width;
buttonY = label.getY();
// GET button position
buttonPosX = buttonX / size.width;
buttonPosY = buttonY / size.height;
buttonIndex = pos[buttonPosY][buttonPosX];
// IF LABEL can be moved
if ((labelY == buttonY && (labelX - buttonX) == size.width )) {
System.out.println("LABEL MOVES LEFT");
labelIndex = buttonIndex + 1;
centerPanel.add(label,buttonIndex);
// HERE I NEED TO "GRAB BUTTON" AND PUT it
// IN LABEL INDEX ???
labelY = label.getY();
labelX = label.getX();
}
Any help is appreciated!! I tried using Component getComponent(int) but I don't know how to implement it. I think something like that would be perfect...
Rely more on maintaining some kind of reference to the JLabel
s then trying to maintain information about what the labels carry.
For example, you could simply place the JLabel
s into a List
and shuffle the list, removing the labels and adding them again.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Shuffle {
public static void main(String[] args) {
new Shuffle();
}
public Shuffle() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final PuzzelPane puzzelPane = new PuzzelPane();
JButton shuffle = new JButton("Shuffel");
shuffle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
puzzelPane.shuffle();
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(puzzelPane);
frame.add(shuffle, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PuzzelPane extends JPanel {
private List<JLabel> labels;
public PuzzelPane() {
setLayout(new GridLayout(3, 3));
labels = new ArrayList<>(9);
for (int index = 0; index < 9; index++) {
JLabel label = new JLabel(String.valueOf(index));
label.setHorizontalAlignment(JLabel.CENTER);
labels.add(label);
}
shuffle();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public void shuffle() {
removeAll();
Collections.shuffle(labels);
for (JLabel label : labels) {
add(label);
}
revalidate();
}
}
}
You could also use arrays for the same idea...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.