Java连连看代码---学java时练手的,凑合看吧。

来源:互联网 发布:曼谷酒店知乎 编辑:程序博客网 时间:2024/05/06 20:12

MainFrame.java

 

Code:
  1. import javax.swing.*;   
  2. import java.awt.*;   
  3. import java.awt.event.*;   
  4. import java.io.File;   
  5. public class MainFrame {   
  6.     public static void main(String[] argv)   
  7.     {   
  8.         int a=4;   
  9.         a=5;   
  10.         new Frames();   
  11.     }   
  12.   
  13. }   
  14. class Frames extends JFrame   
  15. {   
  16.     final int rowNum=10;   
  17.     final int columnNum=10;   
  18.     public Frames()   
  19.     {   
  20.         String dirPath=System.getProperty("user.dir");   
  21.         dirPath+="//NodePic";   
  22.         DrawPanel dPanel=new DrawPanel(dirPath, rowNum, columnNum);   
  23.         dPanel.setNodeMap(NodeMap.CreateNodeMap(columnNum, rowNum, dPanel.getImages(),dPanel.getFixImage()));   
  24.         this.getContentPane().add(dPanel);   
  25.         this.setSize(600,600);   
  26.         this.setTitle("连连看");   
  27.         this.setVisible(true);   
  28.         this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);   
  29.     }   
  30.        
  31.            
  32.   
  33.        
  34. }   
  35. class DrawPanel extends JPanel   
  36. {   
  37.   
  38.     Node preNode;   
  39.     int nodeWidth;   
  40.     int nodeHeight;   
  41.     private MediaTracker mt ;   
  42.     private Image star[];   
  43.     private Image[] statFix;   
  44.     private File fileDir;   
  45.     private int rowNum,columnNum;   
  46.     private final int Pic_Num=10;   
  47.     private final int Node_Num=20;   
  48.     private Node[][] nodeMap;   
  49.     private NodeMap _nodemap;   
  50.     private Graphics myGraphics;   
  51.     public DrawPanel(String dirPath, int row, int column)   
  52.     {   
  53.         super();   
  54.         rowNum=row;   
  55.         columnNum=column;   
  56.            
  57.         mt= new MediaTracker(this);   
  58.         fileDir=new File(dirPath);   
  59.         File list[]=fileDir.listFiles();   
  60.         RandList randlist=new RandList(list.length);   
  61.         int[] randNumList=randlist.getRandNumList(Pic_Num);   
  62.         star=new Image[randNumList.length+5];//其中三个为null,就是为了不给位图   
  63.         for(int i=0; i<randNumList.length; i++)   
  64.         {   
  65.             if(!list[randNumList[i]].isFile())   
  66.             {   
  67.                 System.out.println("NodePic ERROR");   
  68.                 return ;   
  69.             }   
  70.             star[i]= Toolkit.getDefaultToolkit().createImage(list[randNumList[i]].getPath());   
  71.             mt.addImage(star[i], i);   
  72.   
  73.         }   
  74.         try {   
  75.             mt.waitForAll();   
  76.         } catch (InterruptedException e) {   
  77.             // TODO Auto-generated catch block   
  78.             e.printStackTrace();   
  79.         }   
  80.         statFix=(Image[])(new FixPic(star)).getFixPic();   
  81.            
  82.         this.addMouseListener(new PanelListener());   
  83.            
  84.            
  85.            
  86.     }   
  87.     public void setNodeMap(Node[][] map)   
  88.     {   
  89.         this.nodeMap=map;   
  90.         _nodemap=new NodeMap(nodeMap);   
  91.     }   
  92.     public Image[] getImages()   
  93.     {   
  94.         return this.star;   
  95.     }   
  96.     public Image[] getFixImage()   
  97.     {   
  98.         return statFix;   
  99.     }   
  100.     class PanelListener extends MouseAdapter   
  101.     {   
  102.            
  103.         public void mouseClicked(MouseEvent e)   
  104.         {   
  105.             SearchNode searchnode=new SearchNode(nodeMap);   
  106.             System.out.println("clicked");   
  107.             int x=e.getX()/nodeWidth;   
  108.             int y=e.getY()/nodeHeight;   
  109.             Node target=nodeMap[y][x];   
  110.             if(null==target.getImage())   
  111.                 return;   
  112.             if(null==preNode)   
  113.             {   
  114.                 preNode=target;   
  115.                 _nodemap.PressNode(target);   
  116.                 updateUI();   
  117.                 return;   
  118.             }   
  119.             if(preNode==target)   
  120.                 return;   
  121.             _nodemap.PressNode(target);   
  122.             if(preNode.getImage()==target.getImage())   
  123.             {   
  124.                    
  125.                 if(true==searchnode.Search(preNode, target))   
  126.                 {   
  127.                     DrawLine(target);   
  128.                     searchnode.clean();   
  129.                     preNode=null;   
  130.                     updateUI();   
  131.                        
  132.                 }   
  133.                 else  
  134.                 {   
  135.                     preNode.NotPress();   
  136.                     preNode=target;   
  137.                     searchnode.clean();   
  138.                 }   
  139.             }   
  140.             else  
  141.                 preNode=target;   
  142.             updateUI();   
  143.         }   
  144.     }   
  145.     private void DrawLine(Node startNode)   
  146.     {   
  147.         myGraphics=this.getGraphics();   
  148.         int[][] Del={{0,-1},{0,1},{-1,0},{1,0}};   
  149.         Node preNode=null;   
  150.         for(int i=0; i<Del.length; i++)   
  151.         {   
  152.             int x=startNode.getX()+Del[i][0];   
  153.             int y=startNode.getY()+Del[i][1];   
  154.             if(x<0 || x>=nodeMap[0].length ||   
  155.                     y<0 || y>=nodeMap.length)   
  156.                 continue;   
  157.             if(null!=nodeMap[y][x].getPreNode())   
  158.             {   
  159.                 preNode=nodeMap[y][x];   
  160.                 break;   
  161.             }   
  162.         }   
  163.         if(null==preNode)   
  164.             preNode=this.preNode;   
  165.         myGraphics.drawLine(startNode.getX()*nodeWidth + (nodeWidth)/2,   
  166.                 startNode.getY()*nodeHeight + (nodeHeight)/2,   
  167.                 preNode.getX()*nodeWidth +(nodeWidth)/2,   
  168.                 preNode.getY()*nodeHeight+(nodeHeight)/2);   
  169.         ((Graphics2D)myGraphics).setBackground(Color.red);   
  170.         myGraphics.drawLine(20,20,500,500);   
  171.         paintComponent(myGraphics);   
  172.         DrawLineFromPoint2Point(preNode);   
  173.     }   
  174.     private void DrawLineFromPoint2Point(Node node)   
  175.     {   
  176.         Node preNode=node.getPreNode();   
  177.         if(null==preNode)   
  178.             return;   
  179.         myGraphics.drawLine(node.getX()+(nodeWidth)/2,   
  180.                 node.getY()+(nodeHeight)/2,   
  181.                 preNode.getX()+(nodeWidth)/2,   
  182.                 preNode.getY()+(nodeHeight)/2);   
  183.         DrawLineFromPoint2Point(preNode);   
  184.     }   
  185.     public void paintComponent(Graphics g)   
  186.     {   
  187.         super.paintComponent(g);   
  188.         myGraphics=g;   
  189.         nodeWidth=this.getWidth()/columnNum;   
  190.         nodeHeight=this.getHeight()/rowNum;   
  191.         Node targetNode;   
  192.         for(int i=0; i<nodeMap[0].length; i++)   
  193.         {   
  194.             for(int j=0; j<nodeMap.length; j++)   
  195.             {   
  196.                 targetNode=nodeMap[j][i];   
  197.                    
  198.                 if(null==targetNode.getImage())   
  199.                     continue;   
  200.                 int test=targetNode.getImage().getWidth(this);   
  201.                 g.drawImage(targetNode.getImage(), targetNode.getX()*nodeWidth, targetNode.getY()*nodeHeight,   
  202.                         (targetNode.getX()+1)*nodeWidth, (targetNode.getY()+1)*nodeHeight,   
  203.                         00, targetNode.getImage().getWidth(this), targetNode.getImage().getHeight(this), this);   
  204.                    
  205.                    
  206.             }   
  207.         }   
  208.     }   
  209.        
  210. }   
  211.   

 

 

