Java连连看代码---学java时练手的,凑合看吧。
来源:互联网 发布:曼谷酒店知乎 编辑:程序博客网 时间:2024/05/06 20:12
MainFrame.java
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.File;
- public class MainFrame {
- public static void main(String[] argv)
- {
- int a=4;
- a=5;
- new Frames();
- }
- }
- class Frames extends JFrame
- {
- final int rowNum=10;
- final int columnNum=10;
- public Frames()
- {
- String dirPath=System.getProperty("user.dir");
- dirPath+="//NodePic";
- DrawPanel dPanel=new DrawPanel(dirPath, rowNum, columnNum);
- dPanel.setNodeMap(NodeMap.CreateNodeMap(columnNum, rowNum, dPanel.getImages(),dPanel.getFixImage()));
- this.getContentPane().add(dPanel);
- this.setSize(600,600);
- this.setTitle("连连看");
- this.setVisible(true);
- this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
- }
- }
- class DrawPanel extends JPanel
- {
- Node preNode;
- int nodeWidth;
- int nodeHeight;
- private MediaTracker mt ;
- private Image star[];
- private Image[] statFix;
- private File fileDir;
- private int rowNum,columnNum;
- private final int Pic_Num=10;
- private final int Node_Num=20;
- private Node[][] nodeMap;
- private NodeMap _nodemap;
- private Graphics myGraphics;
- public DrawPanel(String dirPath, int row, int column)
- {
- super();
- rowNum=row;
- columnNum=column;
- mt= new MediaTracker(this);
- fileDir=new File(dirPath);
- File list[]=fileDir.listFiles();
- RandList randlist=new RandList(list.length);
- int[] randNumList=randlist.getRandNumList(Pic_Num);
- star=new Image[randNumList.length+5];//其中三个为null,就是为了不给位图
- for(int i=0; i<randNumList.length; i++)
- {
- if(!list[randNumList[i]].isFile())
- {
- System.out.println("NodePic ERROR");
- return ;
- }
- star[i]= Toolkit.getDefaultToolkit().createImage(list[randNumList[i]].getPath());
- mt.addImage(star[i], i);
- }
- try {
- mt.waitForAll();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- statFix=(Image[])(new FixPic(star)).getFixPic();
- this.addMouseListener(new PanelListener());
- }
- public void setNodeMap(Node[][] map)
- {
- this.nodeMap=map;
- _nodemap=new NodeMap(nodeMap);
- }
- public Image[] getImages()
- {
- return this.star;
- }
- public Image[] getFixImage()
- {
- return statFix;
- }
- class PanelListener extends MouseAdapter
- {
- public void mouseClicked(MouseEvent e)
- {
- SearchNode searchnode=new SearchNode(nodeMap);
- System.out.println("clicked");
- int x=e.getX()/nodeWidth;
- int y=e.getY()/nodeHeight;
- Node target=nodeMap[y][x];
- if(null==target.getImage())
- return;
- if(null==preNode)
- {
- preNode=target;
- _nodemap.PressNode(target);
- updateUI();
- return;
- }
- if(preNode==target)
- return;
- _nodemap.PressNode(target);
- if(preNode.getImage()==target.getImage())
- {
- if(true==searchnode.Search(preNode, target))
- {
- DrawLine(target);
- searchnode.clean();
- preNode=null;
- updateUI();
- }
- else
- {
- preNode.NotPress();
- preNode=target;
- searchnode.clean();
- }
- }
- else
- preNode=target;
- updateUI();
- }
- }
- private void DrawLine(Node startNode)
- {
- myGraphics=this.getGraphics();
- int[][] Del={{0,-1},{0,1},{-1,0},{1,0}};
- Node preNode=null;
- for(int i=0; i<Del.length; i++)
- {
- int x=startNode.getX()+Del[i][0];
- int y=startNode.getY()+Del[i][1];
- if(x<0 || x>=nodeMap[0].length ||
- y<0 || y>=nodeMap.length)
- continue;
- if(null!=nodeMap[y][x].getPreNode())
- {
- preNode=nodeMap[y][x];
- break;
- }
- }
- if(null==preNode)
- preNode=this.preNode;
- myGraphics.drawLine(startNode.getX()*nodeWidth + (nodeWidth)/2,
- startNode.getY()*nodeHeight + (nodeHeight)/2,
- preNode.getX()*nodeWidth +(nodeWidth)/2,
- preNode.getY()*nodeHeight+(nodeHeight)/2);
- ((Graphics2D)myGraphics).setBackground(Color.red);
- myGraphics.drawLine(20,20,500,500);
- paintComponent(myGraphics);
- DrawLineFromPoint2Point(preNode);
- }
- private void DrawLineFromPoint2Point(Node node)
- {
- Node preNode=node.getPreNode();
- if(null==preNode)
- return;
- myGraphics.drawLine(node.getX()+(nodeWidth)/2,
- node.getY()+(nodeHeight)/2,
- preNode.getX()+(nodeWidth)/2,
- preNode.getY()+(nodeHeight)/2);
- DrawLineFromPoint2Point(preNode);
- }
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- myGraphics=g;
- nodeWidth=this.getWidth()/columnNum;
- nodeHeight=this.getHeight()/rowNum;
- Node targetNode;
- for(int i=0; i<nodeMap[0].length; i++)
- {
- for(int j=0; j<nodeMap.length; j++)
- {
- targetNode=nodeMap[j][i];
- if(null==targetNode.getImage())
- continue;
- int test=targetNode.getImage().getWidth(this);
- g.drawImage(targetNode.getImage(), targetNode.getX()*nodeWidth, targetNode.getY()*nodeHeight,
- (targetNode.getX()+1)*nodeWidth, (targetNode.getY()+1)*nodeHeight,
- 0, 0, targetNode.getImage().getWidth(this), targetNode.getImage().getHeight(this), this);
- }
- }
- }
- }
FixPic.java
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.image.*;
- class FixPic {
- BufferedImage[] buffRawPic;
- BufferedImage[] buffFixPic;
- public FixPic(Image[] rawPic)
- {
- Init(rawPic);
- }
- public BufferedImage[] getFixPic()
- {
- short[] brighten=LightBrighten();
- for(int i=0; i<buffRawPic.length; i++)
- {
- if(null==buffRawPic[i])
- continue;
- LookupTable lut=new ShortLookupTable(0,brighten);
- LookupOp lop=new LookupOp(lut,null);
- lop.filter(buffFixPic[i], buffRawPic[i]);
- }
- return buffRawPic;
- }
- private void Init(Image[] rawPic)
- {
- buffRawPic=new BufferedImage[rawPic.length];
- buffFixPic=new BufferedImage[rawPic.length];
- for(int i=0; i<buffRawPic.length; i++)
- {
- if(rawPic[i]==null)
- {
- buffRawPic[i]=null;
- buffFixPic[i]=null;
- continue;
- }
- buffRawPic[i]=new BufferedImage(rawPic[i].getWidth(null),
- rawPic[i].getHeight(null),BufferedImage.TYPE_INT_ARGB);
- Graphics2D g2D=buffRawPic[i].createGraphics();
- g2D.drawImage(rawPic[i],0,0,null);
- buffFixPic[i]=new BufferedImage(rawPic[i].getWidth(null),
- rawPic[i].getHeight(null),BufferedImage.TYPE_INT_ARGB);
- }
- }
- private short[] LightBrighten()
- {
- short[] brighten=new short[256];
- short pixelValue;
- for(int i=0; i<256; i++)
- {
- pixelValue=(short)(i+10);
- if(pixelValue>255)
- pixelValue=255;
- else
- if(pixelValue<0)
- pixelValue=0;
- brighten[i]=pixelValue;
- }
- return brighten;
- }
- }
Node.java
- import java.awt.Image;
- class Node
- {
- private int X,Y;
- private Image nodeImage;
- private Image pressImage;
- private Node preNode=null;
- private int mark;
- public boolean isPress=false;
- public Node(int x, int y, Image m, Image pm)
- {
- this.X=x;
- this.Y=y;
- this.nodeImage=m;
- this.pressImage=pm;
- if(null==m)
- this.setMark(0);
- else
- this.setMark(1);
- }
- public void setPreNode(Node n)
- {
- this.preNode=n;
- }
- public Node getPreNode()
- {
- return this.preNode;
- }
- public int getX()
- {
- return this.X;
- }
- public int getY()
- {
- return this.Y;
- }
- public Image getImage()
- {
- if(false==this.isPress)
- return this.nodeImage;
- return this.pressImage;
- }
- public Image getRawImage()
- {
- return this.nodeImage;
- }
- public void setImage(Image m)
- {
- this.nodeImage=m;
- }
- public void Press()
- {
- this.isPress=true;
- }
- public void NotPress()
- {
- this.isPress=false;
- }
- public int getMark()
- {
- return this.mark;
- }
- public void setMark(int m)
- {
- this.mark=m;
- }
- }
- class SearchNode
- {
- private Node[][] nodeMap;
- public SearchNode(Node[][] nodes)
- {
- this.nodeMap=nodes;
- }
- public boolean Search(Node firstNode, Node secondNode)
- {
- firstNode.setMark(-1);
- for(int i=1; i<=3; i++)
- {
- for(int x=0; x<nodeMap[0].length; x++)
- for(int y=0; y<nodeMap.length; y++)
- {
- if(-i==nodeMap[y][x].getMark())
- if(true==SignMark(nodeMap[y][x],secondNode))
- {
- firstNode.NotPress();
- secondNode.NotPress();
- firstNode.setImage(null);
- secondNode.setImage(null);
- //this.clean();
- return true;
- }
- }
- }
- //this.clean();
- return false;
- }
- public void clean()
- {
- for(int i=0; i<nodeMap[0].length; i++)
- {
- for(int j=0; j<nodeMap.length; j++)
- {
- if(null==nodeMap[j][i].getImage() || nodeMap[j][i].getMark()<0)
- {
- nodeMap[j][i].setPreNode(null);
- nodeMap[j][i].setMark(0);
- }
- }
- }
- }
- private boolean SignMark(Node rawNode, Node stopNode)
- {
- int mark=rawNode.getMark()-1;
- int raw_x=rawNode.getX();
- int raw_y=rawNode.getY();
- int del[][]=new int[][]{{-1,0},{1,0},{0,-1},{0,1}};
- for(int k=0; k<del.length; k++)
- {
- int idx_x=raw_x,idx_y=raw_y;
- while(true)
- {
- idx_x+=del[k][0];
- idx_y+=del[k][1];
- if((idx_x<0 || idx_y<0 || idx_y>=nodeMap.length || idx_x>=nodeMap[0].length))
- break;//超出范围了
- if(nodeMap[idx_y][idx_x].getMark()<0 )
- break;//已经标记过了
- if(0==nodeMap[idx_y][idx_x].getMark())
- {
- nodeMap[idx_y][idx_x].setMark(mark);
- nodeMap[idx_y][idx_x].setPreNode(rawNode);
- }
- if(stopNode==nodeMap[idx_y][idx_x])
- {
- return true;
- }
- if(nodeMap[idx_y][idx_x].getMark()>=1)
- break;
- }
- }
- return false;
- }
- }
NodeMap.java
- import java.awt.Image;
- import java.util.Random;
- class NodeMap {
- Node[][] nodeMap;
- public NodeMap(Node[][] nodeMap)
- {
- this.nodeMap=nodeMap;
- }
- public void PressNode(Node node)
- {
- for(int i=0; i<nodeMap.length; i++)
- for(int j=0; j<nodeMap[0].length; j++)
- {
- if(null==nodeMap[i][j])
- continue;
- if(true==nodeMap[i][j].isPress &&
- nodeMap[i][j].getRawImage()!=node.getRawImage())
- {
- nodeMap[i][j].NotPress();
- node.Press();
- return;
- }
- }
- node.Press();
- }
- public static Node[][] CreateNodeMap(int width, int height,
- Image[] images, Image[] pressImage)
- {
- Node[][] nodeMap=new Node[height][width];
- int[] listNum=CreateAddressSerial(width*height);
- int x,y;
- Image randImage;
- Image randPressImage;
- Random rand=new Random(System. currentTimeMillis());
- for(int i=0; i<listNum.length; i+=2)
- {
- int num_rand=rand.nextInt(images.length-1);
- randImage=images[num_rand];
- randPressImage=pressImage[num_rand];
- x=listNum[i]%width;
- y=listNum[i]/width;
- nodeMap[y][x]=new Node(x, y, randImage,randPressImage);
- x=listNum[i+1]%width;
- y=listNum[i+1]/width;
- nodeMap[y][x]=new Node(x, y, randImage,randPressImage);
- }
- return nodeMap;
- }
- private static int[] CreateAddressSerial(int widthMulHeight)
- {
- RandList randlist=new RandList(widthMulHeight);
- return randlist.getRandNumList(widthMulHeight);
- }
- }
RandList.java
- import java.util.*;
- public class RandList {
- private int[] sumList;
- public RandList(int sum)
- {
- sumList=new int[sum];
- for(int i=0;i<sum; i++)
- {
- sumList[i]=i;
- }
- }
- public int[] getRandNumList(int n)
- {
- Random rand=new Random(System. currentTimeMillis());
- int[] for_return=new int[n];
- for(int i=0; i<n; i++)
- {
- int idx=sumList.length-i-1;
- if(0!=idx)
- {
- int randNum=rand.nextInt(idx);
- for_return[i]=sumList[randNum];
- sumList[randNum]=sumList[idx];
- }
- else
- for_return[sumList.length-1]=sumList[idx];
- }
- return for_return;
- }
- }
- Java连连看代码---学java时练手的,凑合看吧。
- java连连看代码
- java连连看代码
- 连连看java代码(2)
- 连连看设计思路与java代码
- JAVA实现连连看
- java连连看applet
- Java版连连看
- java连连看源代码
- 连连看java源代码
- 连连看java
- JAVA写的"连连看",一起学习!
- java 实现 连连看的算法
- 连连看 代码的实现
- hdu 1175 连连看<java>
- 学java的必看!
- 初学JAVA,用JAVA编写一个简单的连连看~
- 外挂练手之“QQ连连看助手”
- 四色原理
- 面向对象程序设计中类之间的关系
- 我的24点算法
- cygwin下不能退出Emacs的解决办法
- NIO学习笔记
- Java连连看代码---学java时练手的,凑合看吧。
- 哪位老师或同学给解答下,谢谢
- 一个草根关于OO的一点理解
- 自信——随感
- 创建Table
- QQ群78928780记录整理:90521-精华
- QQ群78928780记录整理心得
- (转载)解析.Net框架下的XML编程技术
- 让心引领--大话猎头(3)