局域网斗地主(java)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

局域网三人斗地主程序(java版)
说明:本程序支持局域网游戏,运行服务器端后,再运行三个客户端分别输入服务器端的IP 即可以游戏。

附件说明:
扑克牌重1~54命名,分别为3到大王(四个一组,大小王除外,比如1.jpg,2.jpg都是3),0.jpg 为扑克牌的反面可以从网上下载。

声音文件参考QQ斗地主。

服务器端:三个四个类分别是:IMServer.java
客户端主类IMServer.java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import .ServerSocket;
import .Socket;
import java.util.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class IMServer extends JFrame implements ActionListener
{
private int command0=0;//记录接收重新开机的指令数目
private int command1=0;//记录接收是否叫地主的指令数目
private static ArrayList Dizhu=new ArrayList();
private static final int maxThreadLimit = 10;
private static int OnlineNumber=0;
private JPanel p = new JPanel();
DealCard test;
String Command;
private ArrayList Users=new ArrayList();
public int[] Arr=new int[21];
private JTextField jtf = new JTextField();
private JTextArea jta = new JTextArea();
private ArrayList<ThreadServer> threadArray = new ArrayList<ThreadServer>(); //线程数组,用于存放连接线程
public IMServer()
{
p.setLayout(new BorderLayout());
p.add(new JLabel("发送(回车)"),BorderLayout.WEST);
p.add(jtf,BorderLayout.CENTER);
jtf.setHorizontalAlignment(JTextField.RIGHT);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);
getContentPane().add(p,BorderLayout.SOUTH);
jta.setEditable(false);
jtf.setHorizontalAlignment(javax.swing.JTextField.LEFT);
jtf.addActionListener(this);
setTitle("IM");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
new Thread(new Runnable() {
@Override
public void run() {
while(true)
{
if(command0==3)
{
CreatPai();
command0=0;
}
else if(command1==3)
{
int chose1 = 0;
for(int i=0;i<10000;i++)
{
}
if(Dizhu.isEmpty())
{
JOptionPane.showMessageDialog(null,"没人抢地主,随机发牌");
chose1=(int)(Math.random()*3)+1;
}
else
{
int number=(int)(Math.random()*Dizhu.size())+1;
Integer chose=(Integer) Dizhu.get(number-1);
chose1=chose.intValue();
}
Dizhu.clear();
System.out.println(chose1);
guangBo(" 地主是:"+chose1+"系统广播者");
guangBo(" 地主是正在出牌");
String send=test.CreatDizhu(chose1);
System.out.println("地主的牌的是:"+send);
guangBo("12|"+chose1+send);
guangBo("13|"+chose1+test.dipai);
command1=0;
}
}
}
}).start();
try
{
ServerSocket serverSocket = new ServerSocket(8189);
jta.append("服务器开启时间:" + new Date() + '\n');
while(true)
{
if(threadArray.size() >= maxThreadLimit)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
}
}
else
{
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String name=in.readLine();
OnlineNumber++;
jta.append("用户"+name+ "上线: ("+new Date() +")" +'\n');
jta.append("当前在线人数:"+OnlineNumber+"人\n");
ThreadServer thread = new ThreadServer(OnlineNumber,name,socket);
threadArray.add(thread);
Users.add("|"+name);
thread.start();
try {
Thread.sleep(100);
if(OnlineNumber>2)
{
for(int i=0;i<3;i++)
threadArray.get(i).send("ID"+Users.get(0)+Users.get(1)+Users.get(2));
System.out.println(Users);
System.out.println(Users);
CreatPai();
}
} catch (InterruptedException ex) {
}
}
}
}
catch(IOException e)
{
System.err.println(e);
}
}
private void CreatPai()
{
test=new DealCard();
threadArray.get(0).send("11|"+1+er1);
threadArray.get(1).send("11|"+2+er2);
threadArray.get(2).send("11|"+3+er3);
}
public static void main(String[] args)
{
new IMServer();
}
@Override
public void actionPerformed(ActionEvent arg0)
{
if (arg0.getSource() instanceof JTextField)
{
String s = jtf.getText();
if(s != null)
{
guangBo(" 系统消息:"+s);
s = null;
}
jtf.setText("");
}
}
public void guangBo(String s) //向所有客户端监听线程发送消息{
StringTokenizer st = new StringTokenizer(s,"|");
st.nextToken();
jta.append(s+ '\n');
//if(st.hasMoreElements()){
if(threadArray.size() > 0) //遍历这个线程数组
{
for(int i = 0;i < threadArray.size();i++)
{
threadArray.get(i).send(s);
System.out.println(i+": "+s);
}
}
}
//线程内部类
class ThreadServer extends Thread
{
private Socket incoming;
private String name;
private int num;
private PrintWriter out;
public ThreadServer(int num,String name,Socket i)
{
this.num=num;
=name;
incoming = i;
}
public int getnum() {
return num;
}
public String getname()
{
return name;
}
@Override
public void run()
{
// TODO Auto-generated method stub
synchronized(this)
{
try
{
try
{
BufferedReader inStream = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
OutputStream outStream = incoming.getOutputStream();
out = new PrintWriter(outStream, true);
while(true)
{
String s = inStream.readLine();
System.out.println(s);
StringTokenizer st = new StringTokenizer(s,"|");
String command=st.nextToken();
if(st.hasMoreElements())
{
if(command.equals("error"))
{
String name=st.nextToken();
int num=Integer.parseInt(name);
switch(num)
{
case 1:
this.send("11|"+1+er1);
break;
case 2:
this.send("11|"+2+er2);
break;
case 3:
this.send("11|"+2+er2);
break;
default:
System.exit(-1);
}
}
if(command.equals("20"))
{
command0++;
}
else if(command.equals("21"))
{
String name1=st.nextToken();
String dizhu=st.nextToken();
if(dizhu.equals("yes"))
{
Dizhu.add(Integer.parseInt(name1));
}
command1++;
}
else if(command.equals("22"))
{
String name1=st.nextToken();
if(s.endsWith("|"))
{
String realmsg=s.substring(3);
guangBo(" 玩家"+name1+"不出牌");
guangBo("14|"+realmsg+"|-1");
}
else
{
String realmsg=s.substring(3);
System.out.println(realmsg);
guangBo(" 玩家"+name1+"正在出牌");
guangBo("14|"+realmsg);
}
}
else if(command.equals("23"))
{
String name1=st.nextToken();
guangBo("15|"+name1);
}
else if(command.equals("talk"))
{
String msg=st.nextToken();
if(msg.length()>0)
guangBo(s);
}
}
else if(s.length()>0 && !s.equalsIgnoreCase("EXIT")) {
guangBo(s);
s = null;
}
else if(s.equalsIgnoreCase("EXIT"))
{
break;
}
}
}
catch(.SocketException e)
{
System.out.println("有用户退出");
threadArray.remove(this);
}
finally
{
incoming.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
public void send(String s)
{
if(s != null)
{
out.println(s);
}
}
}
public String analysisMsg(String msg)
{
StringTokenizer st = new StringTokenizer(msg,"|");
String realMsg=st.nextToken();
if(st.hasMoreElements())
{
String strre = st.nextToken();//读取用户名
}
//获取信息时间
java.text.DateFormat format1 = new java.text.SimpleDateFormat("HH:mm:ss");
String strTime = format1.format(new Date());
strTime="("+strTime+")";
return msg+strTime;
}
public String ChangeVector(ArrayList v) { //将向量封装成字符串
String RealMsg="";
for(int i=0;i<v.size();i++)
{
RealMsg+="|"+v.get(i);
}
return RealMsg;
}
public String PackMsg(int command,String realMsg)
{
return command+"|Server"+realMsg;
}
}
随机产生排的54不重复的随机数RandomCard.java
public class RandomCard {
public int [] arr=new int[55];
public RandomCard(){
for(int i=1;i<55;i++){
arr[i]=0;
}
}
public int CreatRadom(){
int Radom1=(int)(Math.random()*54)+1;
if(arr[Radom1]==0) arr[Radom1]=1;
else{
while(arr[Radom1]==1){
Radom1=(int)(Math.random()*54)+1;
}
arr[Radom1]=1;
}
return Radom1;
}
/* public static void main(String[] args) {
Radom c= new Radom();
for(int i=1;i<55;i++){
System.out.println(c.CreatRadom()+" "+i);
}
}
*
*/
}
对随机数排序CreateCard,java
public class CreateCard {
public int[] imgPath=new int[55];
static RandomCard r;
public CreateCard(){
r=new RandomCard();
for(int i=1;i<55;i++){
int j=r.CreatRadom();
imgPath[i]=j;
}
this.maoPao(imgPath,1,17);
this.maoPao(imgPath,18,34);
this.maoPao(imgPath,35,51);
this.maoPao(imgPath,52,54);
}
public static void maoPao(int[] x,int begin,int end) {
for (int i = begin; i <end; i++) {
for (int j = i + 1; j <=end; j++) {
if (x[i] > x[j]) {
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
}
public void prinArray(){
for(int i=1;i<55;i++){
if(i%17==1) {System.out.println();}
System.out.println(imgPath[i]);
}
}
/*public static void main(String[] args) {
CreateCard c= new CreateCard();
c.prinArray();
}
*
*/
}
服务器布牌DealCard.java
import java.util.StringTokenizer;
public class DealCard {
CreateCard cr;
public String user1="";
public String user2="";
public String user3="";
public String dipai="";
public String dizhu="";//地主底牌
public int dizhu1;//产生的地主
public int[] card;
public DealCard()
{
this.ChangeToString();
}
public void ChangeToString()
{
cr=new CreateCard();
card=cr.imgPath;
for(int i=1;i<card.length;i++)
{
if(i<18) user1+="|"+card[i];
else if(i<35) user2+="|"+card[i];
else if(i<52) user3+="|"+card[i];
else dipai+="|"+card[i];
}
}
public String SortString(String str)
{
String newString="";
int[] arr=new int[20];
StringTokenizer st = new StringTokenizer(str,"|");
int i=0;
while (st.hasMoreTokens())
{
arr[i]=Integer.parseInt(st.nextToken());
i++;
}
CreateCard.maoPao(arr,0,19);
for(int j=0;j<arr.length;j++)
{
newString+="|"+arr[j];
}
return newString;
}
public String CreatDizhu(int dizhu1) //根据提交的叫地主的情况随机产生地主{
switch(dizhu1)
{
case 1:
return SortString(user1+dipai);
case 2:
return SortString(user2+dipai);
case 3:
return SortString(user3+dipai);
default:
System.exit(-1);
}
return null;
}
public static void main(String[] args)
{
DealCard test=new DealCard();
System.out.println(er1);
System.out.println();
System.out.println();
}
}
客户端程序:
主类MainClinet.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import .Socket;
import javax.swing.*;
import java.util.*;
import java.io.*;
/**
*
* @author Administrator
*/
public class MainClient extends JFrame implements ActionListener,Runnable,MouseListener {
Socket socket;
JTextField textTalk;
JComboBox listOnline; //在线用户列表
boolean ischupai=false;//标记是否出牌
JLabel right=new JLabel("当前牌数为"+17);//标记剩余牌数
JLabel left=new JLabel("当前牌数为"+17);//标记剩余牌数
JLabel current=new JLabel("当前牌数为"+17);//标记剩余牌数
JLabel right1=new JLabel();//标记剩余牌数
JLabel left1=new JLabel();//标记剩余牌数
JLabel current1=new JLabel();//标记剩余牌数
JLabel[] labplayer={new JLabel(""),new JLabel(""),new JLabel("")};
JButton start =new JButton("开始");
JButton yes =new JButton("叫地主");
JButton no =new JButton("不叫");
Compare compare=new Compare();
DealCard music=new DealCard();
JLabel[] labdipai=new JLabel[3];
private JTextArea jta = new JTextArea();
private JTextArea jta1 = new JTextArea();
private OutputStream toServer;
private PrintWriter out;
private BufferedReader in;
private String Id;
private String Username;
private String[] Users=new String[3];
Thread th=new Thread(this,"listen");
ArrayList PlayCard=new ArrayList();//记录要出的牌
ArrayList PlayCard1=new ArrayList();//记录上个玩家出牌ArrayList PlayCard2=new ArrayList();//记录上一个玩家出牌public static ArrayList RecordCard=new ArrayList();//记录玩家出牌ArrayList LeftCard=new ArrayList();//记录剩下的牌
DealLabel[] labelPic=new DealLabel[20];
JLabel[] lab=new JLabel[20];
JLabel[] labr1=new JLabel[20];
JLabel[] labr2=new JLabel[20];
JLabel[] labl1=new JLabel[20];
JLabel[] labl2=new JLabel[20];
private JButton ibLeftSec;
private JButton isbuchuR;
private JButton isbuchuL;
private Thread time, clocker;
public static long startTime, endTime;
long nowTime, leftTime, leftSec, leftMin;
boolean flag = false;
String msg;
String Command;
private String last;
private String dizhu;
public int[] Arr;
public int loc=760; //当前玩家出牌的相对位置
public int locr=580; //当前玩家出牌的相对位置
public int locl1=580; //当前玩家出牌的相对位置
public int locl2=500; //当前玩家出牌的相对位置
private JButton buttonTalk;
String IP;//服务器IP地址
public MainClient(String name,String IP)
{
this.setTitle("玩家"+name);
ername=name;
this.IP=IP;
this.setLayout(null);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setResizable(false);
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
init();
getContentPane().setBackground(Color.GREEN);
new Thread(new Runnable() {
public void run() {
int i=1;
while(true){
new Sound().play("b"+String.valueOf(i));
i++;
if(i>5) i=1;
try{
Thread.sleep(32000);
}
catch(Exception e) {
}
}
}
}).start();
this.Bupai();
clocker = new Thread(this, "clock");
clocker.start();
time = new Thread(this, "ti");
time.start();
this.setVisible(true);
int width = (int)screensize.getWidth();
int height = (int)screensize.getHeight();
int mainloc=width/2-643;
if(mainloc<10) this.setLocation(50, 0);
else this.setLocation(mainloc, 0);
this.setSize(1286,718); //根据我的电脑设计的大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init()
{
for(int i=0;i<3;i++)
{
labplayer[i].setSize(80, 40);
this.add(labplayer[i]);
}
buttonTalk = new JButton("发送");
buttonTalk.setSize(60, 30);
buttonTalk.setLocation(1080, 570); buttonTalk.setFocusable(false); buttonTalk.addActionListener(this);
this.add(buttonTalk);
isbuchuR= new JButton("不出") ; isbuchuR.setFocusable(false); isbuchuR.setEnabled(false);
isbuchuR.setBackground(Color.red); isbuchuR.setBounds(120,300, 62, 40); isbuchuL= new JButton("不出") ; isbuchuL.setFocusable(false);
isbuchuL.setEnabled(false);
isbuchuL.setBackground(Color.red); ibLeftSec = new JButton("");
isbuchuL.setBounds(900,300, 62, 40); ibLeftSec.setFocusable(false); ibLeftSec.setEnabled(false);
ibLeftSec.setBackground(Color.red);
this.add(ibLeftSec);
isbuchuR.setVisible(false);
isbuchuL.setVisible(false);
this.add(isbuchuR);
this.add(isbuchuL);
listOnline=new JComboBox(); listOnline.addItem("All");
listOnline.setLocation(1160, 600); listOnline.setSize(58, 30);
this.add(listOnline);
JLabel labtalk=new JLabel("玩家聊天记录"); labtalk.setBounds(1150, 540,80, 40);
this.add(labtalk);
JLabel labre=new JLabel("消息接受者"); labre.setBounds(1080, 600,80, 40);
this.add(labre);
textTalk = new JTextField(20);
textTalk.setSize(120, 30);
textTalk.setLocation(1150,570); textTalk.addActionListener(this);
this.add(textTalk);
this.add(listOnline);
JLabel lab1=new JLabel("玩家发送消息");
JLabel lab2=new JLabel("<html>系<br>统<br>消<br>息<br>区<br>"); lab1.setBounds(1150, 20,80, 40);
lab2.setBounds(5, 0,40,100);
right.setBounds(890, 270, 120, 40);
left.setBounds(130, 270, 120, 40);
current.setBounds(560, 500, 120, 40);
right1.setBounds(860, 270, 120, 40);
left1.setBounds(100, 270, 120, 40);
current1.setBounds(530, 500, 120, 40);
this.add(lab1);
this.add(lab2);
this.add(right);
this.add(left);
this.add(current);
this.add(right1);
this.add(left1);
this.add(current1);
jta.setLocation(25, 5);
jta.setSize(240, 100);
jta.setVisible(true);
jta1.setLocation(900, 5);
jta1.setSize(140, 600);
jta1.setVisible(true);
start.setSize(60, 40);
start.setLocation(540, 470);
start.setFocusable(false);
start.setVisible(true);
start.setActionCommand("start");
start.addActionListener(this);
yes.setSize(73, 40);
yes.setLocation(450, 470);
yes.setFocusable(false);
yes.setVisible(false);
yes.setActionCommand("yes");
yes.addActionListener(this);
no.setSize(73, 40);
no.setLocation(650, 470);
no.setFocusable(false);
no.setVisible(false);
no.setActionCommand("no");
no.addActionListener(this);
jta.setEditable(true); //设置可编辑
JScrollPane jsp = new JScrollPane(jta); //添加滚动条
jsp.setBounds(25,0,240,100); //设置JScrollPane 宽100,高200
jta1.setEditable(true); //设置可编辑
JScrollPane jsp1 = new JScrollPane(jta1); //添加滚动条
jsp1.setBounds(1110,50,150,500); //设置JScrollPane 宽100,高200
this.add(jsp); //将组件加入容器
this.add(jsp1); //将组件加入容器
this.add(start);
this.add(yes);
this.add(no);
}
@Override
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if (e.getSource() instanceof JTextField)
{
String s =textTalk.getText();
if(s.length()>0)
{
java.text.DateFormat format1 = new java.text.SimpleDateFormat("HH:mm:ss");
String strTime = format1.format(new Date());
strTime="("+strTime+")";
jta1.append("玩家"+Id+": "+s+strTime);
out.println("talk|"+s+"|"+Id+"|"+listOnline.getSelectedItem().toString());
s = "";
}
textTalk.setText("");
}
if(e.getSource()==buttonTalk)
{
String s =textTalk.getText();
if(s.length()>0)
{
java.text.DateFormat format1 = new java.text.SimpleDateFormat("HH:mm:ss");
String strTime = format1.format(new Date());
strTime="("+strTime+")";
jta1.append(ername+": "+s+strTime+"\n");
out.println("talk|"+s+"|"+Id+"|"+listOnline.getSelectedItem().toString());
s="";
}
textTalk.setText("");
}
if(cmd.equals("start"))
{
th.start();
start.setVisible(false);
start.setText("重新开局");
start.setSize(100, 40);
start.setActionCommand("restart");
}
else if(cmd.equals("yes"))
{
out.println("21|"+Id+"|yes");
yes.setText("出牌");
no.setText("不出");
no.setActionCommand("buchu");
yes.setActionCommand("chu");
yes.setVisible(false);
no.setVisible(false);
}
else if(cmd.equals("no"))
{
out.println("21|"+Id+"|no");
yes.setText("出牌");
no.setText("不出");
no.setActionCommand("buchu");
yes.setActionCommand("chu");
yes.setVisible(false);
no.setVisible(false);
}
else if(cmd.equals("chu")) //按键出牌
{
this.Chupai();
}
else if(cmd.equals("buchu"))
{
if(PlayCard2.isEmpty()&&PlayCard1.isEmpty())
{
JOptionPane.showMessageDialog(null,"必须出牌","警告",JOptionPane.WARNING_MESSAGE);
}
else
{
PlayCard.clear();
for(int i=0;i<LeftCard.size();i++)
{
Integer p=(Integer)(LeftCard.get(i));
int k2=p.intValue();
labelPic[k2].setBounds(loc-(i+1)*25, 540, 80, 100);
labelPic[k2].sign=false;
}
String send="22|"+Id+"|"+LeftCard.size();
out.println(send);
yes.setVisible(false);
no.setVisible(false);
isbuchuL.setVisible(false);
isbuchuL.setVisible(false);
ischupai=false;
new Sound().play("buyao"+(int)(Math.random()*4+1));
}
}
else if(cmd.equals("restart"))
{
out.println("20|"+Id+"|start");
start.setVisible(false);
for(int i=0;i<20;i++)
{
if(i<3)
labdipai[i].setIcon(null);
lab[i].setIcon(null);
labl1[i].setIcon(null);
labl2[i].setIcon(null);
labr1[i].setIcon(null);
labr2[i].setIcon(null);
labelPic[i].setIcon(null);
labr1[i].setBounds(1000,locr-i*25, 80, 100);
labl1[i].setBounds(20,locl1-i*25, 80, 100);
labelPic[i].setBounds(loc-(i+1)*25, 540, 80, 100);
if(i<17)
{
labr1[i].setIcon(new
ImageIcon(getClass().getResource("./tupian/"+0+".jpg")));
labl1[i].setIcon(new
ImageIcon(getClass().getResource("./tupian/"+0+".jpg")));
}
}
LeftCard.clear();
PlayCard.clear();
PlayCard1.clear();
yes.setActionCommand("yes");
yes.setText("叫地主");
no.setActionCommand("no");
no.setText("不叫");
current.setText("当前牌数为17");
left.setText("当前牌数为17");
right.setText("当前牌数为17");
isbuchuL.setVisible(false);
isbuchuR.setVisible(false);
current1.setText("");
left1.setText("");
right1.setText("");
ischupai=false;
}
}
public void connection(String name)
{
try
{
//socket = new Socket("222.27.243.169",8189);
socket = new Socket(IP,8189);
toServer = socket.getOutputStream();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(toServer, true);
out.println(name);
while(true)
{
String s = in.readLine();
if(s!=null)
{
if(s.substring(0,1).endsWith(" "))
jta.append(s+"\n");
else
DealMsg(s);
}
s = null;
}
}
catch(IOException e)
{
jta.append("Server doesn't start"+"\n");
System.exit(-1);
}
}
@Override
public void run()
{
if(Thread.currentThread()==th)
{
this.connection(Username);
}
Calendar now;
while(((Thread.currentThread()).getName()).equals("clock"))
{
now = Calendar.getInstance();
nowTime = now.getTime().getTime();
}
while(((Thread.currentThread()).getName()).equals("ti"))
while(flag)
{
leftTime = endTime - nowTime;
leftSec = leftTime/1000;
leftMin = leftTime/(60*1000);
ibLeftSec.setText(leftSec+"秒");
if(leftSec == 0)
{
flag = false;
JOptionPane.showMessageDialog(this, "超时!\n时间到", "超时" , JOptionPane.OK_OPTION);
break;
}
}
}
public void Bupai()
{
for(int i=0;i<20;i++)
{
labelPic[i]=new DealLabel();
lab[i]=new JLabel();
labl1[i]=new JLabel();
labl2[i]=new JLabel();
labr1[i]=new JLabel();
labr2[i]=new JLabel();
labelPic[i].setBounds(loc-(i+1)*25, 540, 80, 100);
lab[i].setBounds(loc-i*25,420, 80, 100);
labr1[i].setBounds(1000,locr-i*25, 80, 100);
labr2[i].setBounds(930-i*25,300, 80, 100);//左边外面
labl1[i].setBounds(20,locl1-i*25, 80, 100);
labl2[i].setBounds(locl2-i*25,300, 80, 100);//右边外面if(i<3)
{
labdipai[i]=new JLabel();
labdipai[i].setBounds(600-i*40, 5, 80, 100);
this.add(labdipai[i]);
}
if(i<17)
{
labr1[i].setIcon(new
ImageIcon(getClass().getResource("./tupian/"+0+".jpg")));
labl1[i].setIcon(new
ImageIcon(getClass().getResource("./tupian/"+0+".jpg")));
}
labelPic[i].setFocusable(false);
labelPic[i].sign=false;
labelPic[i].addMouseListener(this);
this.add(lab[i]);
this.add(labr1[i]);
this.add(labr2[i]);
this.add(labl1[i]);
this.add(labl2[i]);
this.add(labelPic[i]);
}
}
@Override
public void mouseClicked(MouseEvent e)
{
if(!e.isMetaDown())
{//鼠标没有右击,已经左击
if(!PlayCard.isEmpty())
PlayCard.clear();
for(int i=0;i<LeftCard.size();i++)
{
Integer p=(Integer)(LeftCard.get(i));
int k=p.intValue();
if(e.getSource()==labelPic[k])
{
if(!labelPic[k].sign)
{
labelPic[k].setBounds(loc-(i+1)*25, 520, 80, 100);
labelPic[k].sign=true;
}
else
{
labelPic[k].setBounds(loc-(i+1)*25, 540, 80, 100);
labelPic[k].sign=false;
}
}
}
}
else //右键出牌
{
this.Chupai();
}
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
public void AnMsg(String msg) //解析消息
{
StringTokenizer st = new StringTokenizer(msg,"|");
int i=0;
Arr=new int[20];
while (st.hasMoreTokens())
{
if(i==0)
Command=st.nextToken();
else if(i==1)
last=st.nextToken();
else
Arr[i-2]=Integer.parseInt(st.nextToken());
i++;
}
}
public String ChangeVector(ArrayList v)
{ //将向量封装成字符串
String RealMsg="";
for(int i=0;i<v.size();i++)
{
RealMsg+="|"+v.get(i);
}
return RealMsg;
}
public void loadImg()
{
LeftCard.clear();
System.out.println("接收到的牌:");
for(int i=0;i<20;i++)
{
labelPic[i].setIcon(null);
labelPic[i].sign=false;
if(Arr[i]!=0)
{
labelPic[i].setIcon(new
ImageIcon(getClass().getResource("./tupian/"+Arr[i]+".jpg")));
System.out.print(" "+Arr[i]);
labelPic[i].setVisible(true);
labelPic[i].number=Arr[i];
LeftCard.add(i);
}
else
{
labelPic[i].setIcon(null);
}
labelPic[i].setBounds(loc-(i+1)*25, 540, 80, 100);
}
System.out.println();
System.out.println();
System.out.println();
System.out.println("向量Leftcard"+LeftCard);
System.out.println();
System.out.println();
if(LeftCard.size()!=17&&LeftCard.size()!=20)
{
// JOptionPane.showMessageDialog(null, "发牌有误,重新发牌,请等待");//未处理
out.println("error|"+this.Id);
}
}
public void DealMsg(String s)
{
StringTokenizer st = new StringTokenizer(s,"|");
String command=st.nextToken();
if(command.equals("ID"))
{
Users[0]=st.nextToken();
Users[1]=st.nextToken();
Users[2]=st.nextToken();
for(int i=0;i<3;i++)
{
if(ername.equals(Users[i]))
{
this.Id=i+1+"";
labplayer[0].setText(ername);
labplayer[0].setLocation(560, 470);
}
else listOnline.addItem(Users[i]);
}
int Idnum=Integer.parseInt(Id);
labplayer[1].setText(Users[Idnum%3]);
labplayer[1].setLocation(890, 250);
labplayer[2].setText(Users[((Idnum%3)+1)%3]);
labplayer[2].setLocation(130, 250);
}
if(command.equals("11"))
{
this.AnMsg(s);
this.loadImg();
yes.setVisible(true);
no.setVisible(true);
}
else if(command.equals("12"))
{
last=st.nextToken();
int next=Integer.parseInt(last)%3+1;
dizhu=last;
if(last.equals(Id))
{
this.AnMsg(s);
this.loadImg();
yes.setVisible(true);
no.setVisible(true);
jta.append("您,"+Users[Integer.parseInt(Id)-1]+"是地主\n");
jta.append("地主:"+Users[Integer.parseInt(Id)-1]+"正在出牌\n");
this.addtime(540, 430);
ischupai=true;
PlayCard1.clear();
PlayCard2.clear();
current1.setText("地主");
current.setText("当前牌数为"+LeftCard.size());
}
else if(next==Integer.parseInt(Id))
{
jta.append(last+"是地主\n");
left1.setText("地主");
left.setText("当前牌数为"+20);
this.addtime(200, 200);。

相关文档
最新文档