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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
public class Window extends JFrame{
/**
* Main JPanel
*/
private JPanel container = new JPanel();
/**
* Message area
*/
private JLabel screen = new JLabel();
/**
* table light
*/
private Light light[][] = new Light[5][5];
/**
* button who allows to configure the start of game
*/
private JButton configure = new JButton("Configure the lights");
/**
* button who allows to start the game
*/
private JButton play = new JButton("Play");
/**
* button who allows to place random lights
*/
private JButton random = new JButton("Random configuration");
/**
* stop button for stop the game
*/
private JButton stop = new JButton("Stop");
private boolean config = false;
private boolean rand = false;
private boolean playing = false;
/**
* creation of menu bar
*/
private JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File"),
edition = new JMenu("Edition"),
About = new JMenu("About");
/**
* construct a window
* @param controller the controller of the window
*/
public Window(){
this.setSize(500, 500);
this.setTitle("Game of life");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(false);
initComponents();
this.setContentPane(container);
this.setVisible(true);
}
/**
* Initialization of graphical components
*/
private void initComponents(){
//for the message
Font font = new Font("Arial", Font.BOLD, 20);
screen = new JLabel("Game of life");
screen.setFont(font);
screen.setForeground(Color.white);
JPanel panScreen = new JPanel();
panScreen.setPreferredSize(new Dimension(480, 40));
JPanel menuButton = new JPanel();
menuButton.setPreferredSize(new Dimension(480, 100));
JPanel lightJpanel = new JPanel();
lightJpanel.setPreferredSize(new Dimension(300, 300));
//listeners for menu buttons
MenuListener menuListener = new MenuListener();
LightListener lightListener = new LightListener();
lightJpanel.setLayout(new GridLayout(5,5,0,0));
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
light[i][j] = new Light(i,j);
lightJpanel.add(light[i][j]);
light[i][j].addActionListener(lightListener);
}
}
//size button
stop.setPreferredSize(new Dimension(70,40));
play.setPreferredSize(new Dimension(70, 40));
random.setPreferredSize(new Dimension(200, 40));
configure.setPreferredSize(new Dimension(200, 40));
//add listener
play.addActionListener(menuListener);
random.addActionListener(menuListener);
configure.addActionListener(menuListener);
stop.addActionListener(menuListener);
//add buttons to JPanel menuButton
menuButton.add(configure);
menuButton.add(random);
menuButton.add(play);
menuButton.add(stop);
stop.setEnabled(false);
//decoration JLabel screen
panScreen.add(screen);
panScreen.setBackground(Color.blue);
panScreen.setBorder(new javax.swing.border.BevelBorder(BevelBorder.RAISED));
Border borderLine = BorderFactory.createLineBorder(Color.BLACK);
panScreen.setBorder(borderLine);
//test jpanel
lightJpanel.setBackground(Color.CYAN);
menuButton.setBackground(Color.black);
container.setBackground(Color.green);
//positioning different JPanel to main JPanel
container.add(panScreen);
container.add(menuButton);
container.add(lightJpanel);
}
/**
* Listener of menu button
*/
class MenuListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//different conditions that get the light who is clicked
if(((JButton)e.getSource()).getText() == "Configure the lights") {
config = true;
}
if(((JButton)e.getSource()).getText() == "Play") {
config = false;
rand = false;
playing = true;
play.setEnabled(false);
configure.setEnabled(false);
random.setEnabled(false);
stop.setEnabled(true);
}
if(((JButton)e.getSource()).getText() == "Stop") {
playing = false;
configure.setEnabled(true);
random.setEnabled(true);
play.setEnabled(true);
stop.setEnabled(false);
}
if(((JButton)e.getSource()).getText() == "Random configuration") {
if(!playing){
rand = true;
//put all the lights turn off
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
light[i][j].setOff();
}
}
int x, y;
int nbLight = (int)(Math.random()*25);
//put lights lit randomly
for(int i = 0; i < nbLight; i++){
for(int j = 0; j < nbLight; j++){
x = (int)(Math.random()*5);
y = (int)(Math.random()*5);
light[x][y].setOn();
}
}
}
}
}
}
/**
* Listener of light
*/
class LightListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(config)
((Light) e.getSource()).changeState();
if(playing){
((Light) e.getSource()).changeState();
int x = ((Light) e.getSource()).getX();
int y = ((Light) e.getSource()).getY();
light[x-1][y].changeState();
light[x+1][y].changeState();
light[x][y-1].changeState();
light[x][y+1].changeState();
}
}
}
} |
Partager