-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromoter.java
77 lines (63 loc) · 1.78 KB
/
Promoter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package boardgame.gui;
import boardgame.pieces.*;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Promoter extends JFrame {
private JPanel pieces;
public PieceButton queen;
public PieceButton rook;
public PieceButton bishop;
public PieceButton knight;
private volatile PieceName promotion;
public Promoter() throws HeadlessException {
// TODO Auto-generated constructor stub
}
public Promoter(Color color) {
Queen q = new Queen(color);
Rook r = new Rook(color);
Bishop b = new Bishop(color);
Knight k = new Knight(color);
queen = PieceButton.createPieceButton(q);
rook = PieceButton.createPieceButton(r);
bishop = PieceButton.createPieceButton(b);
knight = PieceButton.createPieceButton(k);
pieces = new JPanel();
pieces.setLayout(new GridLayout(2, 2));
pieces.add(queen);
pieces.add(rook);
pieces.add(bishop);
pieces.add(knight);
add(pieces);
setMinimumSize(new Dimension (150, 170));
promotion = PieceName.PAWN;
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setLocation(200, 200);
}
public synchronized PieceName promote() {
while (promotion == PieceName.PAWN) {
try {
System.out.println("Waiting in Thread: " + Thread.currentThread().getName());
wait();
}catch (InterruptedException e) {
e.printStackTrace();
}
}
return promotion;
}
public synchronized void setPromotion(PieceName pName) {
promotion = pName;
notifyAll();
}
public static PieceName getPromotionPiece(Color color) {
Promoter p = new Promoter(color);
p.setVisible(true);
PieceName prom = p.promote();
p.dispose();
return prom;
}
}