FixPic.java

Code:
  1. import java.awt.Graphics2D;   
  2. import java.awt.Image;   
  3. import java.awt.image.*;   
  4.   
  5.   
  6.   
  7. class FixPic {   
  8.     BufferedImage[] buffRawPic;   
  9.     BufferedImage[] buffFixPic;   
  10.     public FixPic(Image[] rawPic)   
  11.     {   
  12.         Init(rawPic);   
  13.     }   
  14.     public  BufferedImage[] getFixPic()   
  15.     {   
  16.            
  17.            
  18.         short[] brighten=LightBrighten();   
  19.         for(int i=0; i<buffRawPic.length; i++)   
  20.         {   
  21.             if(null==buffRawPic[i])   
  22.                 continue;   
  23.             LookupTable lut=new ShortLookupTable(0,brighten);   
  24.             LookupOp lop=new LookupOp(lut,null);   
  25.             lop.filter(buffFixPic[i], buffRawPic[i]);   
  26.         }   
  27.         return buffRawPic;   
  28.     }   
  29.     private void Init(Image[] rawPic)   
  30.     {   
  31.         buffRawPic=new BufferedImage[rawPic.length];   
  32.         buffFixPic=new BufferedImage[rawPic.length];   
  33.         for(int i=0; i<buffRawPic.length; i++)   
  34.         {      
  35.             if(rawPic[i]==null)   
  36.             {   
  37.                 buffRawPic[i]=null;   
  38.                 buffFixPic[i]=null;   
  39.                 continue;   
  40.             }   
  41.             buffRawPic[i]=new BufferedImage(rawPic[i].getWidth(null),   
  42.                     rawPic[i].getHeight(null),BufferedImage.TYPE_INT_ARGB);   
  43.             Graphics2D g2D=buffRawPic[i].createGraphics();   
  44.             g2D.drawImage(rawPic[i],0,0,null);   
  45.             buffFixPic[i]=new BufferedImage(rawPic[i].getWidth(null),   
  46.                     rawPic[i].getHeight(null),BufferedImage.TYPE_INT_ARGB);   
  47.         }   
  48.     }   
  49.     private short[] LightBrighten()   
  50.     {   
  51.         short[] brighten=new short[256];   
  52.         short pixelValue;   
  53.         for(int i=0; i<256; i++)   
  54.         {   
  55.             pixelValue=(short)(i+10);   
  56.             if(pixelValue>255)   
  57.                 pixelValue=255;   
  58.             else  
  59.                 if(pixelValue<0)   
  60.                     pixelValue=0;   
  61.             brighten[i]=pixelValue;   
  62.         }   
  63.         return brighten;   
  64.            
  65.     }   
  66.   
  67. }   

Node.java

Code:
  1. import java.awt.Image;   
  2.   
  3. class Node    
  4. {   
  5.     private int X,Y;   
  6.     private Image nodeImage;   
  7.     private Image pressImage;   
  8.     private Node preNode=null;   
  9.     private int mark;   
  10.     public boolean isPress=false;   
  11.     public Node(int x, int y, Image m, Image pm)   
  12.     {   
  13.         this.X=x;   
  14.         this.Y=y;   
  15.         this.nodeImage=m;   
  16.         this.pressImage=pm;   
  17.         if(null==m)   
  18.             this.setMark(0);   
  19.         else  
  20.             this.setMark(1);   
  21.     }   
  22.     public void setPreNode(Node n)   
  23.     {   
  24.         this.preNode=n;   
  25.     }   
  26.     public Node getPreNode()   
  27.     {   
  28.         return this.preNode;   
  29.     }   
  30.     public int getX()   
  31.     {   
  32.         return this.X;   
  33.     }   
  34.     public int getY()   
  35.     {   
  36.         return this.Y;   
  37.     }   
  38.     public Image getImage()   
  39.     {   
  40.         if(false==this.isPress)   
  41.             return this.nodeImage;   
  42.         return this.pressImage;   
  43.     }   
  44.     public Image getRawImage()   
  45.     {   
  46.         return this.nodeImage;   
  47.     }   
  48.     public void setImage(Image m)   
  49.     {   
  50.         this.nodeImage=m;   
  51.     }   
  52.     public void Press()   
  53.     {   
  54.         this.isPress=true;   
  55.     }   
  56.     public void NotPress()   
  57.     {   
  58.         this.isPress=false;   
  59.     }   
  60.     public int getMark()   
  61.     {   
  62.         return this.mark;   
  63.     }   
  64.     public void setMark(int m)   
  65.     {   
  66.         this.mark=m;   
  67.     }   
  68. }   
  69.   
  70. class SearchNode   
  71. {   
  72.     private Node[][] nodeMap;   
  73.     public SearchNode(Node[][] nodes)   
  74.     {   
  75.         this.nodeMap=nodes;   
  76.     }   
  77.     public boolean Search(Node firstNode, Node secondNode)   
  78.     {   
  79.         firstNode.setMark(-1);   
  80.         for(int i=1; i<=3; i++)   
  81.         {   
  82.             for(int x=0; x<nodeMap[0].length; x++)   
  83.                 for(int y=0; y<nodeMap.length; y++)   
  84.                 {   
  85.                     if(-i==nodeMap[y][x].getMark())   
  86.                         if(true==SignMark(nodeMap[y][x],secondNode))   
  87.                             {   
  88.                                 firstNode.NotPress();   
  89.                                 secondNode.NotPress();   
  90.                                 firstNode.setImage(null);   
  91.                                 secondNode.setImage(null);   
  92.                                 //this.clean();   
  93.                                 return true;   
  94.                             }   
  95.                            
  96.                 }   
  97.         }   
  98.         //this.clean();   
  99.         return false;   
  100.     }   
  101.     public void clean()   
  102.     {   
  103.         for(int i=0; i<nodeMap[0].length; i++)   
  104.         {   
  105.             for(int j=0; j<nodeMap.length; j++)   
  106.             {   
  107.                 if(null==nodeMap[j][i].getImage() || nodeMap[j][i].getMark()<0)   
  108.                 {   
  109.                     nodeMap[j][i].setPreNode(null);   
  110.                     nodeMap[j][i].setMark(0);   
  111.                 }   
  112.             }   
  113.         }   
  114.     }   
  115.     private boolean SignMark(Node rawNode, Node stopNode)   
  116.     {   
  117.         int mark=rawNode.getMark()-1;   
  118.         int raw_x=rawNode.getX();   
  119.         int raw_y=rawNode.getY();   
  120.         int del[][]=new int[][]{{-1,0},{1,0},{0,-1},{0,1}};   
  121.            
  122.         for(int k=0; k<del.length; k++)   
  123.         {   
  124.             int idx_x=raw_x,idx_y=raw_y;   
  125.             while(true)   
  126.             {   
  127.                 idx_x+=del[k][0];   
  128.                 idx_y+=del[k][1];   
  129.                 if((idx_x<0 || idx_y<0 || idx_y>=nodeMap.length || idx_x>=nodeMap[0].length))   
  130.                     break;//超出范围了   
  131.                 if(nodeMap[idx_y][idx_x].getMark()<0 )   
  132.                     break;//已经标记过了   
  133.                 if(0==nodeMap[idx_y][idx_x].getMark())   
  134.                 {   
  135.                     nodeMap[idx_y][idx_x].setMark(mark);   
  136.                     nodeMap[idx_y][idx_x].setPreNode(rawNode);   
  137.                 }   
  138.                 if(stopNode==nodeMap[idx_y][idx_x])   
  139.                 {   
  140.                     return true;   
  141.                 }   
  142.                 if(nodeMap[idx_y][idx_x].getMark()>=1)   
  143.                     break;   
  144.             }   
  145.         }   
  146.         return false;   
  147.     }   
  148. }  

