-
Notifications
You must be signed in to change notification settings - Fork 0
/
PMController.java
134 lines (115 loc) · 4.57 KB
/
PMController.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.concurrent.TimeUnit;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
//MVC model
//control class
public class PMController{
private PMView view; //mvc view instance
private PMModel model; //mvc model instance
//constructor
public PMController(PMView view, PMModel model) {
this.view = view;
this.model = model;
ButtonListenerCMD listener = new ButtonListenerCMD();
view.getCmdGo().addActionListener(listener);
view.getCmdClear().addActionListener(listener);
for(int i=0; i<model.getMATRIX_DIMENSION() ; i++)
for(int j=0; j<model.getMATRIX_DIMENSION() ; j++)
model.getMillingPanels()[i][j].addMouseListener(new MouseListenerCMD());
}//end of constructor
//get next update to view
public void updateView() {
System.out.println("-> updating matrix ");
for(int i=0; i<model.getMATRIX_DIMENSION() ; i++)
for(int j=0; j<model.getMATRIX_DIMENSION() ; j++)
if(model.getMillingPanels()[i][j].getNextUpdate())
model.getMillingPanels()[i][j].setColor(Color.black);
else model.getMillingPanels()[i][j].setColor(Color.white);
model.setBoardReady(false);
JOptionPane.showMessageDialog(null, "Phase completed!");
System.out.println("-> matrix updated");
view.repaint();
}//end of method
//buttons actions
public class ButtonListenerCMD implements ActionListener {
public void actionPerformed(ActionEvent e) {
//set game to play
if(e.getSource() == view.getCmdGo()) {
model.update(); //setting number of transitions and processes running concurrently
model.setBoardReady(true); //locking the board for changes
for(int i=0; i<model.getNUM_OF_TRANSITIONS(); i++)
{
model.millPhase();
updateView();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e1) { e1.printStackTrace();}
}
model.setBoardReady(false); //releasing the board again
JOptionPane.showMessageDialog(null, "transitions completed,\npress Clear to start over or Go to continue!");
}//end of go cmd
//clear board
if(e.getSource() == view.getCmdClear()) {
for(int i=0; i<model.getMATRIX_DIMENSION() ; i++)
for(int j=0; j<model.getMATRIX_DIMENSION() ; j++)
model.getMillingPanels()[i][j].clearPanel();
model.setBoardReady(false);
JOptionPane.showMessageDialog(null, "Clearing board!");
}//end of clear cmd
view.repaint(); //anyway
}//end of action preformed
}//end of button listner
//mose actions
private class MouseListenerCMD implements MouseListener {
// default constructor
MouseListenerCMD() { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
//reset panel to original color when exit
public void mouseExited(MouseEvent e) {
Object source = e.getSource();
if(source instanceof JPanel){
if(!model.isBoardReady())
{
millingPanel panelPressed = (millingPanel) source;
panelPressed.setColor(panelPressed.getColor());
}
}
view.repaint();
}
//change panel to temp color when visited with mouse pointer
public void mouseEntered(MouseEvent e) {
Object source = e.getSource();
if(source instanceof JPanel){
if(!model.isBoardReady())
{
millingPanel panelPressed = (millingPanel) source;
panelPressed.setBackground(Color.lightGray);;
}
}
view.repaint();
}
// change panel color to opposite color when clicked
public void mouseClicked(MouseEvent e)
{
Object source = e.getSource();
if(source instanceof JPanel){
if(!model.isBoardReady())
{
millingPanel panelPressed = (millingPanel) source;
if(panelPressed.getColor().equals(Color.black))
panelPressed.setColor(Color.white);
else panelPressed.setColor(Color.black) ;
}
}
view.repaint();
}
}//end of mouse listner
//show GUI
public void showMvc() { this.view.setVisible(true);} //i chose to show the view through the controller
}//end of class