Skip to content

Commit f08a69c

Browse files
author
Arun Prasaad
committed
Refactor creation of the tile setting buttons
1 parent 00a6d8e commit f08a69c

File tree

1 file changed

+90
-68
lines changed

1 file changed

+90
-68
lines changed

src/main/java/ihm/Editor.java

Lines changed: 90 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,14 @@ public Editor (int rowCount, int columnCount, String name) throws IOException {
7777
this.setLocationRelativeTo(null);
7878

7979
// Tool buttons
80-
JButton playerButton = new JButton(new ImageIcon("img/Joueur.jpg"));
81-
playerButton.setName(Component.PLAYER_BUTTON.name());
82-
83-
JButton backgroundButton = new JButton(new ImageIcon("img/Background.jpg"));
84-
backgroundButton.setName(Component.BACKGROUND_BUTTON.name());
85-
86-
JButton boxButton = new JButton(new ImageIcon("img/Caisse.jpg"));
87-
boxButton.setName(Component.BOX_BUTTON.name());
88-
89-
JButton boxOnTargetButton = new JButton(new ImageIcon("img/CaisseRangee.jpg"));
90-
boxOnTargetButton.setName(Component.BOX_ON_TARGET_BUTTON.name());
91-
92-
JButton playerOnTargetButton = new JButton(new ImageIcon("img/JoueurRangement.jpg"));
93-
playerOnTargetButton.setName(Component.PLAYER_ON_TARGET_BUTTON.name());
94-
95-
JButton wallButton = new JButton(new ImageIcon("img/Mur.jpg"));
96-
wallButton.setName(Component.WALL_BUTTON.name());
97-
98-
JButton targetButton = new JButton(new ImageIcon("img/Rangement.jpg"));
99-
targetButton.setName(Component.TARGET_BUTTON.name());
100-
101-
JButton emptyButton = new JButton(new ImageIcon("img/Vide.jpg"));
102-
emptyButton.setName(Component.EMPTY_BUTTON.name());
80+
createPlayerButton();
81+
createBackgroundButton();
82+
createBoxButton();
83+
createBoxOnTargetButton();
84+
createPlayerOnTargetButton();
85+
createWallButton();
86+
createTargetButton();
87+
createEmptyButton();
10388

10489
// Status label
10590
JLabel invalid_level = new JLabel("", SwingConstants.CENTER);
@@ -120,37 +105,9 @@ public Editor (int rowCount, int columnCount, String name) throws IOException {
120105
quit.setName(Component.QUIT_BUTTON.name());
121106
quit.setBounds(windowWidth + 20, 250, 110, 30);
122107

123-
playerButton.setOpaque(false);
124-
playerButton.setContentAreaFilled(false);
125-
playerButton.setBounds(windowWidth + 30, 0, 32, 32);
126-
127-
backgroundButton.setOpaque(false);
128-
backgroundButton.setContentAreaFilled(false);
129-
backgroundButton.setBounds(windowWidth + 30, 37, 32, 32);
130108

131-
boxButton.setOpaque(false);
132-
boxButton.setContentAreaFilled(false);
133-
boxButton.setBounds(windowWidth + 30, 74, 32, 32);
134109

135-
boxOnTargetButton.setOpaque(false);
136-
boxOnTargetButton.setContentAreaFilled(false);
137-
boxOnTargetButton.setBounds(windowWidth + 30, 111, 32, 32);
138110

139-
playerOnTargetButton.setOpaque(false);
140-
playerOnTargetButton.setContentAreaFilled(false);
141-
playerOnTargetButton.setBounds(windowWidth + 75, 0, 32, 32);
142-
143-
wallButton.setOpaque(false);
144-
wallButton.setContentAreaFilled(false);
145-
wallButton.setBounds(windowWidth + 75, 37, 32, 32);
146-
147-
targetButton.setOpaque(false);
148-
targetButton.setContentAreaFilled(false);
149-
targetButton.setBounds(windowWidth + 75, 74, 32, 32);
150-
151-
emptyButton.setOpaque(false);
152-
emptyButton.setContentAreaFilled(false);
153-
emptyButton.setBounds(windowWidth + 75, 111, 32, 32);
154111

155112
quit.addActionListener(new ActionListener() {
156113
@Override
@@ -270,23 +227,7 @@ else if (line.length() == columnCount) {
270227
}
271228
});
272229

273-
playerButton.addActionListener(_ -> content = TileType.WORKER_ON_FLOOR);
274-
backgroundButton.addActionListener(_ -> content = TileType.OUTSIDE);
275-
boxButton.addActionListener(_ -> content = TileType.UNSTORED_BOX);
276-
boxOnTargetButton.addActionListener(_ -> content = TileType.STORED_BOX);
277-
playerOnTargetButton.addActionListener(_ -> content = TileType.WORKER_IN_STORAGE_AREA);
278-
wallButton.addActionListener(_ -> content = TileType.WALL);
279-
targetButton.addActionListener(_ -> content = TileType.STORAGE_AREA);
280-
emptyButton.addActionListener(_ -> content = TileType.FLOOR);
281-
282-
this.add(playerButton);
283-
this.add(backgroundButton);
284-
this.add(boxButton);
285-
this.add(boxOnTargetButton);
286-
this.add(playerOnTargetButton);
287-
this.add(wallButton);
288-
this.add(targetButton);
289-
this.add(emptyButton);
230+
290231
this.add(save);
291232
this.add(quit);
292233
this.add(back);
@@ -298,10 +239,91 @@ else if (line.length() == columnCount) {
298239

299240
this.addMouseMotionListener(this);
300241
this.addMouseListener(this);
242+
this.addMouseMotionListener(this);
301243
this.pack();
302244
this.setVisible( true );
303245
}
304246

247+
private void createPlayerButton() {
248+
JButton button = new JButton(new ImageIcon("img/Joueur.jpg"));
249+
button.setName(Component.PLAYER_BUTTON.name());
250+
button.setOpaque(false);
251+
button.setContentAreaFilled(false);
252+
button.setBounds(windowWidth + 30, 0, 32, 32);
253+
button.addActionListener(_ -> content = TileType.WORKER_ON_FLOOR);
254+
this.add(button);
255+
}
256+
257+
private void createBackgroundButton() {
258+
JButton button = new JButton(new ImageIcon("img/Background.jpg"));
259+
button.setName(Component.BACKGROUND_BUTTON.name());
260+
button.setOpaque(false);
261+
button.setContentAreaFilled(false);
262+
button.setBounds(windowWidth + 30, 37, 32, 32);
263+
button.addActionListener(_ -> content = TileType.OUTSIDE);
264+
this.add(button);
265+
}
266+
267+
private void createBoxButton() {
268+
JButton button = new JButton(new ImageIcon("img/Caisse.jpg"));
269+
button.setName(Component.BOX_BUTTON.name());
270+
button.setOpaque(false);
271+
button.setContentAreaFilled(false);
272+
button.setBounds(windowWidth + 30, 74, 32, 32);
273+
button.addActionListener(_ -> content = TileType.UNSTORED_BOX);
274+
this.add(button);
275+
}
276+
277+
private void createBoxOnTargetButton() {
278+
JButton button = new JButton(new ImageIcon("img/CaisseRangee.jpg"));
279+
button.setName(Component.BOX_ON_TARGET_BUTTON.name());
280+
button.setOpaque(false);
281+
button.setContentAreaFilled(false);
282+
button.setBounds(windowWidth + 30, 111, 32, 32);
283+
button.addActionListener(_ -> content = TileType.STORED_BOX);
284+
this.add(button);
285+
}
286+
287+
private void createPlayerOnTargetButton() {
288+
JButton button = new JButton(new ImageIcon("img/JoueurRangement.jpg"));
289+
button.setName(Component.PLAYER_ON_TARGET_BUTTON.name());
290+
button.setOpaque(false);
291+
button.setContentAreaFilled(false);
292+
button.setBounds(windowWidth + 75, 0, 32, 32);
293+
button.addActionListener(_ -> content = TileType.WORKER_IN_STORAGE_AREA);
294+
this.add(button);
295+
}
296+
297+
private void createWallButton() {
298+
JButton button = new JButton(new ImageIcon("img/Mur.jpg"));
299+
button.setName(Component.WALL_BUTTON.name());
300+
button.setOpaque(false);
301+
button.setContentAreaFilled(false);
302+
button.setBounds(windowWidth + 75, 37, 32, 32);
303+
button.addActionListener(_ -> content = TileType.WALL);
304+
this.add(button);
305+
}
306+
307+
private void createTargetButton() {
308+
JButton button = new JButton(new ImageIcon("img/Rangement.jpg"));
309+
button.setName(Component.TARGET_BUTTON.name());
310+
button.setOpaque(false);
311+
button.setContentAreaFilled(false);
312+
button.setBounds(windowWidth + 75, 74, 32, 32);
313+
button.addActionListener(_ -> content = TileType.STORAGE_AREA);
314+
this.add(button);
315+
}
316+
317+
private void createEmptyButton() {
318+
JButton button = new JButton(new ImageIcon("img/Vide.jpg"));
319+
button.setName(Component.EMPTY_BUTTON.name());
320+
button.setOpaque(false);
321+
button.setContentAreaFilled(false);
322+
button.setBounds(windowWidth + 75, 111, 32, 32);
323+
button.addActionListener(_ -> content = TileType.FLOOR);
324+
this.add(button);
325+
}
326+
305327
@Override
306328
public void mouseDragged(MouseEvent e) {}
307329

0 commit comments

Comments
 (0)