1. El propósito del experimento
Usa y domina la mayoría de las técnicas de programación Swing.
2. Contenido experimental
1. Crea una clase llamada MedleyGame, que hereda la clase JFrame;
2. Luego declara un objeto de panel y un objeto de botón en esta clase. El objeto de panel se usa para agregar botones de rompecabezas, y el objeto de botón es el que actualmente muestra un imagen en blanco. Botón
3. Por último, escriba un método main () y un método de construcción MedleyGame () para esta clase, y establezca las propiedades relevantes del formulario en el método de construcción, como el título, la posición de visualización y el tamaño del formulario.
Tres, el entorno experimental
SO: Windows 10, entorno de compilación: eclipse
4. Descripción del proceso experimental
Muestre algunos a continuación 内联代码片
.
Project:MedleyGame
// MedleyGame.java(源代码):
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class MedleyGame extends JFrame {
private JPanel centerPanel;// 拼图按钮面板
private JButton emptyButton;// 空白按钮对象
class ImgButtonAction implements ActionListener {
// 拼图按钮监听器
public void actionPerformed(ActionEvent e) {
String emptyName = emptyButton.getName();// 获得空白按钮的名称
char emptyRow = emptyName.charAt(0);// 获得空白按钮所在的行
char emptyCol = emptyName.charAt(1);// 获得空白按钮所在的列
JButton clickButton = (JButton) e.getSource();// 获得被单点按钮对象
String clickName = clickButton.getName();// 获得被单击按钮的名称
char clickRow = clickName.charAt(0);// 获得被单击按钮所在的行
char clickCol = clickName.charAt(1);// 获得被单击按钮所在的行
// 判断被单击按钮与空白按钮是否相邻
if (Math.abs(clickRow - emptyRow) + Math.abs(clickCol - emptyCol) == 1) {
// 将被单击按钮的图片移动到空白按钮上
emptyButton.setIcon(clickButton.getIcon());
// 设置被单击的按钮显示空白图片
clickButton.setIcon(new ImageIcon("src/22.jpg"));
emptyButton = clickButton;// 将被单击的按钮设置为空白按钮
}
}
}
class StartButtonAction implements ActionListener {
// 下一局按钮监听器
public void actionPerformed(ActionEvent e) {
String[][] stochasticOrder = reorder();// 获得网格图片的随机摆放顺序
int i = 0;// 拼图按钮在拼图按钮面板中的索引
for (int row = 0; row < 3; row++) {
// 遍历行
for (int col = 0; col < 3; col++) {
// 遍历列
JButton button = (JButton) centerPanel.getComponent(i++);
button.setIcon(new ImageIcon(stochasticOrder[row][col]));
if (stochasticOrder[row][col].equals("src/22.jpg"))
emptyButton = button;
}
}
}
}
public static void main(String[] args) {
try {
MedleyGame frame = new MedleyGame();// 创建本类对象
frame.setVisible(true);// 设置窗体为可见
// frame.Cut();
} catch (Exception e) {
e.printStackTrace();
}
}
private static String[][] reorder() {
// 用来获取网格图片的随机摆放顺序
String[][] exactnessOrder = new String[3][3];// 网格图片的正确摆放顺序
for (int row = 0; row < 3; row++) {
// 遍历行
for (int col = 0; col < 3; col++) {
// 遍历列
exactnessOrder[row][col] = "src/" + row + col + ".jpg";
}
}
String[][] stochasticOrder = new String[3][3];// 网格图片的随机摆放顺序
for (int row = 0; row < 3; row++) {
// 遍历行
for (int col = 0; col < 3; col++) {
// 遍历列
while (stochasticOrder[row][col] == null) {
// 随机摆放顺序的指定网格为空
int r = (int) (Math.random() * 3);// 取随机行
int c = (int) (Math.random() * 3);// 取随机列
if (exactnessOrder[r][c] != null) {
// 正确摆放顺序的指定网格不为空
stochasticOrder[row][col] = exactnessOrder[r][c];
exactnessOrder[r][c] = null;
}
}
}
}
return stochasticOrder;
}
public void Cut() throws IOException {
int pw=centerPanel.getWidth();//获取面板宽高
int ph=centerPanel.getHeight();
BufferedImage bufImage = ImageIO.read(new File("C:\\Users\\mingjie\\eclipse-workspace\\MedleyGame\\src\\1.jpg"));
for(int x=0;x<=200;x+=100)
for(int y=0;y<=200;y+=100) {
BufferedImage cutImage;
cutImage=bufImage.getSubimage (x, y,pw/3, ph/3);
ImageIO.write(cutImage, "JPEG", new File("C:\\Users\\mingjie\\eclipse-workspace\\MedleyGame\\src\\"+x/100+y/100+".jpg"));
}
}
public MedleyGame() {
super();// 继承JFrame类的构造方法
setResizable(false);// 窗体大小不可改变
setTitle("拼图游戏");// 设置窗体的标题
setBounds(300, 100, 314,439 );// 设置窗体的显示位置及大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭窗体时退出程序
// 创建面板对象
final JPanel topPanel = new JPanel();
topPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION,
null, null));// 为面板添加边框
topPanel.setLayout(new BorderLayout());// 设置面板采用边界布局
getContentPane().add(topPanel, BorderLayout.NORTH);// 将面板添加到窗体顶部
final JLabel modelLabel = new JLabel();// 创建显示参考图片的标签对象
modelLabel.setBounds(0, 0, 100, 100);
modelLabel.setIcon(new ImageIcon("src/3.jpg"));// 设置标签显示的参考图片
topPanel.add(modelLabel, BorderLayout.WEST);// 将标签添加到面板的左侧
// 创建“下一句”按钮对象
final JButton startButton = new JButton();// 创建“下一句”按钮对象
startButton.setText("下一局");// 设置按钮的标签文本
startButton.addActionListener(new StartButtonAction());// 为按钮添加监听器
topPanel.add(startButton, BorderLayout.EAST);// 将按钮添加到面板的中间
// 创建拼图按钮面板对象
centerPanel = new JPanel();
centerPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null));// 为面板添加边框
centerPanel.setLayout(new GridLayout(0, 3));// 设置拼图按钮面板采用3列的网格布局
getContentPane().add(centerPanel, BorderLayout.CENTER);// 将面板添加到窗体的中间
String[][] stochasticOrder = reorder();// 获得网格图片的随机摆放顺序
for (int row = 0; row < 3; row++) {
// 遍历行
for (int col = 0; col < 3; col++) {
// 遍历列
final JButton button = new JButton();// 创建拼图按钮对象
button.setName(row + "" + col);// 设置按钮的名称
button.setIcon(new ImageIcon(stochasticOrder[row][col]));// 为按钮设置图片
if (stochasticOrder[row][col].equals("src/22.jpg"))// 判断是否为空白按钮
emptyButton = button;
button.addActionListener(new ImgButtonAction());// 为拼图按钮添加监听器
centerPanel.add(button);// 为按钮添加到拼图按钮面板中
}
}
}
}