NodeMap.java

Code:
  1. import java.awt.Image;   
  2. import java.util.Random;   
  3.  class NodeMap {   
  4.      Node[][] nodeMap;   
  5.      public NodeMap(Node[][] nodeMap)   
  6.      {   
  7.          this.nodeMap=nodeMap;   
  8.      }   
  9.      public void PressNode(Node node)   
  10.      {   
  11.          for(int i=0; i<nodeMap.length; i++)   
  12.              for(int j=0; j<nodeMap[0].length; j++)   
  13.             {   
  14.                  if(null==nodeMap[i][j])   
  15.                      continue;   
  16.                  if(true==nodeMap[i][j].isPress &&   
  17.                      nodeMap[i][j].getRawImage()!=node.getRawImage())   
  18.                  {   
  19.                      nodeMap[i][j].NotPress();   
  20.                      node.Press();   
  21.                      return;   
  22.                  }   
  23.             }   
  24.          node.Press();   
  25.             
  26.      }   
  27.     public static Node[][] CreateNodeMap(int width, int height,   
  28.             Image[] images, Image[] pressImage)   
  29.     {   
  30.         Node[][] nodeMap=new Node[height][width];   
  31.         int[] listNum=CreateAddressSerial(width*height);   
  32.         int x,y;   
  33.         Image randImage;   
  34.         Image randPressImage;   
  35.         Random rand=new Random(System. currentTimeMillis());   
  36.         for(int i=0; i<listNum.length; i+=2)   
  37.         {   
  38.             int num_rand=rand.nextInt(images.length-1);   
  39.             randImage=images[num_rand];   
  40.             randPressImage=pressImage[num_rand];   
  41.             x=listNum[i]%width;   
  42.             y=listNum[i]/width;   
  43.             nodeMap[y][x]=new Node(x, y, randImage,randPressImage);   
  44.                
  45.             x=listNum[i+1]%width;   
  46.             y=listNum[i+1]/width;   
  47.             nodeMap[y][x]=new Node(x, y, randImage,randPressImage);   
  48.         }   
  49.         return nodeMap;   
  50.     }   
  51.     private static int[] CreateAddressSerial(int widthMulHeight)   
  52.     {   
  53.         RandList randlist=new RandList(widthMulHeight);   
  54.         return randlist.getRandNumList(widthMulHeight);   
  55.     }   
  56. }   

