package SendCard; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; public class DealCard { public DealCard(int cardMax, int personNumber) { CardBuffer cardbuffer = new CardBuffer(personNumber); new SendCardThread(cardbuffer, cardMax).start(); String[] titles = { "北", "东", "南", "西" }; int[] x = { 300, 550, 300, 50 }; int[] y = { 200, 320, 440, 320 }; for (int i = 0; i < personNumber; i++) { new ReceiveCardThread(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 Thread { private CardBuffer buffer; private int cardMax; private int[] cardNumber;// 0 1 2 3 4 5 .... 51 public SendCardThread(CardBuffer buffer, int cardMax) { this.buffer = buffer; this.cardMax = cardMax; this.setPriority(MAX_PRIORITY); cardNumber = new int[cardMax]; for (int i = 0; i < cardNumber.length; i++) {// 初始化牌 cardNumber[i] = i; } } 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 { private CardBuffer buffer; private int order; private JTextArea text; private Color[] colors; public ReceiveCardThread(CardBuffer buffer, int order, String title, int x, int y) { super(title); setLocation(x, y); setSize(250, 100); setDefaultCloseOperation(EXIT_ON_CLOSE); this.buffer = buffer; this.order = order; this.buffer = buffer; text = new JTextArea(); text.setLineWrap(true); text.setEditable(false); getContentPane().add(text); colors = new Color[] {Color.BLACK,Color.RED,Color.GRAY,Color.pink}; setVisible(true); new Thread(this).start(); } @Override public void run() { while (true) { int value = buffer.get(order); int color = value/13; if(value==-1){ return ; } text.append(String.format("%4d", value)); try { Thread.sleep(30); } catch (InterruptedException e) { System.out.println("qwer"); } } } }
时间: 2024-12-20 02:51:09