展开菜单
首页 精品内容 本月促销 装机必备 Windows PDF教程 macOS软件 IOS软件 Android AI 专题
全部分类

当前位置:

首页 > 编程开发 > 基于Java怎么实现经典蜘蛛纸牌游戏

基于Java怎么实现经典蜘蛛纸牌游戏

效果展示前面的导入过程这里就不多说了,不会的可以自己去问度娘。导入后,选择Spider.java类直接运行就可以了,下面是游戏运行的截图:游戏结构核心代码AboutDialog.java类importjavax.swing.*;importjava.awt.*;/***“关于”窗口*/publicclassAboutDialogextendsJDialog{JPaneljMainPane=newJPanel();JTabbedPanejTabbedPane=newJTabbedPane();private

本文目录

    效果展示

    前面的导入过程这里就不多说了,不会的可以自己去问度娘。导入后,选择Spider.java类直接运行就可以了,下面是游戏运行的截图:

    基于Java怎么实现经典蜘蛛纸牌游戏

    游戏结构

    基于Java怎么实现经典蜘蛛纸牌游戏

    核心代码

    AboutDialog.java类

    import javax.swing.*;
    import java.awt.*;
    
    
    /*
     **“关于”窗口
     */
    public class AboutDialog extends JDialog
    {
    	JPanel jMainPane = new JPanel();
    
    	JTabbedPane jTabbedPane = new JTabbedPane();
    	private JPanel jPanel1 = new JPanel();
    	private JPanel jPanel2 = new JPanel();
    
    	private JTextArea jt1 = new JTextArea("将电脑多次分发给你的牌按照相同的花色由大至小排列起来。直到桌面上的牌全都消失。"); 
    	private JTextArea jt2 = new JTextArea("该游戏中,纸牌的图片来自于Windows XP的纸牌游戏,图片权属于原作者所有!"); 
    
    	/*
    	 **构造函数
    	 */
    	public AboutDialog()
    	{
    		setTitle("蜘蛛牌");
    		setSize(300,200);
    		setResizable(false);
    		setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE); 
    		
    		Container c = this.getContentPane();
    		
    		jt1.setSize(260,200);
    		jt2.setSize(260,200);
    		
    		jt1.setEditable(false);
    		jt2.setEditable(false);
    		
    		jt1.setLineWrap(true); 
    		jt2.setLineWrap(true); 
    
    		jt1.setFont(new Font("楷体_GB2312", java.awt.Font.BOLD, 13));
    		jt1.setForeground(Color.blue);
    
    		jt2.setFont(new Font("楷体_GB2312", java.awt.Font.BOLD, 13));
    		jt2.setForeground(Color.black);
    		
    		jPanel1.add(jt1);
    		jPanel2.add(jt2);
    		
    		jTabbedPane.setSize(300,200);
    		jTabbedPane.addTab("游戏规则", null, jPanel1, null);
    		jTabbedPane.addTab("声明", null, jPanel2, null);
    		
    		jMainPane.add(jTabbedPane);
    		c.add(jMainPane);
    
    		pack();
    		this.setVisible(true);
    	}
    }

    PKCard.java类

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class PKCard extends JLabel implements MouseListener,
            MouseMotionListener{
    
        //纸牌的位置
    	Point point = null;
        Point initPoint = null;
        
    	int value = 0;
        int type = 0;
        
    	String name = null;
        Container pane = null;
        
    	Spider main = null;
        
    	boolean canMove = false;
        boolean isFront = false;
        PKCard previousCard = null;
    
        public void mouseClicked(MouseEvent arg0){
    	}
        
        public void flashCard(PKCard card){
    		//启动Flash线程
    		new Flash(card).start();
    		//不停的获得下一张牌,直到完成
    		if(main.getNextCard(card) != null){
    			card.flashCard(main.getNextCard(card));
    		}
    	}
    
        class Flash extends Thread{
    		private PKCard card = null;
    		
    		public Flash(PKCard card){
    			this.card = card;
    		}
    		
    		/*
    		 **线程的run()方法
    		 **为纸牌的正面设置白色图片
    		 */
    		public void run(){
    			boolean is = false;
    			ImageIcon icon = new ImageIcon("images/white.gif");
    			for (int i = 0; i < 4; i++){
    				try{
    					Thread.sleep(200);
    				}
    				catch (InterruptedException e){
    					e.printStackTrace();
    				}
    				
    				if (is){
    					this.card.turnFront();
    					is = !is;
    				}
    				else{
    					this.card.setIcon(icon);
    					is = !is;
    				}
    				// 根据当前外观将card的UI属性重置
    				card.updateUI();
    			}
    		}
    	}
    
        /**
    	 **点击鼠标
    	 */
    	public void mousePressed(MouseEvent mp){
            point = mp.getPoint();
            main.setNA();
            this.previousCard = main.getPreviousCard(this);
        }
    
        /**
    	 **释放鼠标
    	 */
    	public void mouseReleased(MouseEvent mr){
    		Point point = ((JLabel) mr.getSource()).getLocation();
    		//判断可行列
    		int n = this.whichColumnAvailable(point);
    		if (n == -1 || n == this.whichColumnAvailable(this.initPoint)){
    			this.setNextCardLocation(null);
    			main.table.remove(this.getLocation());
    			this.setLocation(this.initPoint);
    			main.table.put(this.initPoint, this);
    			return;
    		}
    		
    		point = main.getLastCardLocation(n);
    		boolean isEmpty = false;
    		PKCard card = null;
    		if (point == null){
    			point = main.getGroundLabelLocation(n);
    			isEmpty = true;
    		}
    		else{
    			card = (PKCard) main.table.get(point);
    		}
    		
    		if (isEmpty || (this.value + 1 == card.getCardValue())){
    			point.y += 40;
    			if (isEmpty) point.y -= 20;
    			this.setNextCardLocation(point);
    			main.table.remove(this.getLocation());
    			point.y -= 20;
    			this.setLocation(point);
    			main.table.put(point, this);
    			this.initPoint = point;
    			if (this.previousCard != null){
    				this.previousCard.turnFront();
    				this.previousCard.setCanMove(true);
    			}
    			
    			this.setCanMove(true);
    		}
    		else{
    			this.setNextCardLocation(null);
    			main.table.remove(this.getLocation());
    			this.setLocation(this.initPoint);
    			main.table.put(this.initPoint, this);
    			return;
    		}
            point = main.getLastCardLocation(n);
            card = (PKCard) main.table.get(point);
            if (card.getCardValue() == 1){
    			point.y -= 240;
    			card = (PKCard) main.table.get(point);
    			if (card != null && card.isCardCanMove()){
    				main.haveFinish(n);
    			}
    		}
    	}
    	
    	/*
    	 **方法:放置纸牌
    	 */
    	public void setNextCardLocation(Point point){
    		PKCard card = main.getNextCard(this);
    		if (card != null){
    			if (point == null){
    				card.setNextCardLocation(null);
    				main.table.remove(card.getLocation());
    				card.setLocation(card.initPoint);
    				main.table.put(card.initPoint, card);
    			}
    			else{
    				point = new Point(point);
    				point.y += 20;
    				card.setNextCardLocation(point);
    				point.y -= 20;
    				main.table.remove(card.getLocation());
    				card.setLocation(point);
    				main.table.put(card.getLocation(), card);
    				card.initPoint = card.getLocation();
    			}
    		}
    	}
    
        /**
    	 **返回值:int
    	 **方法:判断可用列
    	 */
    	public int whichColumnAvailable(Point point){
    		int x = point.x;
    		int y = point.y;
    		int a = (x - 20) / 101;
    		int b = (x - 20) % 101;
    		if (a != 9){
    			if (b > 30 && b <= 71){
    				a = -1;
    			}
    			else if (b > 71){
    				a++;
    			}
    		}
    		else if (b > 71){
    			a = -1;
    		}
    		
    		if (a != -1){
    			Point p = main.getLastCardLocation(a);
    			if (p == null) p = main.getGroundLabelLocation(a);
    			b = y - p.y;
    			if (b <= -96 || b >= 96){
    				a = -1;
    			}
    		}
    		return a;
    	}
    
        public void mouseEntered(MouseEvent arg0){
        }
    
        public void mouseExited(MouseEvent arg0){
        }
    
        /**
    	 **用鼠标拖动纸牌
    	 */
    	public void mouseDragged(MouseEvent arg0){
            if (canMove){
    			int x = 0;
    			int y = 0;
    			Point p = arg0.getPoint();
    			x = p.x - point.x;
    			y = p.y - point.y;
    			this.moving(x, y);
    		}
    	}
    
        /**
    	 **返回值:void
    	 **方法:移动(x,y)个位置
    	 */
    	public void moving(int x, int y){
            PKCard card = main.getNextCard(this);
            Point p = this.getLocation();
            
    		//将组件移动到容器中指定的顺序索引。 
    		pane.setComponentZOrder(this, 1);
            
    		//在Hashtable中保存新的节点信息
    		main.table.remove(p);
            p.x += x;
            p.y += y;
            this.setLocation(p);
            main.table.put(p, this);
            if (card != null) card.moving(x, y);
        }
    
        public void mouseMoved(MouseEvent arg0){
        }
    
        /**
         **构造函数
         */
        public PKCard(String name, Spider spider){
            super();
            this.type = new Integer(name.substring(0, 1)).intValue();
            this.value = new Integer(name.substring(2)).intValue();
            this.name = name;
            this.main = spider;
            this.pane = this.main.getContentPane();
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
            this.setIcon(new ImageIcon("images/rear.gif"));
            this.setSize(71, 96);
            this.setVisible(true);
        }
    
        /**
    	 **返回值:void
    	 **方法:令纸牌显示正面
    	 */
    	public void turnFront(){
            this.setIcon(new ImageIcon("images/" + name + ".gif"));
            this.isFront = true;
        }
    
        /**
    	 **返回值:void
    	 **方法:令纸牌显示背面
    	 */
    	public void turnRear(){
            this.setIcon(new ImageIcon("images/rear.gif"));
            this.isFront = false;
            this.canMove = false;
        }
    
        /**
    	 **返回值:void
    	 **方法:将纸牌移动到点point
    	 */
    	public void moveto(Point point){
            this.setLocation(point);
            this.initPoint = point;
        }
    
        /**
    	 **返回值:void
    	 **方法:判断牌是否能移动
    	 */
    	public void setCanMove(boolean can){
            this.canMove = can;
            PKCard card = main.getPreviousCard(this);
            if (card != null && card.isCardFront()){
                if (!can){
                    if (!card.isCardCanMove()){
                        return;
    				}
                    else{
    					card.setCanMove(can);
    				}
    			}
                else{
                    if (this.value + 1 == card.getCardValue()
                            && this.type == card.getCardType()){
    					card.setCanMove(can);
    				}
    				else{
    					card.setCanMove(false);
    				}
                }
            }
        }
    
        /**
    	 **返回值:boolean
    	 **方法:判断card是否是正面
    	 */
    	public boolean isCardFront(){
            return this.isFront;
        }
    
        /*
    	 **返回值:boolean
    	 **方法:返回是否能够移动
    	 */
    	public boolean isCardCanMove(){
            return this.canMove;
        }
    
        /**
    	 **返回值:int
    	 **方法:获得card的内容值
    	 */
    	public int getCardValue(){
            return value;
        }
    
        /**
    	 **返回值:int
    	 **方法:获得card的类型
    	 */
    	public int getCardType(){
            return type;
        }
    }

    SpiderMenuBar.java类

    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JRadioButtonMenuItem;
    import javax.swing.ButtonGroup;
    
    public class SpiderMenuBar extends JMenuBar{
        
    	//生成spider框架对象
        Spider main = null;
       
        //生成菜单组
        JMenu jNewGame = new JMenu("游戏");
        JMenu jHelp = new JMenu("帮助");
        
        //生成菜单项
        JMenuItem jItemAbout = new JMenuItem("关于");
        JMenuItem jItemOpen = new JMenuItem("开局");
        JMenuItem jItemPlayAgain = new JMenuItem("重新发牌");
       
        //生成单选框
        JRadioButtonMenuItem jRMItemEasy = new JRadioButtonMenuItem("简单:单一花色");
        JRadioButtonMenuItem jRMItemNormal = new JRadioButtonMenuItem("中级:双花色");
        JRadioButtonMenuItem jRMItemHard = new JRadioButtonMenuItem("高级:四花色");;
        
        JMenuItem jItemExit = new JMenuItem("退出");
        JMenuItem jItemValid = new JMenuItem("显示可行操作");
        
        
        /**
         **构造函数,生成JMenuBar的图形界面
         */
        public SpiderMenuBar(Spider spider){
            
            this.main = spider;
            
            /**
             **初始化“游戏”菜单栏
             */
            jNewGame.add(jItemOpen);
            jNewGame.add(jItemPlayAgain);
            jNewGame.add(jItemValid);
            
            jNewGame.addSeparator();
            jNewGame.add(jRMItemEasy);
            jNewGame.add(jRMItemNormal);
            jNewGame.add(jRMItemHard);
            
            jNewGame.addSeparator();
            
            jNewGame.add(jItemExit);
            
            ButtonGroup group = new ButtonGroup();
            group.add(jRMItemEasy);
            group.add(jRMItemNormal);
            group.add(jRMItemHard);
    
            jHelp.add(jItemAbout);
    
            this.add(jNewGame);
            this.add(jHelp);
    
    		//为组件添加事件监听并实现
            //“开局”
    		jItemOpen.addActionListener(new java.awt.event.ActionListener() { 
                public void actionPerformed(java.awt.event.ActionEvent e) {    
                    main.newGame();
                }
            });
    
            //“重新发牌”
    		jItemPlayAgain.addActionListener(new java.awt.event.ActionListener() { 
                public void actionPerformed(java.awt.event.ActionEvent e) {    
                    if(main.getC() < 60){
                        main.deal();
                    }
                }
            });
    
            //"显示可行操作"
    		jItemValid.addActionListener(new java.awt.event.ActionListener() { 
                public void actionPerformed(java.awt.event.ActionEvent e) {    
                    new Show().start();
                }
            });
            
            //“退出”
    		jItemExit.addActionListener(new java.awt.event.ActionListener() { 
                public void actionPerformed(java.awt.event.ActionEvent e) {    
                    main.dispose();
                    System.exit(0);
                }
            });
    		
    		//“简单级别”默认已选
    		jRMItemEasy.setSelected(true);
            
    		//“简单级别”
    		jRMItemEasy.addActionListener(new java.awt.event.ActionListener() { 
                public void actionPerformed(java.awt.event.ActionEvent e) {    
                    main.setGrade(Spider.EASY);
                    main.initCards();
                    main.newGame();
                }
            });
            
            //“中级”
    		jRMItemNormal.addActionListener(new java.awt.event.ActionListener() { 
                public void actionPerformed(java.awt.event.ActionEvent e) {    
                    main.setGrade(Spider.NATURAL);
                    main.initCards();
                    main.newGame();
                }
            });
    
            //“高级”
    		jRMItemHard.addActionListener(new java.awt.event.ActionListener() { 
                public void actionPerformed(java.awt.event.ActionEvent e) {    
                    main.setGrade(Spider.HARD);
                    main.initCards();
                    main.newGame();
                }
            });
    
    		jNewGame.addMenuListener(new javax.swing.event.MenuListener() { 
                public void menuSelected(javax.swing.event.MenuEvent e) {    
                    if(main.getC() < 60){
                        jItemPlayAgain.setEnabled(true);
                    }
                    else{
                        jItemPlayAgain.setEnabled(false);
                    }
                }
                public void menuDeselected(javax.swing.event.MenuEvent e) {} 
                public void menuCanceled(javax.swing.event.MenuEvent e) {} 
            });
            
            //“关于”
    		jItemAbout.addActionListener(new java.awt.event.ActionListener() { 
                public void actionPerformed(java.awt.event.ActionEvent e) {    
                    new AboutDialog();
                }
            });
        }
    
        /**
         **构造线程:显示可以执行的操作
         */
        class Show extends Thread{
            public void run(){
                main.showEnableOperator();
            }
        }
     }

    Spider.java 类

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    
    public class Spider extends JFrame{
    	
    	//整型变量,表示难度等级为:简单
    	public static final int EASY = 1;
        //整型变量,表示难度等级为:普通
    	public static final int NATURAL = 2;
        //整型变量,表示难度等级为:难
    	public static final int HARD = 3;
        //设定初始难度等级为简单
    	private int grade = Spider.EASY;
        private Container pane = null;
        //生成纸牌数组
    	private PKCard cards[] = new PKCard[104];
        private JLabel clickLabel = null;
        private int c = 0;
        private int n = 0;
        private int a = 0;
        private int finish = 0;
        Hashtable table = null;
        private JLabel groundLabel[] = null;
    
        public static void main(String[] args){
            Spider spider = new Spider();
    		spider.setVisible(true);
        }
    
        /**
         **构造函数
         */
        public Spider(){
        	//改变系统默认字体
    		Font font = new Font("Dialog", Font.PLAIN, 12);
    		java.util.Enumeration keys = UIManager.getDefaults().keys();
    		while (keys.hasMoreElements()) {
    			Object key = keys.nextElement();
    			Object value = UIManager.get(key);
    			if (value instanceof javax.swing.plaf.FontUIResource) {
    				UIManager.put(key, font);
    			}
    		}
    		setTitle("蜘蛛牌");
            setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
            //设置框架的大小
    		setSize(1024, 742);
            
    		//生成SpiderMenuBar对象,并放置在框架之上
    		setJMenuBar(new SpiderMenuBar(this));
            pane = this.getContentPane();
            //设置背景颜色
    		pane.setBackground(new Color(0, 112, 26));
            //将布局管理器设置成为null
    		pane.setLayout(null);
            clickLabel = new JLabel();
            clickLabel.setBounds(883, 606, 121, 96);
            pane.add(clickLabel);
            
    		clickLabel.addMouseListener(new MouseAdapter(){
                public void mouseReleased(MouseEvent me){
                    if (c < 60){
    					Spider.this.deal();
    				}
                }
            });
            
    		this.initCards();
            this.randomCards();
            this.setCardsLocation();
            groundLabel = new JLabel[10];
            
    		int x = 20;
            for (int i = 0; i < 10; i++)
            {
                groundLabel[i] = new JLabel();
                groundLabel[i]
                        .setBorder(javax.swing.BorderFactory
                                .createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
                groundLabel[i].setBounds(x, 25, 71, 96);
                x += 101;
                this.pane.add(groundLabel[i]);
            }
            
    		this.setVisible(true);
            this.deal();
            
    		this.addKeyListener(new KeyAdapter(){
                class Show extends Thread{
                    public void run(){
                        Spider.this.showEnableOperator();
                    }
                }
    
                public void keyPressed(KeyEvent e){
                    if (finish != 8) if (e.getKeyCode() == KeyEvent.VK_D && c < 60){
                        Spider.this.deal();
                    }
                    else if (e.getKeyCode() == KeyEvent.VK_M){
                        new Show().start();
                    }
                }
            });
        }
    
        /**
    	 **开始新游戏
    	 */
    	public void newGame(){
            this.randomCards();
            this.setCardsLocation();
            this.setGroundLabelZOrder();
            this.deal();
        }
    
        /**
    	 **返回值:int
    	 **返回牌的数量
    	  */
    	public int getC(){
            return c;
        }
    
        /**
    	 **设置等级
    	 */
    	public void setGrade(int grade){
            this.grade = grade;
        }
    
        /**
    	 **纸牌初始化
    	 */
    	public void initCards(){
            //如果纸牌已被赋值,即将其从框架的面板中移去
    		if (cards[0] != null){
                for (int i = 0; i < 104; i++){
                    pane.remove(cards[i]);
                }
            }
            
    		int n = 0;
            //通过难度等级,为n赋值
    		if (this.grade == Spider.EASY){
                n = 1;
            }
            else if (this.grade == Spider.NATURAL){
                n = 2;
            }
            else{
                n = 4;
            }
          //为card赋值  
    		for (int i = 1; i <= 8; i++){
                for (int j = 1; j <= 13; j++){
                    cards[(i - 1) * 13 + j - 1] = new PKCard((i % n + 1) + "-" + j,
                            this);
                }
            }
            
    		//随机纸牌初始化
    		this.randomCards();
        }
    
        /**
    	 **纸牌随机分配
    	 */
    	public void randomCards(){
            PKCard temp = null;
            //随机生成牌号
    		for (int i = 0; i < 52; i++){
                int a = (int) (Math.random() * 104);
                int b = (int) (Math.random() * 104);
                temp = cards[a];
                cards[a] = cards[b];
                cards[b] = temp;
            }
        }
    
        /**
    	 **设置还原
    	 */
    	public void setNA(){
            a = 0;
            n = 0;
        }
    
        /**
    	 **设置纸牌的位置
    	 */
    	public void setCardsLocation(){
            table = new Hashtable();
            c = 0;
            finish = 0;
            n = 0;
            a = 0;
            int x = 883;
            int y = 580;
    		//初始化待展开的纸牌
            for (int i = 0; i < 6; i++){
                for (int j = 0; j < 10; j++){
                    int n = i * 10 + j;
                    pane.add(cards[n]);
                    //将card转向背面
    				cards[n].turnRear();
                    //将card放在固定的位置上
    				cards[n].moveto(new Point(x, y));
                    //将card的位置及相关信息存入
    				table.put(new Point(x, y), cards[n]);
                }
                x += 10;
            }
    		
    		x = 20;
            y = 45;
            //初始化表面显示的纸牌
    		for (int i = 10; i > 5; i--){
                for (int j = 0; j < 10; j++){
                    int n = i * 10 + j;
                    if (n >= 104) continue;
                    pane.add(cards[n]);
                    cards[n].turnRear();
                    cards[n].moveto(new Point(x, y));
                    table.put(new Point(x, y), cards[n]);
                    x += 101;
                }
                x = 20;
                y -= 5;
            }
        }
    
        /**
    	 **返回值:void
    	 **方法:显示可移动的操作
    	 */
    	public void showEnableOperator(){
            int x = 0;
            out: while (true){
                Point point = null;
                PKCard card = null;
                do{
                    if (point != null){
    					n++;
    				}
                    point = this.getLastCardLocation(n);
                    while (point == null){
                        point = this.getLastCardLocation(++n);
                        if (n == 10) n = 0;
                        x++;
                        if (x == 10) break out;
                    }
                    card = (PKCard) this.table.get(point);
                }
                while (!card.isCardCanMove());
                while (this.getPreviousCard(card) != null
                        && this.getPreviousCard(card).isCardCanMove()){
                    card = this.getPreviousCard(card);
                }
                if (a == 10){
    				a = 0;
    			}
                for (; a < 10; a++){
                    if (a != n){
                        Point p = null;
                        PKCard c = null;
                        do{
                            if (p != null){
    							a++;
    						}
    						p = this.getLastCardLocation(a);
                            int z = 0;
                            while (p == null){
                                p = this.getLastCardLocation(++a);
                                if (a == 10) a = 0;
                                if (a == n) a++;
                                z++;
                                if (z == 10) break out;
                            }
                            c = (PKCard) this.table.get(p);
                        }
                        while (!c.isCardCanMove());
                        if (c.getCardValue() == card.getCardValue() + 1){
                            card.flashCard(card);
                            try{
                                Thread.sleep(800);
                            }
                            catch (InterruptedException e){
                                e.printStackTrace();
                            }
                            c.flashCard(c);
                            a++;
                            if (a == 10){
    							n++;
    						}
                            break out;
                        }
                    }
                }
                n++;
                if (n == 10){
    				n = 0;
    			}
                x++;
                if (x == 10){
    				break out;
    			}
            }
        }
    
        /*
    	 **返回值:void
    	 **方法:游戏运行
    	 */
    	public void deal()
        {
            this.setNA();
            //判断10列中是否空列
    		for (int i = 0; i < 10; i++){
                if (this.getLastCardLocation(i) == null){
                    JOptionPane.showMessageDialog(this, "有空位不能发牌!", "提示",
                            JOptionPane.WARNING_MESSAGE);
                    return;
                }
            }
            int x = 20;
            
    		for (int i = 0; i < 10; i++){
                Point lastPoint = this.getLastCardLocation(i);
                //这张牌应“背面向上”
    			if (c == 0){
                    lastPoint.y += 5;
    			}
                //这张牌应“正面向上”
    			else{
                    lastPoint.y += 20;
    			}
                
    			table.remove(cards[c + i].getLocation());
                cards[c + i].moveto(lastPoint);
                table.put(new Point(lastPoint), cards[c + i]);
                cards[c + i].turnFront();
                cards[c + i].setCanMove(true);
                
    			//将组件card移动到容器中指定的顺序索引。 
    			this.pane.setComponentZOrder(cards[c + i], 1);
                
    			Point point = new Point(lastPoint);
                if (cards[c + i].getCardValue() == 1){
                    int n = cards[c + i].whichColumnAvailable(point);
                    point.y -= 240;
                    PKCard card = (PKCard) this.table.get(point);
                    if (card != null && card.isCardCanMove()){
                        this.haveFinish(n);
                    }
                }
                x += 101;
            }
            c += 10;
        }
    
    	/*
    	 **返回值:PKCard对象
    	 **方法:获得card上面的那张牌
    	 */
    	public PKCard getPreviousCard(PKCard card){
            Point point = new Point(card.getLocation());
            point.y -= 5;
            card = (PKCard) table.get(point);
            if (card != null){
    			return card;
    		}
            point.y -= 15;
            card = (PKCard) table.get(point);
            return card;
        }
    
        /**
    	 **返回值:PKCard对象
    	 **方法:取得card下面的一张牌
    	 */
    	public PKCard getNextCard(PKCard card){
            Point point = new Point(card.getLocation());
            point.y += 5;
            card = (PKCard) table.get(point);
            if (card != null)
    			return card;
            point.y += 15;
            card = (PKCard) table.get(point);
            return card;
        }
    
        /**
    	 **返回值:Point对象
    	 **方法:取得第column列最后一张牌的位置
    	 */
    	public Point getLastCardLocation(int column){
            Point point = new Point(20 + column * 101, 25);
            PKCard card = (PKCard) this.table.get(point);
            if (card == null) return null;
            while (card != null){
                point = card.getLocation();
                card = this.getNextCard(card);
            }
            return point;
        }
    
        public Point getGroundLabelLocation(int column){
            return new Point(groundLabel[column].getLocation());
        }
    
        /*
    	 **返回值:void
    	 **方法:放置groundLable组件
    	 */
    	public void setGroundLabelZOrder(){
            for (int i = 0; i < 10; i++){
                //将组件groundLable移动到容器中指定的顺序索引。顺序(105+i)确定了绘制组件的顺序;具有最高顺序的组件将第一个绘制,而具有最低顺序的组件将最后一个绘制。在组件重叠的地方,具有较低顺序的组件将覆盖具有较高顺序的组件。 
    			pane.setComponentZOrder(groundLabel[i], 105 + i);
            }
        }
    
        /*
    	 **返回值:void
    	 **方法:判断纸牌的摆放是否完成
    	 */
    	public void haveFinish(int column){
            Point point = this.getLastCardLocation(column);
            PKCard card = (PKCard) this.table.get(point);
            do{
                this.table.remove(point);
                card.moveto(new Point(20 + finish * 10, 580));
                //将组件移动到容器中指定的顺序索引。 
    			pane.setComponentZOrder(card, 1);
                //将纸牌新的相关信息存入Hashtable
    			this.table.put(card.getLocation(), card);
                card.setCanMove(false);
                point = this.getLastCardLocation(column);
                if (point == null)
                    card = null;
                else
                    card = (PKCard) this.table.get(point);
            }
            while (card != null && card.isCardCanMove());
            finish++;
            //如果8付牌全部组合成功,则显示成功的对话框
    		if (finish == 8){
                JOptionPane.showMessageDialog(this, "恭喜你,顺利通过!", "成功",
                        JOptionPane.PLAIN_MESSAGE);
            }
            if (card != null){
                card.turnFront();
                card.setCanMove(true);
            }
        }
    }
    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系bd@zhengruan.com
    作者最新文章
    编程开发 Java
    相关文章 更多
    精品专题 更多
    装机必备

    正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

    Windows

    正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

    PDF教程

    正软商城PDF教程频道提供PDF编辑、转换、合并、拆分、压缩及格式处理方法,同时介绍常用PDF软件和工具的使用技巧。

    macOS软件

    正软商城macOS软件专区,精选适用于Mac电脑的办公、设计、影音、效率、开发和系统工具,提供软件功能介绍、macOS兼容版本、正版授权及购买下载服务。

    IOS软件

    正软商城iOS软件专区,精选适用于iPhone和iPad的办公、学习、影音、设计、效率及AI应用,提供功能介绍、适用设备、系统要求和正版获取方式等信息。

    AI

    正软商城AI软件专区,汇集AI写作、AI绘画、AI视频、AI办公、AI编程、AI翻译、智能客服和数据分析等人工智能工具,提供功能介绍、适用平台、收费方式及正版购买信息。

    Mac软件 更多
    Adobe Acrobat Pro macOS版
    Adobe Acrobat Pro macOS版

    Adobe Acrobat Pro 是面向办公、法务、财务、教育及出版用户的专业PDF处理软件,可在Mac上创建、编辑、转换、合并和保护文档,并提供OCR文字识别、表单、批注、内容比较、敏感信息遮盖及电子签名功能。

    Blender
    Blender

    Blender 是一款免费开源、跨平台的专业 3D 创作软件,集建模、动画、渲染、视频编辑与视觉合成等功能于一体,广泛应用于影视动画、游戏设计和建筑可视化等领域。软件支持 Cycles 物理渲染器与 Eevee 实时渲染引擎,并提供多边形建模、骨骼绑定、物理模拟等专业工具。Blender 兼容 Windows、macOS 和 Linux 系统,安装包轻巧、运行流畅,依托活跃的全球开发者社区持续更新,是从初学者到专业创作者都值得选择的正版 3D 创作工具。

    Shapr3D macOS版
    Shapr3D macOS版

    Shapr3D是一款面向工业设计、机械工程、建筑概念和三维打印工作流的CAD软件。Mac版采用Parasolid建模内核,支持草图约束、实体建模、工程图、可视化渲染及常见CAD格式交换,并可通过账户在多台设备之间同步项目。

    Adobe Illustrator macOS版
    Adobe Illustrator macOS版

    Adobe Illustrator 是 Adobe 推出的专业矢量图形设计软件,适合在 Mac 上制作标志、图标、插画、包装、信息图及印刷版面。它以路径和锚点构建可无损缩放的图形,并提供钢笔、形状、文字、图像描摹、渐变、画板及生成式功能。

    Adobe InDesign macOS版
    Adobe InDesign macOS版

    Adobe InDesign 是面向印刷品与数字出版物的专业版面设计软件,可在 Mac 上制作书籍、杂志、宣传册、海报、报告及交互式文档。它提供精细的文字样式、网格、母版、长文档管理和印前输出能力。

    Affinity Designer macOS版
    Affinity Designer macOS版

    Affinity Designer 的矢量设计能力现已整合进 Affinity by Canva,可在 Mac 上进行标志、图标、插画、界面原型及印刷品设计。个人用户使用免费 Canva 账户即可使用完整的矢量、像素和版面工具。

    Archicad macOS版
    Archicad macOS版

    Archicad是Graphisoft推出的建筑信息模型设计软件,可在Mac上完成概念设计、参数化建模、图纸编制、工程量统计、渲染展示及团队协同。模型与平立剖面、明细表和布局保持关联,适合建筑师、室内设计师、BIM团队及相关专业学生使用。

    AutoCAD LT macOS版
    AutoCAD LT macOS版

    AutoCAD LT是Autodesk推出的专业二维CAD设计与文档编制软件,Mac版本提供原生界面、多点触控及分屏查看体验。用户可精确创建和编辑DWG图形,完成图层管理、尺寸标注、表格、PDF参考、外部参照及打印输出。

    AutoCAD macOS版
    AutoCAD macOS版

    AutoCAD for Mac 是 Autodesk 面向 macOS 提供的专业计算机辅助设计软件,支持精确的二维绘图、标注、布局输出以及实体、曲面和网格建模。软件采用适配 Mac 操作习惯的界面,可处理 DWG 图纸。

    Autodesk Fusion macOS版
    Autodesk Fusion macOS版

    Autodesk Fusion 是面向产品设计与制造的一体化云连接平台,在 Mac 上提供参数化建模、自由曲面、装配、工程图、仿真、PCB 设计和 CAM 加工等工具。它适合机械工程师、工业设计师、创客及制造团队使用。

    Autodesk Maya macOS版
    Autodesk Maya macOS版

    Autodesk Maya是一套面向影视动画、游戏开发和视觉特效制作的专业三维创作软件,集多边形与曲面建模、角色绑定、关键帧动画、动力学模拟、程序化特效及Arnold渲染于一体,并提供脚本、插件和开放式制作管线支持。

    Blender macOS版
    Blender macOS版

    Blender是一款免费开源的综合三维创作软件,可在Mac上完成建模、数字雕刻、材质制作、角色绑定、动画、物理模拟、渲染、合成及视频剪辑。软件提供Cycles与Eevee渲染工具,并支持几何节点、蜡笔绘制和Python扩展。

    WINDOWS 更多
    AutoCAD Windows版
    AutoCAD Windows版

    AutoCAD 是 Autodesk 推出的专业计算机辅助设计软件,Windows 版支持精确二维绘图、三维实体与曲面建模、图纸标注、布局发布和任务自动化,并包含多套行业专业化工具组合,适用于建筑、机械、电气、工程建设及制造设计人员。

    Adobe Acrobat Pro Windows版
    Adobe Acrobat Pro Windows版

    Adobe Acrobat Pro 是面向专业 PDF 工作的综合解决方案,可在 Windows 中创建、编辑、转换、比较、整理和保护文档,并支持表单、电子签名、扫描件文字识别及敏感信息永久遮盖。订阅还包含网页、移动服务和云端协作能力,

    Blender
    Blender

    Blender 是一款免费开源、跨平台的专业 3D 创作软件,集建模、动画、渲染、视频编辑与视觉合成等功能于一体,广泛应用于影视动画、游戏设计和建筑可视化等领域。软件支持 Cycles 物理渲染器与 Eevee 实时渲染引擎,并提供多边形建模、骨骼绑定、物理模拟等专业工具。Blender 兼容 Windows、macOS 和 Linux 系统,安装包轻巧、运行流畅,依托活跃的全球开发者社区持续更新,是从初学者到专业创作者都值得选择的正版 3D 创作工具。

    3dmax(3ds max)
    3dmax(3ds max)

    Autodesk 3ds Max 是一款专业的三维建模、动画与渲染软件,广泛应用于建筑可视化、游戏开发、影视动画、广告设计和产品展示等领域。

    Adobe InDesign Windows版
    Adobe InDesign Windows版

    Adobe InDesign 是面向印刷与数字出版的专业页面排版工具,可制作书籍、杂志、宣传册、海报、交互式 PDF 和电子书。它提供精细的文字样式、多页面管理、印前检查及多格式输出能力,并能与 Photoshop、Illustrator

    Adobe Illustrator Windows版
    Adobe Illustrator Windows版

    Adobe Illustrator 是用于绘制矢量图形、插画、图标、徽标和版式的专业设计工具。它以路径和锚点为核心,作品放大后仍能保持清晰,并提供画板、文字、渐变、图像描摹及精确变换等功能,适合品牌视觉、印刷物料、数字界面和商业插画创作。

    Figma Windows版
    Figma Windows版

    Figma 是面向产品设计与开发团队的云端协作平台,可在 Windows 桌面客户端中完成界面绘制、组件管理、交互原型、评论评审和开发交付。文件保存在云端,多名成员能够同步编辑并共享设计系统;官方同时提供免费 Starter 方案及按席位计

    Affinity Designer Windows版
    Affinity Designer Windows版

    Affinity Designer 是面向矢量图形、数字插画和界面设计的专业创作工具,可在矢量、像素与导出工作区之间快速切换。它提供曲线编辑、形状构建、画板、排版、图层和专业色彩管理,并支持常见设计文件的导入及多种印刷、网页格式输出。

    Cinema 4D Windows版
    Cinema 4D Windows版

    Cinema 4D是Maxon推出的专业三维创作软件,将建模、动画、MoGraph动态图形、粒子与动力学模拟、材质灯光及Redshift渲染整合在统一工作环境中,适用于广告包装、影视特效、产品和建筑可视化、游戏素材等制作场景。

    Blender Windows版
    Blender Windows版

    Blender 是免费开源的三维创作套件,在 Windows 上提供建模、雕刻、材质、动画、物理模拟、合成、跟踪、视频剪辑及 Cycles、EEVEE 渲染工具。它适合个人创作者、学生和小型工作室,

    Autodesk Maya Windows版
    Autodesk Maya Windows版

    Autodesk Maya 是面向影视、电视、动画和游戏行业的专业三维制作软件,集多边形与 NURBS 建模、角色绑定、关键帧和动作捕捉动画、Bifrost 特效模拟、材质开发及 Arnold 渲染于一体,

    SketchUp Pro Windows版
    SketchUp Pro Windows版

    SketchUp Pro 是面向建筑、室内、景观、施工和产品设计等专业场景的三维建模工具,提供桌面建模、LayOut 二维文档、模型资源库、扩展程序及云端协作能力。其操作逻辑直观,适合从概念推敲、空间规划到图纸表达和方案汇报的完整工作流程。