目前我正在使用java构建游戏拼图。首先我的算法是创建按钮阵列2d,在这些按钮上设置图像并将它们添加到面板(网格布局3 * 3)。我的问题是如何将这些按钮上的每一张图像与原始图像进行比较? 我在下面的代码中添加按钮到Jpanel。
private void set() throws Exception {
position = new int[row][col];
lstBtn = new JButton[row][col];
count = new int[row * col];
arr=new ArrayList<>(9);
panelContainer.setLayout(null);
panelContainer.setLayout(new GridLayout(row, col));
BufferedImage img;
int numCount = 0;
int posNum=0;
orgImg=new BufferedImage[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
position[i][j] = posNum++;
if (i ==0 & j == 0) {
// For blank Button
lstBtn[i][j] = new JButton();
lstBtn[i][j].setBackground(Color.WHITE);
// panelContainer.add(lstBtn[i][j]);
lstBtn[i][j].setVisible(true);
arr.add(lstBtn[i][j]);
count[0] = numCount;
}
else {
numCount++;
lstBtn[i][j] = new JButton();
lstBtn[i][j].addActionListener(this);
img = image_cutting.getSubImage(j,i);//160, 116 //120, 87,
orgImg[j][i]=img;
lstBtn[i][j].setIcon(new ImageIcon(orgImg[j][i].getScaledInstance(160, 116, BufferedImage.SCALE_SMOOTH)));
lstBtn[i][j].setHorizontalTextPosition(JButton.CENTER);
lstBtn[i][j].setVerticalTextPosition(JButton.CENTER);
lstBtn[i][j].setFont(new Font("Dialog",2,20));
lstBtn[i][j].setForeground(Color.BLUE);
lstBtn[i][j].setText("" + numCount);
arr.add(lstBtn[i][j]);
// panelContainer.add(lstBtn[i][j]);
lstBtn[i][j].setVisible(true);
}
if (numCount == 0) {
continue;
}
count[numCount] = numCount;
}
此图像是以匹配图像滑动的拼图样本
答案 0 :(得分:1)
这是一个概念性的想法,需要一些充实,但是。从一个单一的想法开始,它持有它的顺序&#34;顺序&#34;或&#34;索引&#34;在拼图和它的图像
ArrayAdapter(Context context, int resource)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId)
Constructor
ArrayAdapter(Context context, int resource, T[] objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Constructor
ArrayAdapter(Context context, int resource, List<T> objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
Constructor
接下来,将其包装在某种模型中。它应该管理碎片,提供对它们的访问,并可能提供一个&#34;被命令&#34;检查以确定碎片是否有序。
此示例获取源图像,所需的cols /行数,并将图像切片并切成碎片。然后它将这些碎片混洗并添加一块空白块代替第一块(索引为public class PuzzelPiece {
private int index;
private Image img;
public PuzzelPiece(int index, Image img) {
this.index = index;
this.img = img;
}
public Image getImage() {
return img;
}
public int getIndex() {
return index;
}
}
)
0
它提供了一个简单的public class Puzzle {
private List<PuzzelPiece> pieces;
public Puzzle(BufferedImage source, int cols, int rows) {
int rowHeight = source.getHeight() / rows;
int colWidth = source.getWidth() / cols;
pieces = new ArrayList<>(25);
int index = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
Image img = source.getSubimage(col * colWidth, row * rowHeight, colWidth, rowHeight);
pieces.add(new PuzzelPiece(index++, img));
}
}
pieces.remove(0);
Collections.shuffle(pieces);
pieces.add(new PuzzelPiece(0, null)); // Blank
}
public boolean isOrdered() {
// Instead of sorting the list like this each time
// you could just maintain two lists to start with
// one ordered and one shuffled
List<PuzzelPiece> ordered = new ArrayList<>(pieces);
Collections.sort(ordered, new Comparator<PuzzelPiece>() {
@Override
public int compare(PuzzelPiece o1, PuzzelPiece o2) {
return o1.getIndex() - o2.getIndex();
}
});
boolean isOrdered = true;
for (int index = 0; index < ordered.size(); index++) {
if (ordered.get(index) != pieces.get(index)) {
isOrdered = false;
break;
}
}
return isOrdered;
}
public int size() {
return pieces.size();
}
public PuzzelPiece getPieceAt(int index) {
return pieces.get(index);
}
public void swap(PuzzelPiece piece, PuzzelPiece with) {
int pieceIndex = pieces.indexOf(piece);
int withIndex = pieces.indexOf(with);
pieces.set(pieceIndex, with);
pieces.set(withIndex, piece);
}
}
方法来交换片段和简单的swap
方法来检查列表是否有序
这是未经测试的,并且按照&#34;并且意在推动这个想法,提供一个实现