java仿windows纸牌游戏设计
openkk
12年前
下面是运行截图
主要用到的java api
HashMap, list , MouseListener ,MouseMoitionListener,其他就没什么了,主要是规则的实现
package com; import java.awt.Color; import java.awt.Container; import java.util.List; import java.awt.Point; import java.util.ArrayList; import java.util.HashMap; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class CardMain extends JFrame{ public Container container=null; HashMap<Point, Card> table =new HashMap<Point, Card>();//专门装桌面上的牌 List<Card> list = new ArrayList<Card>();//装未发的牌堆 List<Card> dragList=new ArrayList<Card>();//装当前正在移动的纸牌 这样设计是为了解决某个bug Card border1[]=new Card[6]; //第一排白框 Card border2[]=new Card[7];//第二排白框 JLabel card[]=new Card[52];//所有卡片 int min=10,max=20; public CardMain(){ Init();//初始化 SetMenu();//创建菜单 InitCards();//发牌 InitBorder();//设置边框 this.setVisible(true); } //得到随机数 public void getRondom(){ JLabel temp=null; for(int i=0;i<52;i++) { int a = (int) (Math.random() * 52); int b = (int) (Math.random() * 52); temp = card[a]; card[a] = card[b]; card[b] = temp; } } //发牌 public void InitCards(){ for(int i=0;i<4;i++) { for(int j=0;j<13;j++) { card[i*13+j]=new Card(this,(i+1)+"-"+(j+1),"front"); } } getRondom();//打乱顺序 //先发7堆,并且每一堆最上面一章是翻开的 int count=0; Point point=new Point(); for(int k=0;k<24;k++) { ((Card)card[count]).turnRear(); point=new Point(); point.x=600; point.y=30; card[count].setLocation(point); list.add((Card)card[count]); container.add(card[count]); count++; } for(int i=0;i<7;i++) { for(int j=i;j>=0;j--) { if(j<i) { ((Card)card[count]).turnRear(); }else { ((Card)card[count]).canmove=true; } point=new Point(); point.x=50+100*i; point.y=180+min*j; card[count].setLocation(point); table.put(point, ((Card)card[count])); container.add(card[count]); count++; } } } //画边框 public void InitBorder(){ //4个A for(int i=0;i<6;i++) { border1[i]=new Card(this); border1[i].setBorder(BorderFactory.createLineBorder(Color.white)); if(i<4) { border1[i].setBounds(50+100*i, 30,71,96); }else { border1[i].setBounds(600+(i-4)*100, 30,71,96); } container.add(border1[i]); } //7列 for(int i=0;i<7;i++) { border2[i]=new Card(this); border2[i].setBorder(BorderFactory.createLineBorder(Color.white)); border2[i].setBounds(50+100*i, 180,71,96); container.add(border2[i]); } } //初始化窗体 public void Init(){ this.setTitle("经典纸牌游戏---by 小柒"); this.setSize(850, 650); setResizable(false); setLocationRelativeTo(getOwner()); container=this.getContentPane(); container.setLayout(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); container.setBackground(new Color(0, 112, 26)); } //创建菜单 public void SetMenu(){ JMenuBar jMenuBar= new JMenuBar(); JMenu game = new JMenu("游戏"); JMenu help = new JMenu("帮助"); JMenuItem start = new JMenuItem("新游戏"); JMenuItem exit = new JMenuItem("退出"); JMenuItem about = new JMenuItem("关于"); game.add(start); game.add(exit); help.add(about); jMenuBar.add(game); jMenuBar.add(help); this.setJMenuBar(jMenuBar); } //开始游戏Main方法 public static void main(String []args){ new CardMain(); } }