⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zhipai.java

📁 类似于WINDOWS自带纸牌游戏的源码。
💻 JAVA
字号:
package src;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.Hashtable;
public class Zhipai extends JFrame{
   private Container pane=new Container();
   public Carder cards[]=new Carder[54];
   private JLabel clickLable=null;
   private int c=0;
   private int a=0;
   private int n=0;
   int d=0;
   private int finish =0;
   public Hashtable table =new Hashtable();
   public JLabel groundLabel[]=null;
   int columns[]=new int[12];
   int fuhao[]=new int[4];
   boolean isFirst=false;
   Carder card=null;
   int paidui[]=new int[25];
   
   public static void main(String[] args){
	   Zhipai zhipai=new Zhipai();
	   zhipai.setVisible(true);	   
   }
   
   //构造函数
   public 	Zhipai(){
	   setTitle("纸牌游戏");
	   setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
	   setSize(900,700);
	   
	   setJMenuBar(new MenuBar(this));
	   pane=this.getContentPane();
	   pane.setBackground(Color.white);
	   pane.setLayout(null);
       /*JLabel clickLabel=new JLabel();
       clickLabel.setBounds(20,25,71,96);
       clickLabel.setIcon(new ImageIcon("rear.gif"));
       pane.add(clickLabel);
       clickLabel.addMouseListener(new MouseAdapter (){
    	   public void mouseReleased(MouseEvent me){
    		   if(c>0){
    			   Zhipai.this.deal();
    		   }
    	   }
       });*/
       this.initCards();
       this.randomCards();
       this.setCardsLocation();
       
        groundLabel=new JLabel[13];
        groundLabel[12]=new JLabel();
    	groundLabel[12].setBorder(javax.swing.BorderFactory
    			.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
    	groundLabel[12].setBounds(20,25,71,96);
    	this.pane.add(groundLabel[12]);
        int x=20;
        for(int i=0;i<7;i++){
        	groundLabel[i]=new JLabel();
        	groundLabel[i].setBorder(javax.swing.BorderFactory
        			.createEtchedBorder

(javax.swing.border.EtchedBorder.RAISED));
        	groundLabel[i].setBounds(x,200,71,96);
        	x+=130;
        	this.pane.add(groundLabel[i]);     	
        }
        groundLabel[7]=new JLabel();
        groundLabel[7].setBorder(javax.swing.BorderFactory
     			.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
     	groundLabel[7].setBounds(110,25,71,96);
     	this.pane.add(groundLabel[7]);
        int y=300;
        for(int i=8;i<12;i++){
        	groundLabel[i]=new JLabel();
        	groundLabel[i].setBorder(javax.swing.BorderFactory
        			.createEtchedBorder

(javax.swing.border.EtchedBorder.RAISED));
        	groundLabel[i].setBounds(y,25,71,96);
        	y+=130;
        	this.pane.add(groundLabel[i]);       	
        } 
    	this.setVisible(true);
    	this.deal(); 
   }
   
   public int getC(){
	   return c;
   }
   public void initCards(){
	   if(cards[0] !=null){
		   for(int i=0;i<=53;i++){
			   pane.remove(cards[i]);
		   }
	   }
	  for(int i=0;i<=53;i++){
		  cards[i]=new Carder(i,this,pane);
	  }   
	  this.randomCards();
   }
   public void randomCards(){
	   //随即生成牌号
	   int c=1;
	   for(int i=0;i<52;i++){
		   int a=(int)(Math.random()*52);
		   int b=(int)(Math.random()*52);
		   cards[52].value=cards[a].value;
		   cards[a].value=cards[b].value;
		   cards[b].value=cards[52].value;
	   }
	   for(int i=0;i<=52;i++){
			  cards[i]=new Carder(cards[i].value,this,pane);
			  }
   }
   //放置纸牌的位置
   public void setCardsLocation(){
	   c=0;
	   finish=0;
	   n=0;
	   int x=883;
	   int y=600;
	   pane.add(cards[53]);
	   cards[53].turnRear();
	   cards[53].moveto(new Point(20,25));
	   table.put(new Point(20,25), cards[53]);

	   int d=220;
	   for (int i=2;i>=1;i--){
		   pane.add(cards[i]);
		   if(i==2) {cards[i].turnFront();
		   cards[i].isFirst=true;}
		   else {cards[i].turnRear();}
		   cards[i].moveto(new Point(150,d));
		   table.put(new Point(150,d), cards[i]);
		   d-=20;
	   }
	   d=240;
	   for (int i=5;i>=3;i--){
		   pane.add(cards[i]);
		   if(i==5) {cards[i].turnFront();
		   cards[i].isFirst=true;}
		   else {cards[i].turnRear();}
		   cards[i].moveto(new Point(280,d));
		   table.put(new Point(280,d), cards[i]);
		   d-=20;
	   }
	   d=260;
	   for (int i=9;i>=6;i--){
		   pane.add(cards[i]);
		   if(i==9) {cards[i].turnFront();
		   cards[i].isFirst=true;}
		   else {cards[i].turnRear();}
		   cards[i].moveto(new Point(410,d));
		   table.put(new Point(410,d), cards[i]);
		   d-=20;
	   }
	   d=280;
	   for (int i=14;i>=10;i--){
		   pane.add(cards[i]);
		   if(i==14) {cards[i].turnFront();
		   cards[i].isFirst=true;}
		   else {cards[i].turnRear();}
		   cards[i].moveto(new Point(540,d));
		   table.put(new Point(540,d), cards[i]);
		   d-=20;
	   }
	   d=300;
	   for (int i=20;i>=15;i--){
		   pane.add(cards[i]);
		   if(i==20) {cards[i].turnFront();
		   cards[i].isFirst=true;}
		   else {cards[i].turnRear();}
		   cards[i].moveto(new Point(670,d));
		   table.put(new Point(670,d), cards[i]);
		   d-=20;
	   }
	   d=320;
	   for (int i=27;i>=21;i--){
		   pane.add(cards[i]);
		   if(i==27) {cards[i].turnFront();
		   cards[i].isFirst=true;}
		   else {cards[i].turnRear();}
		   cards[i].moveto(new Point(800,d));
		   table.put(new Point(800,d), cards[i]);
		   d-=20;
	   }
	   pane.add(cards[0]);
	   cards[0].turnFront();
	   cards[0].moveto(new Point(20,200));
	   table.put(new Point(20,200), cards[0]);
	   for(int i=0;i<=6;i++){
		   columns[i]=i+1;
	   }
	   columns[7]=24;
	   for(int i=8;i<12;i++){
		columns[i]=0;   
	   }
	   for(int i=0;i<25;i++){
		   paidui[i]=1;
	   }
	   int first=0;
	   int one=1;
	   int m=20;
	   int n=200;
	                                                                                     

                                                                                             

                                                                   
   }
   
   public void setNA(){
	   a=0;
	   n=0;
   }
   public void deal(){
	   this.setNA();
	   for(int i=0;i<10;i++){

	   }
   }
   //获得card上面的那张牌
   public Carder getPreviousCard(Carder card){
	   Point point =new Point(card.getLocation());
	   point.y-=20;
	   card =(Carder) table.get(point);
	   if(card !=null){
		   return card;
	   }
	   else{
	return null;
	   }
   }
   public Carder getNextCard(Carder card){
	   Carder card1=new Carder(1,card.main,pane);
	   Point point =new Point(card.getLocation());
	   point.y+=20;
	   card1 =(Carder) table.get(point);
	   if(card1 !=null){
		   return card1;
	   }
	   /*point.y+=15;
	   card=(Carder) table.get(point);*/
	   else {return null;}
   }
   public Point getLastCardLocation(int column){
	   Point point =new Point(20+column*130,200);
	   Carder card=(Carder) this.table.get(point);
	   if(card ==null) return null;
	   while(card !=null){
		   point =card.getLocation();
		   card =this.getNextCard(card);
	   }
	   return point;
   }
   public Point Available(int column){
	   Point point =new Point(20+column*130,200+columns[column]*20);
	   Point point2=new Point(300+(column-8)*130, (25+columns[column]*1));
	   Point point1=new Point();
	   point1.x=point.x;
	   point1.y=point.y-20;
	   Carder card=(Carder) this.table.get(point1);
	   if( column<7){
		   return point;
	   }
	   else if(column>7){
		   return point2;
	   }
	   else if(column==7){
		   point1.x=110;
		   point1.y=25;
		   return point1;
	   }
	   else {
		   return null;}
	   }
   public Carder getFirstCard(Carder card){
	   Point point =new Point(card.getLocation());
	   Point point1 =new Point();
	   point1=point;
	   Carder card1 =(Carder) table.get(point);
	   while(card1 !=null){
		   point1.y=point1.y+20;
		   card1 =(Carder) table.get(point);
	   }
	   point1.y=point1.y-20;
	   card=(Carder) table.get(point);
	   return card1;
   }
   public boolean isFirstCard(Carder card){
	   Point point=new Point(card.getLocation());
	   int m=(point.x-20)/130;
	   if(point.y==200+columns[m]*20-20){
		   isFirst=true;
		   return isFirst;
	   }
	   else {return false;}
   }
   public Point getFirstCardLocation(int column){
	   if(n<7){
	   Point point =new Point(20+column*130,200+columns[column]*20-20);
	   Carder card=(Carder) this.table.get(point);
	   if(card ==null) return null;
	   while(card !=null){
		   point =card.getLocation();
		   card =this.getNextCard(card);
	   }
	   return point;
	   }
	   else if(n>7){
		 Point point1=new Point(300+(column-8)*130,25+columns[column]*1-1);
		 return point1;
	   }
	   else {return null;}
   }
   public Point getGroundLabelLocation(int column){
	   return new Point (groundLabel[column].getLocation());
   }
   public void setGroundLabelZOrder(){
	   for(int i=0;i<13;i++){
		   pane.setComponentZOrder(groundLabel[i], 130+i);
	   } 
   }

public void columns(int m){
	columns[m]--;
}
   
   
   
   
   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -