# java拼图小游戏
程序员文章站
2022-07-13 08:27:44
...
java拼图小游戏
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Font;
import javax.swing.UIManager;
import java.util.Random;
public class Pintu extends JFrame implements ActionListener {
JLabel picture, picbox;
JButton next, part[][];
int h = 2, l = 2;
JPanel control, pintu;
ImageIcon icon[][];
int game=1;
public Pintu(String s) {
super(s);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 329, 415);
getContentPane().setLayout(null);
control = new JPanel();// 添加上方控制模块
control.setBounds(0, 0, 313, 100);
control.setLayout(null);
getContentPane().add(control);
picbox = new JLabel();// 标签存放完整图片示例
picbox.setFont(new Font("华文琥珀", Font.PLAIN, 20));
picbox.setForeground(Color.RED);
picbox.setHorizontalAlignment(SwingConstants.CENTER);
picbox.setBounds(21, 10, 94, 84);
control.add(picbox);
next = new JButton("开始闯关");// 下一张按钮
next.setBackground(UIManager.getColor("Button.light"));
next.setFont(new Font("华文琥珀", Font.PLAIN, 20));
next.setForeground(Color.RED);
next.setBounds(134, 23, 144, 56);
next.addActionListener(this);
control.add(next);
pintu = new JPanel();// 拼图模块
pintu.setBounds(0, 98, 313, 278);
getContentPane().add(pintu);
pintu.setLayout(new GridLayout(3, 3));// 采用网格布局方式
part = new JButton[3][3];
icon = new ImageIcon[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
part[i][j] = new JButton("");
}
}
}
public void Start(int n) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
BufferedImage buf = null;// 原图像
BufferedImage buf2 = null;// 分割以后的图像
try {
buf = ImageIO.read(new File(n+".jpg"));
int width = 0, height = 0;
width = buf.getWidth() / 3;
height = buf.getHeight() / 3;
buf2 = buf.getSubimage(width * j, height * i, width, height);// 按下表切割图片
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
icon[i][j] = new ImageIcon(buf2);
Image temp2 = icon[i][j].getImage().getScaledInstance(pintu.getWidth() / 3, pintu.getHeight() / 3,
icon[i][j].getImage().SCALE_FAST);
icon[i][j] = new ImageIcon(temp2);
part[i][j].setIcon(icon[i][j]);
pintu.add(part[i][j]);
part[i][j].addActionListener(this);
part[i][j].setEnabled(true);
if (i == 2 && j == 2) {
part[i][j].setVisible(false);
}
}
}
Move();
}
public void Move() {//开具想让图片随机打乱,不可以用开局随机分配图片的方法,因为那样做可能会怎样移动都无法得到正确的图案
int n=20;
while(n>0) {
int i,j;
Random r=new Random();
int number=r.nextInt(4);
switch(number) {
case 1:{
i=h-1;
j=l;
if(i>2||i<0||j>2||j<0) {
continue;
}
break;
}
case 2:{
i=h+1;
j=l;
if(i>2||i<0||j>2||j<0) {
continue;
}
break;
}
case 3:{
i=h;
j=l-1;
if(i>2||i<0||j>2||j<0) {
continue;
}
break;
}
default:{
i=h;
j=l+1;
if(i>2||i<0||j>2||j<0) {
continue;
}
break;
}
}
part[h][l].setIcon(part[i][j].getIcon());
part[h][l].setVisible(true);
part[i][j].setIcon(icon[2][2]);
part[i][j].setVisible(false);
h = i;
l = j;
n--;}
}
public void perform(int n) {
ImageIcon icon1 = new ImageIcon(n+".jpg");
Image temp = icon1.getImage().getScaledInstance(picbox.getWidth(), picbox.getHeight(),
icon1.getImage().SCALE_DEFAULT);
icon1 = new ImageIcon(temp);
picbox.setIcon(icon1);
next.setText("下一张");
next.setEnabled(false);
for (int i1 = 0; i1 < 3; i1++) {
for (int j1 = 0; j1 < 3; j1++) {
part[i1][j1].setEnabled(true);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == next) {
Start(game);
perform(game);
game++;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (e.getSource() == part[i][j]) {
part[h][l].setIcon(part[i][j].getIcon());
part[h][l].setVisible(true);
part[i][j].setIcon(icon[2][2]);
part[i][j].setVisible(false);
h = i;
l = j;
// System.out.println(Check());
if (Check() == true) {
part[2][2].setVisible(true);
for (int i1 = 0; i1 < 3; i1++) {
for (int j1 = 0; j1 < 3; j1++) {
part[i1][j1].setEnabled(false);
picbox.setIcon(null);
picbox.setText("太棒了!");
}
}
if(game>=8) {
picbox.setText("你赢了!");
next.setEnabled(false);
return;
}
next.setEnabled(true);
}
break;
}
}
}
}
public boolean Check() {// 判断是否每一幅图都到达了应该到达的位置
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (!(i == 2 && j == 2) && part[i][j].getIcon().equals(icon[i][j]) == false)
return false;
}
}
return true;
}
}
import java.awt.EventQueue;
public class MainTest {
public static void main(String[] args) {
Pintu frame = new Pintu("拼图游戏");//创建一个名为拼图游戏的窗口
frame.setVisible(true);
}
}
下一篇: 拼图小游戏(前端)