Java 发牌


JAVA 发牌

package pukepai;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class test implements ActionListener
{

    JFrame jf;
    JButton b1;
    JButton b2;
    public  static JButton ButtonPhoto[];
    public class createChuangkou1 extends JInternalFrame
    {
        JLayeredPane jp;
        int n = 0;
        dapai da = new dapai();
        Toolkit tk = Toolkit.getDefaultToolkit();
        //工具包
        int w = (tk.getScreenSize().width);
        int h = (tk.getScreenSize().height);
        JButton chupaiButton;

        int chupaishu = 0;
        int tiaoguoshu = 0;
        int chupaixuhao[] = new int[13];
        int tiaoguopaiBool = 0;
        int yitiaoguoshu = 0;

        public createChuangkou1(int n)
        {

            super("Player" + n);
            ButtonPhoto=new JButton[13];
            this.n = n;
            this.setLayout(null);
            jp = this.getLayeredPane();
            Setbg();
            for (int i = 0; i <= 12; i++)
            {
                chupaixuhao[i] = -1;
            }
            Setbutton(n);
            chupaiButton = new JButton("To play a card");
            chupaiButton.setBounds(850, h - 201 - 150 + 200, 200, 50);
            chupaiButton.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    for (int i = 0; i <= 12; i++)
                    {
                        if (ButtonPhoto[i].getLocation().y != 729)
                        {
                            ButtonPhoto[i].setLocation(w / 2 - 60, 400);

                            chupaixuhao[chupaishu] = i;
                            chupaishu++;
                        }

                    }
                }

            });
            jp.add(chupaiButton, new Integer(-110));
            repaint();
        }

        void Setbg()
        {
            ImageIcon bj = new ImageIcon("img/bj.jpg");
            JLabel jl = new JLabel(bj);
            jl.setSize(w, h);
            jp.add(jl, new Integer(-100));
        }
         class WaIt implements Runnable
        {


            public void run()
            {

                for(int i=0;i<=12;i++)
                {
                    ButtonPhoto[i].setVisible(true);
                    try
                    {
                        Thread.sleep(350);
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }


            }
        }
        void Setbutton(int k)
        {
            WaIt wait1=new WaIt();
            Thread puk=new Thread(wait1);
            String[] WpukePhoto = new String[13];
            WpukePhoto = da.zuhe();

            ImageIcon PukePhoto = new ImageIcon("img/fang1.jpg");
            int pw = PukePhoto.getIconWidth();
            int ph = PukePhoto.getIconHeight();
            int x1 = 70; // 表示2个扑克牌按钮叠加在一起的宽度
            int y1 = 150; // 表示扑克牌按钮底边距离neibuchuangti底边的位置
            int kuan = pw - x1;
            int firstPositionx = (w - kuan * 13) / 2 - 35;
            int firstPositiony = (h - ph - y1);

            for (int i = 0; i <= 12; i++)
            {

                if (tiaoguopaiBool != 1)
                {
                    PukePhoto = new ImageIcon("img/" + WpukePhoto[i]);
                    ButtonPhoto[i] = new JButton(PukePhoto);
                    ButtonPhoto[i].setBounds(firstPositionx + kuan * i, firstPositiony, pw, ph);
                    ButtonPhoto[i].addActionListener(new ActionListener()
                    {
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                            JButton bn;
                            bn = (JButton) e.getSource();
                            int flag = 1;
                            jp.add(chupaiButton, new Integer(10));

                            for (int i = 0; i <= 12; i++)
                            {
                                for (int j = 0; j <= 12; j++)
                                {
                                    if (i == chupaixuhao[j])
                                    {
                                        tiaoguopaiBool = 1;
                                        yitiaoguoshu++;
                                    }
                                }
                                if (bn == ButtonPhoto[i])
                                {
                                    for (int j = 0; j <= 12; j++)
                                    {
                                        if (j == i)
                                            continue;
                                        if (ButtonPhoto[j].getLocation().y != (h - ph - y1))
                                            ButtonPhoto[j].setBounds(firstPositionx + kuan * j, firstPositiony, pw, ph);
                                    }
                                    if (ButtonPhoto[i].getLocation().y == (h - ph - y1))
                                    {
                                        System.out.println(h - ph - y1);
                                        ButtonPhoto[i].setBounds(firstPositionx + kuan * i, firstPositiony - 30, pw, ph);
                                    }
                                }
                            }
                            repaint();
                        }
                    });
                    jp.add(ButtonPhoto[i], new Integer(i));
                    ButtonPhoto[i].setVisible(false);
                    WaIt t1=new WaIt();
                    Thread thread=new Thread(t1);
                    thread.start();
                }

            }
        }



    }
    public class createChuangkou extends JInternalFrame
    {
        JButton[] ButtonPhoto;
        JLayeredPane jp;
        int n = 0;
        dapai da = new dapai();
        Toolkit tk = Toolkit.getDefaultToolkit();
        //工具包
        int w = (tk.getScreenSize().width) / 2;
        int h = (tk.getScreenSize().height) / 2;
        JButton chupaiButton;

        int chupaishu = 0;
        int tiaoguoshu = 0;
        int chupaixuhao[] = new int[13];
        int tiaoguopaiBool = 0;
        int yitiaoguoshu = 0;

        public createChuangkou(int n)
        {
            super("Player" + n);
            this.n = n;
            this.setLayout(null);
            jp = this.getLayeredPane();
            Setbg();
            Setbutton(n);;
            repaint();
        }

        void Setbg()
        {
            ImageIcon bj = new ImageIcon("img/bj.jpg");
            JLabel jl = new JLabel(bj);
            jl.setSize(w, h);
            jp.add(jl, new Integer(-100));
        }
        class WaIt2 implements Runnable
        {


            public void run()
            {

                for(int i=0;i<=12;i++)
                {
                    ButtonPhoto[i].setVisible(true);
                    try
                    {
                        Thread.sleep(350);
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }


            }
        }

        void Setbutton(int k)
        {

            String[] WpukePhoto = new String[13];
            WpukePhoto = da.zuhe();
            ButtonPhoto = new JButton[13];
            ImageIcon PukePhoto = new ImageIcon("img/fang1.jpg");
            int pw = PukePhoto.getIconWidth();
            int ph = PukePhoto.getIconHeight();
            int x1 = 70; // 表示2个扑克牌按钮叠加在一起的宽度
            int y1 = 150; // 表示扑克牌按钮底边距离neibuchuangti底边的位置
            int kuan = pw - x1;
            int firstPositionx = (w - kuan * 13) / 2 - 35;
            int firstPositiony = (h - ph - y1 + 20);
            for (int i = 0; i <= 12; i++)
            {
                PukePhoto = new ImageIcon("img/" + WpukePhoto[i]);
                ButtonPhoto[i] = new JButton(PukePhoto);
                ButtonPhoto[i].setBounds(firstPositionx + kuan * i, firstPositiony, pw, ph);
                ButtonPhoto[i].addActionListener(new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        JButton bn;
                        bn = (JButton) e.getSource();
                        int flag = 1;
                        jp.add(chupaiButton, new Integer(10));

                        for (int i = 0; i <= 12; i++)
                        {
                            for (int j = 0; j <= 12; j++)
                            {
                                if (i == chupaixuhao[j])
                                {
                                    tiaoguopaiBool = 1;
                                    yitiaoguoshu++;
                                }
                            }
                            if (bn == ButtonPhoto[i])
                            {
                                for (int j = 0; j <= 12; j++)
                                {
                                    if (j == i)
                                        continue;
                                    if (ButtonPhoto[j].getLocation().y != firstPositiony)
                                    {
                                        System.out.println(ButtonPhoto[j].getLocation().y);
                                        ButtonPhoto[j].setBounds(firstPositionx + kuan * j, firstPositiony, pw, ph);
                                    }

                                }
                                if (ButtonPhoto[i].getLocation().y == firstPositiony)
                                {
                                    System.out.println(firstPositiony);
                                    ButtonPhoto[i].setBounds(firstPositionx + kuan * i, firstPositiony - 30, pw, ph);
                                }


                            }
                        }
                        repaint();
                    }
                });
                jp.add(ButtonPhoto[i], new Integer(i));
                ButtonPhoto[i].setVisible(false);
                WaIt2 t2=new WaIt2();
                Thread thread2=new Thread(t2);
                thread2.start();
            }
            chupaiButton = new JButton("To play a card");
            chupaiButton.setBounds(300, h - 201 - 150 + 200 + 20, 200, 50);
            chupaiButton.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    for (int i = 0; i <= 12; i++)
                    {
                        if (ButtonPhoto[i].getLocation().y != 189 + 20)
                        {
                            ButtonPhoto[i].setBounds(w / 2 - 60, 10, 134, 201);

                            chupaixuhao[chupaishu] = i;
                            chupaishu++;
                        }

                    }
                }

            });
            jp.add(chupaiButton, new Integer(-110));


        }

    }


    public test()
    {
        dapai da = new dapai();
        //抽取程序
        Font font = new Font("黑体", Font.BOLD, 36);
        JFrame jf = new JFrame("LHL's Java");
        jf.setLayout(new FlowLayout(FlowLayout.CENTER));
        JButton b1 = new JButton("Play 1");
        b1.addActionListener(new ActionListener()
                             {
                                 @Override
                                 public void actionPerformed(ActionEvent e)
                                 {
                                     JFrame jf2 = new JFrame("Play1");

                                     jf2.setLayout(new GridLayout(1, 1));
                                     //外窗口
                                     JInternalFrame jif = new JInternalFrame();
                                     jif = new createChuangkou1(1);
                                     jif.setVisible(true);
                                     jf2.add(jif);
                                     jf2.setExtendedState(JFrame.MAXIMIZED_BOTH);
                                     jf2.setVisible(true);
                                     jf2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



                                 }


                             }
        );
        b1.setFont(font);
        jf.add(b1, Component.CENTER_ALIGNMENT);
        JButton b2 = new JButton("Play 4");
        b2.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JFrame jf2 = new JFrame("Play2");

                jf2.setLayout(new GridLayout(2, 2));
                //外窗口
                JInternalFrame jif[] = new JInternalFrame[4];
                for (int i = 0; i <= 3; i++)
                {
                    jif[i] = new createChuangkou(i + 1);
                    jif[i].setVisible(true);
                    jf2.add(jif[i]);
                }

                jf2.setExtendedState(JFrame.MAXIMIZED_BOTH);
                jf2.setVisible(true);
                jf2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            }
        });
        b2.setFont(font);
        jf.add(b2);

        jf.setBounds(500, 500, 400, 135);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args)
    {
        new test();

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {


    }
}




