如何用eclipse做拼图

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;


import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;


public class app extends JFrame implements ActionListener {
Random random=new Random();
JMenuBar gamebar=new JMenuBar();
int count=0;
JMenu game=new JMenu("游戏");
JMenuItem start=new JMenuItem("开始 Ctrl+S");
JMenuItem operation=new JMenuItem("操作 Ctrl+R");
JMenuItem help=new JMenuItem("帮助 Ctrl+H");
JMenuItem about=new JMenuItem("关于 Ctrl+A");
JMenuItem exit=new JMenuItem("退出 Ctrl+Q");
//菜单栏

JButton[] buttons=new JButton[16];
Image[]img=new Image[16];
int[][] chessmap={
{ 0, 1, 2, 3 }, 
{ 4, 5, 6, 7 }, 
{ 8, 9, 10, 11 }, 
{ 12, 13, 14, 15 }
};
//按钮布局

public app() {
this.setTitle("带你们玩游戏");
Container container=this.getContentPane();
container.setLayout(new GridLayout(4, 4,0,0));
//窗体名字和打扫窗体空间

game.add(start);
game.add(operation);
game.add(help);
game.add(about);
game.add(exit);
gamebar.add(game);
this.setJMenuBar(gamebar);
//添加菜单项

start.addActionListener(this);
operation.addActionListener(this);
help.addActionListener(this);
about.addActionListener(this);
exit.addActionListener(this);
start.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S,java.awt.Event.CTRL_MASK));
operation.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R,java.awt.Event.CTRL_MASK));
help.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H,java.awt.Event.CTRL_MASK));
about.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A,java.awt.Event.CTRL_MASK));
exit.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_J,java.awt.Event.CTRL_MASK));
//菜单设置监听以及快捷键

for(int i=0;i<16;i++){
buttons[i]=new JButton();
img[i]=Toolkit.getDefaultToolkit().createImage("image//png_"+i+".png");
container.add(buttons[i]);
buttons[i].addActionListener(this);
}

this.setBounds(100, 100, 651, 701);
this.setVisible(true);
    setButtonImage();
    daluan();
setButtonImage();
}


public boolean isover()
    {
        for(int i=0;i<4;i++)
        {
          for(int j=0;j<4;j++)
          {
   if(chessmap[i][j]!=(i*4+j))return false;
           }
         }
        return true;
        
    }

public void daluan(){
int hang=3;
int lie=3;
for(int i=0;i<=10000;i++){
int fangxiang=random.nextInt(4)+1;
if(fangxiang==1 && hang-1>=0){
chessmap[hang][lie]=chessmap[hang-1][lie];
chessmap[hang-1][lie]=15;
hang=hang-1;}//向上交换
if(fangxiang==2 && hang+1<=3){
chessmap[hang][lie]=chessmap[hang+1][lie];
chessmap[hang+1][lie]=15;
hang=hang+1;}//向下交换
if(fangxiang==3 && lie-1>=0){
chessmap[hang][lie]=chessmap[hang][lie-1];
chessmap[hang][lie-1]=15;
lie=lie-1;}//向左交换
if(fangxiang==4 && lie+1<=3){
chessmap[hang][lie]=chessmap[hang][lie+1];
chessmap[hang][lie+1]=15;
    lie=lie+1;}//向右交换
                         }
}

//给按钮放入图片
private void setButtonImage() {
for(int i=0;i<16;i++){
buttons[i].setIcon(new ImageIcon(img[chessmap[i/4][i%4]]));
buttons[i].setSize(160,160);
buttons[i].setMargin(new Insets(0, 0, 0, 0));
buttons[i].setBorderPainted(false);
}
}



public void pintu(int p){
      int hang=p/4;
      int lie=p%4;
      if(hang-1>=0 && chessmap[hang-1][lie]==15){
      chessmap[hang-1][lie]=chessmap[hang][lie];
      chessmap[hang][lie]=15;}
      if(hang+1<=3 && chessmap[hang+1][lie]==15){
      chessmap[hang+1][lie]=chessmap[hang][lie];
      chessmap[hang][lie]=15;}
      if(lie-1>=0 && chessmap[hang][lie-1]==15){
      chessmap[hang][lie-1]=chessmap[hang][lie];
      chessmap[hang][lie]=15;}
      if(lie+1<=3 && chessmap[hang][lie+1]==15){
      chessmap[hang][lie+1]=chessmap[hang][lie];
      chessmap[hang][lie]=15;}
      setButtonImage();
       
       }







public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==start)
{
JOptionPane.showMessageDialog(null, "游戏开始!!!");
}
else if (e.getSource()==operation)
{
JOptionPane.showMessageDialog(null, "游戏操作如下:点击完整图上下左右四个图使其上下左右移动");
}
else if (e.getSource()==help)
{
JOptionPane.showMessageDialog(null, "QQ可加好友:1004297084"
+ "微信可加好友:fn1004297084"
+ "有在线客服24小时为您提供帮助"
);
}
else if (e.getSource()==about) 
{
JOptionPane.showMessageDialog(null,"浙江机电职业技术学院-网络1701 方大大 版权所有,盗版必究","版权说明",JOptionPane.ERROR_MESSAGE);
}
else if (e.getSource()==exit) {
System.exit(0);
}
if(isover()){
    JOptionPane.showMessageDialog(null,"挑战成功!!!");
    JOptionPane.showMessageDialog(null,"你用了"+count+"步");
    }
if(e.getSource()==start){
JOptionPane.showMessageDialog(null,"你确定进入轮回吗?");
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
chessmap[i][j]=i*4+j;
}
}
  daluan();
  setButtonImage();
}

for (int i = 0; i < 16; i++) {
if (e.getSource() == buttons[i]) {
count++;
//JOptionPane.showMessageDialog(null, "我是第" + (i + 1) + "个按钮,请开始游戏");
pintu(i);
break;
}

}

}

}

以下是本人的拼图成果

想在下面框框里分别加入计时器 暂停键 计步器


目前遇到的难题


愿意帮助我的大佬请在下面留言

猜你喜欢

转载自blog.csdn.net/Silver280/article/details/80643629