张家口模型制作java拼图

 

//引入包名


//java.awt是一个软件包,包含用于创建用户界面和绘制图形图像的所有分类。
import java.awt.BorderLayout; //这是一个布置容器的边框布局,它可以对容器组件进行安排,并调整其大小,使其符合下列五个区域:北、南、东、西、中。
import java.awt.GridLayout;  //GirdLayout布局管理器将容器分割成纵横分割的网格,每个网格所占据的领域大小相同。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame; //Swing 是在AWT的基础上构建的一套新的图形界面系统
import javax.swing.JPanel;
import javax.swing.ImageIcon;  
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;

public class Game extends JFrame { 
 //全局变量
 private JLabel modelLabel;
 private JPanel centerPanel;
 private JButton emptyButton;
 int number = 0;
 //构造方法
 public Game() {
  super();
  setTitle("金手指大赛———拼多多");
  setResizable(false);//参数值为true时,表示在生成的窗体可以自由改变大小,值为false时,不可以自由改变该窗体的大小。
  setBounds(100, 100, 370, 525);  //建立坐标图像大小
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  //大框
  centerPanel = new JPanel();
  centerPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION,
    TitledBorder.DEFAULT_POSITION, null, null));
  centerPanel.setLayout(new GridLayout(4, 0));
  
  getContentPane().add(centerPanel, BorderLayout.CENTER); 
  String[][] rightOrder = initPicture();  //第二个方法
  //按行、按列添加素材图片
  for (int row=0; row<4; row++) {  //修改数字可变换拼图模式。例如:改成3*3
   for (int col=0; col<4; col++) {
    final JButton button = new JButton();
    button.setName(row+""+col);
    button.setIcon(new ImageIcon(rightOrder[row][col]));
    if (rightOrder[row][col].equals("image/"+ number+"00.jpg"))
     emptyButton = button;
    button.addActionListener(new imgButton());
    centerPanel.add(button);
   }
  }
  //UI布局
  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);
  //显示原图
  modelLabel = new JLabel();
  modelLabel.setIcon(new ImageIcon("image/"+ number+ "model.jpg"));
  topPanel.add(modelLabel, BorderLayout.WEST);
  //在右侧加个添加两个按钮。下一张和开始
  JPanel rightPanel = new JPanel();
  topPanel.add(rightPanel,BorderLayout.CENTER);
  rightPanel.setLayout(new BorderLayout());
  JButton nextButton = new JButton();
  nextButton.setText("下一关");
  nextButton.setBackground(Color.WHITE);
  nextButton.addActionListener(new NextButton());
  rightPanel.add(nextButton,BorderLayout.NORTH);
  //创建“开始”button,添加事件
  final JButton startButton = new JButton();
  startButton.setText("开 始");
  startButton.setFont(new Font("宋体", Font.PLAIN, 18));
  
  startButton.addActionListener(new StartButton());//第一个方法
  rightPanel.add(startButton, BorderLayout.CENTER);
 }
 
 //开局类
 class StartButton implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   String[][] randomOrder = randomPic(); 
   int i= 0;
   for (int row=0; row<4; row++) {
    for (int col=0; col<4; col++) {
     JButton button = (JButton) centerPanel.getComponent(i++);
     button.setIcon(new ImageIcon(randomOrder[row][col]));
     if(randomOrder[row][col].equals("image/"+ number+ "00.jpg"))
      emptyButton=button;
     }
    }
   }
  }
 
 //初始化图片,正确的顺序
 private String[][] initPicture() {
  String[][] rightOrder = new String[4][4];
  for (int row=0; row<4; row++) {
   for (int col=0; col<4; col++) {
    rightOrder[row][col] = "image/"+ number+ row+ col+ ".jpg";
   }
  }
  return rightOrder;
 }
 //调用随机函数,排列图片
 private String[][] randomPic() {
  String[][] rightOrder = new String[4][4];
  for (int row=0; row<4; row++) {
   for (int col=0; col<4; col++) {
    rightOrder[row][col] = "image/"+ number+ row+ col+ ".jpg";
   }
  }
  String[][] randomOrder = new String[4][4]; //定义随机变量
  //随机排列
  for (int row=0; row<4; row++) {
   for (int col=0; col<4; col++) {
    while (randomOrder[row][col]==null) {
     int r = (int) (Math.random()*4);
     int c = (int) (Math.random()*4);
     if (rightOrder[r][c] != null) {
      randomOrder[row][col] = rightOrder[r][c];
      rightOrder[r][c] = null;
     }
    }
   }
  }
  return randomOrder;
 } 
 class imgButton implements ActionListener {//继承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("image/"+ number+ "00.jpg"));
    emptyButton = clickButton;
   }
  }
 }
 //下一张
 class NextButton implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   if (number==5) {
    number=0;
   } else {
    ++number;
   }
   modelLabel.setIcon(new ImageIcon("image/"+number+"model.jpg"));
   String[][] rightOrder = initPicture();
   int i=0;
   for (int row=0; row<4; row++) {
    for (int col=0; col<4; col++) {
     JButton button = (JButton) centerPanel.getComponent(i++);
     button.setIcon(new ImageIcon(rightOrder[row][col]));
     if(rightOrder[row][col].equals("image/"+ number+ "00.jpg"))
      emptyButton=button;
    }
   }
  }
 }
 
 public static void main(String[] args) {
  try {
   Game frame = new Game();
   frame.setVisible(true);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值