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 = {"North", "East", "South", "West"};int[] x = {, 550, +, +};int[] y = {$, +, +,-};receivecardthrea DDS = new Receivecardthread[personnumber];for (int i = 0; i < Personnumber; i++) {receivecardthreadds[i] = new Receivec Ardthread (Sendcardthread,cardbuffer, I, Titles[i], x[i], y[i]);}} public static void Main (string[] args) {new DealcARD (52, 4);//52 cards, 4 people received}}class cardbuffer {private int personnumber;//How many people receive the private int value;private int order;//The sequential PR of the card Ivate Boolean isempty;//Flagpole flag public cardbuffer (int personnumber) {this.personnumber = Personnumber;isempty = True;order = 0;} Public synchronized void put (int value) {while (!isempty) {///To put data, must be null in buffer as try {this.wait ();} catch (Interruptedexcept Ion e) {System.out.println ("buffer is not empty, wait for an exception!! ");}} This.value = Value;isempty =!isempty;this.notifyall ();} public synchronized int gets (int order) {while (IsEmpty | | This.order! = order) {//Only the ordered person can receive the try {this.wait ();} catch (in Terruptedexception e) {System.out.println ("Buffer is empty, wait for exception!! ");}} IsEmpty =!isempty;this.notifyall (); This.order = (this.order + 1)% this.personnumber;//orderly licensing return value;} Class Sendcardthread extends JFrame implements Runnable {private cardbuffer buffer;private int cardmax;private int[] Cardn umber;//0 1 2 3 4 5 .... 51JTextArea Text_area; JPanel pnl;public sendcardthread (cardbuffer buffer, int cardmax) {Super ("^_^ Friendship card ^_^"); This.buffer = Buffer;this.cardmax = Cardmax;cardnumber = new Int[cardmax];for (int i = 0; i < card Number.length; i++) {//init card cardnumber[i] = i;} SetBounds, Setdefaultcloseoperation (exit_on_close); setvisible (true); Text_area = new JTextArea (); Text_area.setsize (+); text_area.seteditable (false); Text_area.setlinewrap (true);p nl = new JPanel (); Pnl.setsize (+);p nl.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-Elements exchange Rand = (int) with the element of the first Rand) (Math.random () * 10 0)% cardmax;//swap position, wrong order temp = MycleAncard[rand];mycleancard[rand] = mycleancard[i];mycleancard[i] = temp;} return Mycleancard;} @Overridepublic void Run () {int[] Mycard = Shuffle (Cardnumber); for (int i = 0; i < mycard.length; i++) {Buffer.put (Myca Rd[i]);} for (int i = 0; i < 4; i++) {//causes 4 receive threads to close Buffer.put (-1);}}} Class Receivecardthread extends JFrame implements Runnable, ActionListener {private cardbuffer buffer;public int ORDER;PR Ivate 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 (n); Setdefaultclos Eoperation (exit_on_close); this.sendcardthread = Sendcardthread;this.buffer = Buffer;this.order = Order;this.buffer = BUFFER;PNL = new JPanel ();p nl.setlayout (New FlowLayout (flowlayout.left));p Nl.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 <; i++) {btns[i] = new JButton (); btns [I].addactionlistener (this);} SetVisible (True); new Thread (This). Start (); @Overridepublic void Run () {synchronized (this) {while (true) {int value = Buffer.get (order); if (value = =-1) {count = 0;r Eturn;} Btns[count].setbackground (COLORS[VALUE/13]); if (value% = = 0) {Btns[count].settext (String.Format ("%4d"));} if ( Value% >=10) {Btns[count].settext (String.Format ("%4d", value%13));} else {btns[count].settext ("" +string.format ("%4d", value%13));} Pnl.add (Btns[count]); count++;}} @Overridepublic void actionperformed (ActionEvent e) {for (int i = 0; i <; 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); ischange =!ischange;} Else{sendcArdthread.setsize (402); ischange =!ischange;}}}}
Licensing Procedure (ii)