RandList.java

Code:
  1. import java.util.*;   
  2.   
  3.   
  4. public class RandList {   
  5.     private int[] sumList;   
  6.     public RandList(int sum)   
  7.     {   
  8.         sumList=new int[sum];   
  9.         for(int i=0;i<sum; i++)   
  10.         {   
  11.             sumList[i]=i;   
  12.         }   
  13.     }   
  14.     public int[] getRandNumList(int n)   
  15.     {   
  16.         Random rand=new Random(System. currentTimeMillis());   
  17.         int[] for_return=new int[n];   
  18.            
  19.         for(int i=0; i<n; i++)   
  20.         {   
  21.             int idx=sumList.length-i-1;   
  22.             if(0!=idx)   
  23.             {   
  24.                 int randNum=rand.nextInt(idx);   
  25.                 for_return[i]=sumList[randNum];   
  26.                 sumList[randNum]=sumList[idx];   
  27.             }   
  28.             else  
  29.                 for_return[sumList.length-1]=sumList[idx];   
  30.         }   
  31.         return for_return;   
  32.     }   
  33. }   

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩精神性尿频怎么办 小孩睡觉前尿频怎么办 生僻字打不出来怎么办 名字里有生僻字怎么办 qq语音红包生僻字怎么办 生僻字上不了户口怎么办 7岁儿童结巴怎么办 小孩咳喘有痰怎么办 都说我不爱说话怎么办 宝宝突然结巴了怎么办 淘宝客户不理人怎么办 遇到不说话客户怎么办 同学群没人聊怎么办 群里没人说话怎么办 儿童说话声音沙哑怎么办 孩子不和外人说话怎么办 同学退群该怎么办 宝宝睡觉枕头湿怎么办 一个多月的小宝宝便秘怎么办 小宝宝便秘拉屎困难怎么办 一岁小宝宝便秘怎么办 客户不听我说话怎么办 微信上客户不理怎么办 小宝宝母乳不够吃怎么办 小宝宝吃母乳拉肚子怎么办 母乳小宝宝吃奶吃不了怎么办 婴儿感冒吐奶怎么办 小孩感冒吐奶怎么办 小孩吐奶怎么办月子 新生儿一直吐奶怎么办 宝宝50天吐奶厉害怎么办 小儿吐奶厉害怎么办 宝宝一直便秘了怎么办 误建了微信群聊怎么办 新生儿大口吐奶怎么办 宝宝喝了就吐奶怎么办 婴儿顿顿吐奶怎么办 说话着急就结巴怎么办 幼儿舌头长泡怎么办 一着急说话结巴怎么办 幼儿舌头又溃疡怎么办