[英]Sliding Puzzle Java Shuffle issue
I am trying to shuffle pieces for a 15 piece sliding puzzle when you click the shuffle button. 当您单击随机播放按钮时,我正在尝试随机播放15个滑动拼图。 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:
不幸的是,我不知道如何“抓取” JPanel按钮,因为它们是动态的并且不会“传递”给动作监听器:这是我的for循环,用于创建和添加按钮:
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). 这就是到目前为止我有4次随机移动的随机播放代码(我知道这很糟糕,我不知道更好)。 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.
我尝试使用Component getComponent(int),但是我不知道如何实现它。 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. 更加依赖于维护对
JLabel
的某种引用,然后尝试维护有关标签携带内容的信息。
For example, you could simply place the JLabel
s into a List
and shuffle the list, removing the labels and adding them again. 例如,您可以简单地将
JLabel
放置在一个List
然后对列表进行混洗,删除标签并再次添加它们。
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... 您也可以将数组用于相同的想法...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.