package pukepai;

public class dapai
{
    public String[] puke1=new String[53];
    public String[] puke2=new String[puke1.length];
    public String[] puke3=new String[13];
    //没排好序的牌
    public String[] puke4=new String[53];
    //计算重复的牌
    public String[] puke5=new String[53];
    //排好序的牌
    int k=0;
    //取出牌的数量

    public dapai()
    {
        int count=0;
        String[] puke= {"fang","hong","mei","hei"};
        int i,j,k;
        for(i=0;i<4;i++)
        {
            String name=puke[i];
            for(j=0;j<13;j++)
            {
                puke1[count]=
                        name=puke[i]+(int)(j+1)+".jpg";
                count++;
            }
        }
        daluan();

    }

    public void  daluan()
    {
        System.arraycopy(puke1, 0, puke2, 0, puke1.length);
        int i,j,m;
        for(m=0;m<puke1.length;m++) {
            i=(int)(Math.random()*100)%52;
            j=(int)(Math.random()*100)%52;
            String t;
            t=puke2[i];
            puke2[i]=puke2[j];
            puke2[j]=t;
        }
    }
    public void chouqu()
    {
        int i;
        for(i=0;i<13;i++)
        {
            puke3[i]=puke2[i];
            System.out.println(puke3[i]);
        }

    }
    public  String[] zuhe()
    {
        int i;
        int j;
        int f=1;
        int k3=0;//puke3
        int k4=0;//puke4
        for(i=0;i<13;i++)
        {
            for(j=0;j<52;j++)
            {
                if(puke2[i]==puke4[j])
                {
                    f = 0;
                    break;
                }
            }
            if(f==1)
            {
                puke3[k3++]=puke2[i];
                puke4[k4++]=puke2[i];
            }
            f=1;
        }
        int k=0;
        //puke5的计数
        for(i=0;i<52;i++)
        {
            for(j=0;j<13;j++)
            {
                if(puke1[i]==puke3[j])
                {
                    puke5[k++]=puke3[j];
                }
            }
        }
        return puke5;
    }

    public static void main(String[]args)
    {
        dapai da=new dapai();
        String s1[]=new String[13];


        s1=da.zuhe();

        for(int i=0;i<13;i++)
            System.out.println(s1[i]);
        System.out.println("---------------------------");

    }
};




文章作者: LHL
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 LHL !
评论
  目录