码迷,mamicode.com
首页 > 其他好文 > 详细

发牌程序(三)

时间:2015-06-07 09:46:22      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

public class DealCard extends JFrame implements WindowListener {
	private static final long serialVersionUID = 1L;
	ReceiveCard[] receivecards;
	PipedInputStream pipedintputs[];
	PipedOutputStream pipedoutputs[];
	JPanel uppanel, downpanel, panels[];
	JTextArea textArea;
	JSplitPane splitpanel;
	JButton[] btns;

	public DealCard(int numberOfCard, int numberOfPerson) throws IOException {
		super("九九牌~~~");
		Dimension dms = getToolkit().getScreenSize();
		setBounds(0, 0, (int) dms.getWidth(), (int) dms.getHeight());
		addWindowListener(this);
		downpanel = new JPanel();
		downpanel.setLayout(new GridLayout(3, 3));
		btns = new JButton[0];

		pipedintputs = new PipedInputStream[numberOfPerson];
		pipedoutputs = new PipedOutputStream[numberOfPerson];
		for (int i = 0; i < numberOfPerson; i++) {// 管道套结
			pipedintputs[i] = new PipedInputStream();
			pipedoutputs[i] = new PipedOutputStream(pipedintputs[i]);
		}
		uppanel = new SenderCard(pipedoutputs, numberOfCard);// 发牌线程
		uppanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		receivecards = new ReceiveCard[numberOfPerson];// 接牌线程
		String titles[] = { "北", "西", "东", "南" };
		panels = new JPanel[5];
		for (int i = 0; i < 4; i++) {// new出四个接收线程
			downpanel.add(panels[i] = new JPanel());
			receivecards[i] = new ReceiveCard(pipedintputs[i], titles[i], this);
			downpanel.add(receivecards[i]);
		}
		textArea = new JTextArea();
		textArea.setLineWrap(true);
		panels[2].setLayout(new BorderLayout());
		panels[2].add(new JScrollPane(textArea));
		textArea.setEditable(false);

		splitpanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT, uppanel,
				downpanel);
		splitpanel.setDividerLocation(200);
		getContentPane().add(splitpanel);
		setVisible(true);
	}

	public void pulsButton(JButton btn) {
		JButton[] temp = new JButton[btns.length + 1];
		for (int i = 0; i < btns.length; i++) {
			temp[i] = btns[i];
		}
		temp[btns.length] = btn;
		btns = temp;
		if (btns.length >= 50) {
			uppanel.removeAll();
			btns = new JButton[0];
		}
		uppanel.add(btn);
	}

	public static void main(String[] args) {
		try {
			new DealCard(52, 4);
		} catch (IOException e) {
		}
	}

	public void windowOpened(WindowEvent e) {
	}

	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}

	public void windowClosed(WindowEvent e) {
	}

	public void windowIconified(WindowEvent e) {
	}

	public void windowDeiconified(WindowEvent e) {
	}

	public void windowActivated(WindowEvent e) {
	}

	public void windowDeactivated(WindowEvent e) {
	}

}

class SenderCard extends JPanel implements Runnable {
	private static final long serialVersionUID = 1L;
	PipedOutputStream[] pipOutputStreams;
	int numberOfCard;
	int[] number;

	public SenderCard(PipedOutputStream[] pipOutputStreams, int numberOfCard) {
		setBorder(new TitledBorder("^_^牌面控制^_^"));
		this.pipOutputStreams = pipOutputStreams;
		this.numberOfCard = numberOfCard;
		number = new int[numberOfCard];
		for (int i = 0; i < numberOfCard; i++) {
			number[i] = (i + 1);
		}
		shuffle(number);
		Thread t = new Thread(this);
		t.setPriority(Thread.MAX_PRIORITY);
		t.start();
	}

	private void shuffle(int[] number2) {
		int num = 0;
		for (int i = 0; i < number2.length; i++) {
			int rand = (int) (Math.random() * 100) % number2.length;
			num = number2[rand];
			number2[rand] = number2[i];
			number2[i] = num;
		}
	}

	public void run() {
		DataOutputStream douts[] = new DataOutputStream[pipOutputStreams.length];
		for (int i = 0; i < douts.length; i++) {
			douts[i] = new DataOutputStream(pipOutputStreams[i]);
		}
		try {
			int value = 0;
			while (value < numberOfCard) {
				for (int i = 0; value < numberOfCard && i < douts.length; i++) {
					douts[i].writeInt(number[value++]);
				}
			}
		} catch (IOException e) {
		}
		try {
			for (int i = 0; i < douts.length; i++) {
				pipOutputStreams[i].close();
				douts[i].close();
			}
		} catch (IOException e) {
		}
	}

}

class ReceiveCard extends JPanel implements Runnable, ActionListener {
	private static final long serialVersionUID = 1L;
	PipedInputStream pipedInputStream;
	String[] myNumberCard = { "  K  ]", "  1  ]", "  2  ]", "  3  ]", "  4  ]",
			"  5  ]", "  6  ]", "  7  ]", "  8  ]", "  9  ]", "  10  ]",
			"  J  ]", "  Q  ]" };
	Color colors[] = { Color.LIGHT_GRAY, Color.red, Color.LIGHT_GRAY, Color.red };// ^黑—*红——#梅——@花片
	String[] flags = { "^[", "*[", "#[", "@[" };
	JButton btns[];
	DealCard dealcard;
	int[] number;
	boolean stamp;

	public ReceiveCard(PipedInputStream pipedInputStream, String title,
			DealCard dealcard) {
		this.dealcard = dealcard;
		setBorder(new TitledBorder(title));
		this.pipedInputStream = pipedInputStream;
		setSize(250, 150);
		setLayout(new GridLayout(4, 4));
		btns = new JButton[13];
		for (int i = 0; i < btns.length; i++) {
			btns[i] = new JButton();
			btns[i].addActionListener(this);
			add(btns[i]);
		}
		number = new int[13];
		new Thread(this).start();
	}

	public void run() {
		DataInputStream din = new DataInputStream(this.pipedInputStream);
		int count = 0;
		while (true) {
			try {
				int temp = din.readInt();
				for (int i = 0; i < 13; i++) {
					if (temp % 13 == i) {
						number[i] = temp;
						btns[count].setBackground(colors[(temp - 1) / 13]);
						btns[count].setText(flags[(temp - 1) / 13]
								+ myNumberCard[temp % 13]);
					}
				}
				count++;
			} catch (IOException e) {
				break;
			}
		}
		try {
			pipedInputStream.close();
			din.close();
		} catch (IOException e) {
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JButton) {
			JButton btn = new JButton();
			for (int i = 0; i < 13; i++) {
				if (e.getSource() == btns[i]) {
					btn.setBackground(btns[i].getBackground());
					btn.setText(btns[i].getText());
					dealcard.textArea.append("" + number[i] + "_");
					break;
				}
			}
			dealcard.pulsButton(btn);
			if (stamp) {
				dealcard.splitpanel.setDividerLocation(201);
			} else {
				dealcard.splitpanel.setDividerLocation(202);
			}
		}
	}

}

发牌程序(三)

标签:

原文地址:http://blog.csdn.net/hncu1306602liuqiang/article/details/46390365

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!