标签:
package SendCardcopy; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Action; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class DealCard { SendCardThread sendcardthread; ReceiveCardThread[] receivecardthreadds; public DealCard(int cardMax, int personNumber) { CardBuffer cardbuffer = new CardBuffer(personNumber); sendcardthread = new SendCardThread(cardbuffer, cardMax); String[] titles = { "北", "东", "南", "西" }; int[] x = { 300, 550, 300, 50 }; int[] y = { 200, 320, 440, 320 }; receivecardthreadds = new ReceiveCardThread[personNumber]; for (int i = 0; i < personNumber; i++) { receivecardthreadds[i] = new ReceiveCardThread(sendcardthread,cardbuffer, i, titles[i], x[i], y[i]); } } public static void main(String[] args) { new DealCard(52, 4);// 52张牌,4个人收 } } class CardBuffer { private int personNumber;// 多少人接收 private int value; private int order;// 接牌的循序 private boolean isEmpty;// 旗杆标志 public CardBuffer(int personNumber) { this.personNumber = personNumber; isEmpty = true; order = 0; } public synchronized void put(int value) { while (!isEmpty) {// 要放入数据,必须buffer为空 try { this.wait(); } catch (InterruptedException e) { System.out.println("buffer不为空,等待异常!!"); } } this.value = value; isEmpty = !isEmpty; this.notifyAll(); } public synchronized int get(int order) { while (isEmpty || this.order != order) {// 只有是有序的人才能接收 try { this.wait(); } catch (InterruptedException e) { System.out.println("buffer为空,等待异常!!"); } } isEmpty = !isEmpty; this.notifyAll(); this.order = (this.order + 1) % this.personNumber;// 有序发牌 return value; } } class SendCardThread extends JFrame implements Runnable { private CardBuffer buffer; private int cardMax; private int[] cardNumber;// 0 1 2 3 4 5 .... 51 JTextArea text_area; JPanel pnl; public SendCardThread(CardBuffer buffer, int cardMax) { super("^_^友谊牌^_^"); this.buffer = buffer; this.cardMax = cardMax; cardNumber = new int[cardMax]; for (int i = 0; i < cardNumber.length; i++) {// 初始化牌 cardNumber[i] = i; } setBounds(800, 200, 400, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); text_area = new JTextArea(); text_area.setSize(100, 300); text_area.setEditable(false); text_area.setLineWrap(true); pnl = new JPanel(); pnl.setSize(200, 300); pnl.setLayout(new FlowLayout(FlowLayout.LEFT)); getContentPane().add(pnl); getContentPane().add(new JScrollPane(text_area),BorderLayout.EAST); Thread t = new Thread(this); t.setPriority(Thread.MAX_PRIORITY); t.start(); } public int[] shuffle(int[] cleancard) { if(cleancard==null||cleancard.length==0){ return null; } int[] mycleancard = new int[cleancard.length]; for (int i = 0; i < mycleancard.length; i++) { mycleancard[i] = cleancard[i]; } int temp = 0, rand; for (int i = 0; i < mycleancard.length; i++) {// 第i个的元素与第rand个的元素 交换 rand = (int) (Math.random() * 100) % cardMax;// 交换位置,错序 temp = mycleancard[rand]; mycleancard[rand] = mycleancard[i]; mycleancard[i] = temp; } return mycleancard; } @Override public void run() { int[] mycard = shuffle(cardNumber); for (int i = 0; i < mycard.length; i++) { buffer.put(mycard[i]); } for (int i = 0; i < 4; i++) {// 使4个接收线程关闭 buffer.put(-1); } } } class ReceiveCardThread extends JFrame implements Runnable, ActionListener { private CardBuffer buffer; public int order; private Color[] colors; private JPanel pnl; public JButton[] btns; private int count = 0; SendCardThread sendcardthread; public boolean isChange; public ReceiveCardThread(SendCardThread sendcardthread,CardBuffer buffer, int order, String title, int x, int y) { super(title); setLocation(x, y); setSize(220, 230); setDefaultCloseOperation(EXIT_ON_CLOSE); this.sendcardthread = sendcardthread; this.buffer = buffer; this.order = order; this.buffer = buffer; pnl = new JPanel(); pnl.setLayout(new FlowLayout(FlowLayout.LEFT)); pnl.setBackground(Color.white); getContentPane().add(pnl); colors = new Color[] {Color.LIGHT_GRAY,Color.RED,Color.GRAY,Color.pink}; btns = new JButton[13]; for (int i = 0; i < 13; i++) { btns[i] = new JButton(); btns[i].addActionListener(this); } setVisible(true); new Thread(this).start(); } @Override public void run() { synchronized (this) { while (true) { int value = buffer.get(order); if (value == -1) { count = 0; return; } btns[count].setBackground(colors[value/13]); if (value % 13 == 0) { btns[count].setText(String.format("%4d", 13)); } if(value % 13 >=10){ btns[count].setText(String.format("%4d", value%13)); }else { btns[count].setText(" "+String.format("%4d", value%13)); } pnl.add(btns[count]); count++; } } } @Override public void actionPerformed(ActionEvent e) { for (int i = 0; i < 13; i++) { if(e.getSource() == btns[i]){ String str = btns[i].getText(); sendcardthread.text_area.append(str); JButton btn = new JButton(); btn.setBackground(btns[i].getBackground()); btn.setText(btns[i].getText()); sendcardthread.pnl.add(btn); if(isChange){ sendcardthread.setSize( 401, 300); isChange = !isChange; }else{ sendcardthread.setSize( 402, 300); isChange = !isChange; } } } } }
标签:
原文地址:http://blog.csdn.net/hncu1306602liuqiang/article/details/46390351