zoukankan      html  css  js  c++  java
  • java拼图

    package java拼图;
    
    import java.awt.Rectangle;
    
    import javax.swing.Icon;
    import javax.swing.JButton;
    
    public class Cell extends JButton{
        public static final int IMAGEWIDTH=100;
        public static final int IMAGEHEIGHT=100;//图片宽度
        private int place;//图片位置
        public Cell(Icon icon,int palce) {
            this.setSize(IMAGEWIDTH,IMAGEWIDTH);
            this.setIcon(icon);
            this.place=place;
        }
        
        //移动图片单元的办法
        public void move(Direction dir) {
            Rectangle rec=this.getBounds();//获取图片的rectangle对象
            //Rectangle 指定坐标空间中的一个区域,通过坐标空间中 Rectangle 对象左上方的点 (x,y)、宽度和高度可以定义这个区域。
            switch(dir){
                case UP:
                    this.setLocation(rec.x,rec.y-IMAGEWIDTH);
                    break;
                case DOWN:
                    this.setLocation(rec.x,rec.y+IMAGEWIDTH);
                    break;
                case LEFT:
                    this.setLocation(rec.x-IMAGEWIDTH, rec.y);
                    break;
                case RIGHT:
                    this.setLocation(rec.x+IMAGEWIDTH, rec.y);
                    break;
            }
        }
        
        public int getX() {
            return this.getBounds().x;//获取单元图片的x坐标
            
        }
        public int getY() {
            return this.getBounds().y;
        }
        public int getPlace() {
            return place;
        }
        
        
        
        
    //定义一个枚举类型表示移动方向
        public  enum Direction{UP,DOWN,LEFT,RIGHT}
        
    }
    package java拼图;
    
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.Random;
    
    import javax.imageio.ImageIO;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    import java拼图.Cell.Direction;
    
    public class GamePanel extends JPanel implements MouseListener{
           Icon icon=null;
            private Cell[] cells=new Cell[9];
            private Cell  cellBlank=null;
            public GamePanel() {
                super();
                setLayout(null);
                init();
            }
            public void init() {
                int x=0,y=0,w=50,h=50;
                BufferedImage src=null;
                BufferedImage newpic=null;
                try {
                    src=ImageIO.read(new File("images\link.jpg"));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                int num=0;//图片序号
                Cell cell=null;//单元图片对象
                for(int i=0;i<3;i++) {//分割图片时的行
                    for(int j=0;j<3;j++) {//
                        num=i*3+j;
                        x=j*100;
                        y=i*100;
                        newpic=src.getSubimage(x, y, w, h);
                        if(num+1==9)
                            icon=new ImageIcon("images\图片9.png");
                        else
                            icon=new ImageIcon(newpic);
                        cell=new Cell(icon,num);
                        cell.setLocation(j*cell.IMAGEWIDTH,i*Cell.IMAGEHEIGHT);
                        cells[num]=cell;
                        
                    }
                }
                for(int i=0;i<cells.length;i++) {
                    this.add(cells[i]);
                }
            }
            
            
            
            
            //对图片进行随机排序
            public void random() {
                Random random=new Random();
                int m,n,x,y;
                if(cellBlank==null) {
                    cellBlank=cells[cells.length-1];
                    for(int i=0;i<cells.length;i++) {
                        if(i!=cells.length-1) {
                            cells[i].addMouseListener(this);
                        }
                    }
                }
                for(int i=0;i<cells.length;i++) {
                    m=random.nextInt(cells.length);
                    n=random.nextInt(cells.length);
                    x=cells[m].getX();
                    y=cells[m].getY();
                    cells[m].setLocation(cells[n].getX(),cells[n].getY());
                    cells[n].setLocation(x,y);
                    
                }
            }
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                Cell cell=(Cell)e.getSource();//获取触发事件的对象
                int x=cells[8].getX();
                int y=cells[8].getY();
                if((x-cell.getX())==Cell.IMAGEWIDTH&&cell.getY()==y) {//左右移动
                    cell.move(Direction.RIGHT);
                    cellBlank.move(Direction.LEFT);
                }
                if((x-cell.getX())==-Cell.IMAGEWIDTH&&cell.getY()==y) {
                    cell.move(Direction.LEFT);
                    cellBlank.move(Direction.RIGHT);
                }
                if((cell.getX())==x&&cell.getY()-y==Cell.IMAGEHEIGHT) {//上下移动
                    cell.move(Direction.UP);
                    cellBlank.move(Direction.DOWN);
                }
                if((cell.getX())==x&&cell.getY()-y==-Cell.IMAGEHEIGHT) {//上下移动
                    cell.move(Direction.DOWN);
                    cellBlank.move(Direction.UP);
                }
                //判断是否成功
                if(isSuccess()) {
                    int i=JOptionPane.showConfirmDialog(this,"win!Do you want play again?","win",JOptionPane.YES_NO_OPTION);
                    if(i==JOptionPane.YES_OPTION) {
                        random();//重新开始一局
                    }
                }
                
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub
                
            }
            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                
            }
            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                
            }
            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub
                
            }
            
            
            public boolean isSuccess() {
                for(int i=0;i<cells.length;i++) {
                    int x=cells[i].getX();
                    int y=cells[i].getY();
                    if(i!=0) {
                        if(y/Cell.IMAGEHEIGHT*3+x/Cell.IMAGEWIDTH!=cells[i].getPlace()) {
                            return false;
                        }
                    }
                }
                return true;
            }
            
            
            
            
            
            
            
            
            
            
            
            
            
    }
    package java拼图;
    
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class MainFrame extends JFrame {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                //EventQueue 是一个与平台无关的类,它将来自于底层同位体类和受信任的应用程序类的事件列入队列。
                //invokeLater
    
    //public static void invokeLater(Runnable runnable)
    //导致 runnable 的 run 方法在 the system EventQueue 的指派线程中被调用。
                public void run() {
                    MainFrame frame=new MainFrame();
                    new Music();
                    frame.setVisible(true);
                }
            });
        }
        public MainFrame() {
            super();
            getContentPane().setLayout(new BorderLayout());
            setTitle("拼图游戏");
            setBounds(300,300,358,414);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final JPanel panel=new JPanel();
            getContentPane().add(panel, BorderLayout.NORTH);
            final GamePanel gamePanel=new GamePanel();
            getContentPane().add(gamePanel, BorderLayout.CENTER);
            final JButton button=new JButton();
            button.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    gamePanel.random();
                    
                }
            });
            button.setText("开始");
            panel.add(button);
            
        }
    
    }
    package java拼图;
    
    import java.applet.Applet;
    import java.applet.AudioClip;
    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URI;
    import java.net.URL;
    
    public class Music {//在游戏里导入音乐的方法!!!!!!!
      File f;
      URI uri;
      URL url;
      Music(){
          f=new File("D:/1.wav");
          uri=f.toURI();
          try {
            url=uri.toURL();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          AudioClip auu;//AudioClip 接口是用于播放音频剪辑的简单抽象。多个 AudioClip 项能够同时播放,得到的声音混合在一起可产生合成声音。
          auu=Applet.newAudioClip(url);
          auu.loop();//循环播放
          
      }
    }
  • 相关阅读:
    PHP trim() 函数
    php 计算2个日期的相差天数
    php date('Y-n-j')的和date('Y-m-d')的区别
    转移服务器
    Invalid argument supplied for foreach()解决办法
    wordpress 后台忘记密码怎么办
    qrcode js插件生成微信二维码
    thinkphp5 注释
    tp5 新增完数据,获取id
    resstFul服务文件上传下载
  • 原文地址:https://www.cnblogs.com/kekexxr/p/12292162.html
Copyright © 2011-2022 走看看