我无法在我的代码中获得我的洗牌方法的逻辑,也无法在我的图像方法idk
中获得解决此问题所需的操作。我几乎尝试了所有东西,但总是有例外。
我的 isFinished() 方法几乎可以工作,但它有一个小问题,当我赢了时,我需要点击一个按钮来获取消息。
以下是我的代码:
public class Puzzle extends JFrame implements ActionListener
{
private JPanel centerPanel = new JPanel();
private JPanel northPanel = new JPanel();
private JPanel southPanel = new JPanel();
private JButton newGame = new JButton("New Game");
private JButton[] btArray = new JButton[8];
private JLabel moves = new JLabel("Moves : ");
private JLabel label;
private Container mainPanel = this.getContentPane();
private int[][] pos;
private int count;
private String str;
int width, height;
public Puzzle() {
super("Puzzle Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initUI();
}
public void initUI() {
pos = new int[][]{
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{9,10,11}
};
/** Initialize button array */
for (int i = 0; i < 8; i++)
{
// image = createImage(new FilteredImageSource(source.getSource(),
// new CropImageFilter(i*width/3, i*height/4,
// (width/3)+1, height/4)));
// btArray.setIcon(new ImageIcon(image));
btArray[i] = new JButton("B" + i);
// btArray[i].setIcon(new ImageIcon(image));
}
/** North Panel */
newGame.setFocusable(false);
newGame.addActionListener(this);
northPanel.setBackground(Color.red);
northPanel.add(newGame);
mainPanel.add(northPanel, BorderLayout.NORTH);
/** South Panel */
southPanel.add(moves);
southPanel.setBackground(Color.LIGHT_GRAY);
mainPanel.add(southPanel, BorderLayout.SOUTH);
/** Game Panel */
centerPanel.setLayout(new GridLayout(3, 3, 0, 0));
centerPanel.setBackground(Color.BLACK);
/** Add actionListeners to buttons */
for (int i = 0; i < 8; i++)
{
btArray[i].addActionListener(this);
}
newGame();
mainPanel.revalidate();
label = new JLabel("");
label.setBackground(Color.BLACK);
centerPanel.add(label);
mainPanel.add(centerPanel);
}
public void newGame()
{
count = 0;
for (int j = 7; j >= 0; j--)
{
centerPanel.remove(btArray[j]);
}
for (int j = 7; j >= 0; j--)
{
centerPanel.add(btArray[j]);
}
centerPanel.revalidate();
}
public boolean isFinished() {
if ((btArray[0].getY() == btArray[1].getY() && btArray[1].getY() == btArray[2].getY()) && (btArray[3].getY() == btArray[4].getY() && btArray[4].getY() == btArray[5].getY())
&& (btArray[6].getY() == btArray[7].getY()))
{
if (btArray[0].getX() == btArray[3].getX() && btArray[3].getX() == btArray[6].getX()
&& btArray[1].getX() == btArray[4].getX() && btArray[4].getX() == btArray[7].getX()
&& btArray[2].getX() == btArray[5].getX())
{
return true;
}
}
return false;
}
}
public static void main(String[] args)
{
Puzzle puz = new Puzzle();
puz.setBounds(20, 20, 300, 325);
puz.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
JButton button = (JButton) ae.getSource();
Dimension size = button.getSize();
if (isFinished())
{
JOptionPane.showMessageDialog(null, "You have Won the game");
}
if (ae.getSource() == newGame)
{
newGame();
}
int labelX = label.getX();
int labelY = label.getY();
int buttonX = button.getX();
int buttonY = button.getY();
int buttonPosX = buttonX / size.width;
int buttonPosY = buttonY / size.height;
int buttonIndex = pos[buttonPosY][buttonPosX];
if (labelX == buttonX && (labelY - buttonY) == size.height) {
int labelIndex = buttonIndex + 3;
centerPanel.remove(buttonIndex);
centerPanel.add(label, buttonIndex);
centerPanel.add(button, labelIndex);
centerPanel.validate();
count++;
}
if (labelX == buttonX && (labelY - buttonY) == -size.height) {
int labelIndex = buttonIndex - 3;
centerPanel.remove(labelIndex);
centerPanel.add(button, labelIndex);
centerPanel.add(label, buttonIndex);
centerPanel.validate();
count++;
}
if (labelY == buttonY && (labelX - buttonX) == size.width) {
int labelIndex = buttonIndex + 1;
centerPanel.remove(buttonIndex);
centerPanel.add(label, buttonIndex);
centerPanel.add(button, labelIndex);
centerPanel.validate();
count++;
}
if (labelY == buttonY && (labelX - buttonX) == -size.width) {
int labelIndex = buttonIndex - 1;
centerPanel.remove(buttonIndex);
centerPanel.add(label, labelIndex);
centerPanel.add(button, labelIndex);
centerPanel.validate();
count++;
}
str = String.valueOf(count);
moves.setText("Moves : " + str);
}
}
洗牌的方法,但我得到一个错误,我无法将我的按钮转换为 int
public void shuffle()
{
int aux[] = new int[8];
int i, j, counter = 0;
for(i=0; i<16; i++)
{
aux[i] = (int)(Math.random()*15);
}
for(i=0; i<8; i++)
{
for(j=0; j<i; j++)
{
if(aux[i]==aux[j])
{
aux[i] = (int)(Math.random()*16);
j=-1;
}
}
}
for(i=0; i<7; i++)
{
btArray[i] = aux[counter];
counter++;
}
对于我的图像,我尝试了此代码但没有用
ImageIcon sid = new ImageIcon(PuzzleBoard.class.getResource("flower.jpg"));
centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(4, 4, 0, 0));
source = sid.getImage();
width = sid.getIconWidth();
height = sid.getIconHeight();
for ( int j = 0; j < 7; j++) {
button = new JButton();
button.addActionListener(this);
centerPanel.add(button);
image = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(j*width/3, i*height/4,
(width/3)+1, height/4)));
btArray[j].setIcon(new ImageIcon(image));
}