aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/uk/co/notori/gol/MainUI.java
blob: dbb3dddd869b6f84e2301216a3706414e28e679e (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package uk.co.notori.gol;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The main UI for the Game of Life app
 */
public class MainUI extends JPanel {
    private static final Logger log = LoggerFactory.getLogger(MainUI.class);

    public final Panel panel;
    private final JLabel generationLabel;
    private final JLabel populationLabel;
    
    public MainUI(MainScreen.ExitHook exitHook, Panel panel) {
        super();
        
        this.panel = panel;
        
        setLayout(new BorderLayout());
        
		// Panel area
        add(panel, BorderLayout.CENTER);
        
        // calculate appropriate font sizes based on screen dimensions, 
		//  not tested on multiple devicesbut in theory works.
        Dimension screenSize = getScreenSize();
        int labelFontSize = calculateFontSize(screenSize, 14);
        int buttonFontSize = calculateFontSize(screenSize, 12);
        int speedFontSize = calculateFontSize(screenSize, 10);
        int infoFontSize = calculateFontSize(screenSize, 10);
        
        // info panel (top)
        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new GridLayout(1, 2));
        
        generationLabel = new JLabel("Generation: 0", JLabel.CENTER);
        generationLabel.setFont(new Font("SansSerif", Font.BOLD, labelFontSize));
        infoPanel.add(generationLabel);
        
        populationLabel = new JLabel("Population: 0", JLabel.CENTER);
        populationLabel.setFont(new Font("SansSerif", Font.BOLD, labelFontSize));
        infoPanel.add(populationLabel);
        
        add(infoPanel, BorderLayout.NORTH);
        
        // control panel (bottom)
        JPanel bottomPanel = new JPanel(new BorderLayout(5, 5));
        
        JLabel creditsLabel = new JLabel("Kindle Game Of Life - made by notori :)", JLabel.CENTER);
        creditsLabel.setFont(new Font("SansSerif", Font.ITALIC, infoFontSize));
        creditsLabel.setBorder(BorderFactory.createEmptyBorder(2, 0, 5, 0));
        bottomPanel.add(creditsLabel, BorderLayout.NORTH);
        
        // main control buttons in a grid
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new GridLayout(1, 3, 10, 10)); // 1 row, 3 columns with gaps
        controlPanel.setOpaque(true);
        
        // speed buttons in their own panel
        JPanel speedPanel = new JPanel();
        speedPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 8, 5));
        speedPanel.setOpaque(true);
        speedPanel.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(Color.BLACK, 1),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)
        ));
        
        JLabel speedLabel = new JLabel("Speed:", JLabel.CENTER);
        speedLabel.setFont(new Font("SansSerif", Font.BOLD, speedFontSize));
        speedPanel.add(speedLabel);
        
        // declare all buttons
        JButton playButton = new JButton("Play");
        JButton pauseButton = new JButton("Pause");
        JButton resetButton = new JButton("Reset");
        JButton slowButton = new JButton("S");
        JButton mediumButton = new JButton("M");
        JButton fastButton = new JButton("F");
        JButton exitButton = new JButton("Exit");
        
        // configure control buttons
        playButton.setFont(new Font("SansSerif", Font.BOLD, buttonFontSize));
        playButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel.play();
            }
        });
        controlPanel.add(playButton);
        styleControlButton(playButton);
        
        pauseButton.setFont(new Font("SansSerif", Font.BOLD, buttonFontSize));
        pauseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel.pause();
            }
        });
        controlPanel.add(pauseButton);
        styleControlButton(pauseButton);
        
        resetButton.setFont(new Font("SansSerif", Font.BOLD, buttonFontSize));
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel.reset();
            }
        });
        controlPanel.add(resetButton);
        styleControlButton(resetButton);
        
        // configure speed buttons
        slowButton.setFont(new Font("SansSerif", Font.PLAIN, speedFontSize));
        slowButton.setPreferredSize(new Dimension(50, 50));
        slowButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel.setSpeed(500);
                updateSpeedButtonSelection(slowButton, mediumButton, fastButton);
            }
        });
        speedPanel.add(slowButton);
        styleSpeedButton(slowButton);
        
        mediumButton.setFont(new Font("SansSerif", Font.PLAIN, speedFontSize));
        mediumButton.setPreferredSize(new Dimension(50, 50));
        mediumButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel.setSpeed(300);
                updateSpeedButtonSelection(slowButton, mediumButton, fastButton);
            }
        });
        speedPanel.add(mediumButton);
        styleSpeedButton(mediumButton);
        
        fastButton.setFont(new Font("SansSerif", Font.PLAIN, speedFontSize));
        fastButton.setPreferredSize(new Dimension(50, 50));
        fastButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel.setSpeed(100);
                updateSpeedButtonSelection(slowButton, mediumButton, fastButton);
            }
        });
        speedPanel.add(fastButton);
        styleSpeedButton(fastButton);
        
        // medium speed as default
        updateSpeedButtonSelection(slowButton, mediumButton, fastButton);
        
        // exit button
        exitButton.setFont(new Font("SansSerif", Font.BOLD, buttonFontSize));
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exitHook.exit();
            }
        });
        styleControlButton(exitButton);
        
        JPanel exitPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        exitPanel.add(exitButton);
        
        // add control panels to the container
        JPanel controlContainer = new JPanel(new BorderLayout());
        controlContainer.add(controlPanel, BorderLayout.NORTH);
        controlContainer.add(speedPanel, BorderLayout.CENTER);
        
        // add all panels to the bottom panel
        bottomPanel.add(controlContainer, BorderLayout.CENTER);
        bottomPanel.add(exitPanel, BorderLayout.SOUTH);
        
        // add bottom panel to main layout
        add(bottomPanel, BorderLayout.SOUTH);
        
		// another very ugly fix for weird component visibility behaviour
        new java.util.Timer().schedule(
            new java.util.TimerTask() {
                public void run() {
                    try {
                        SwingUtilities.invokeAndWait(new Runnable() {
                            public void run() {
                                // Force the buttons to be visible
                                refreshButton(playButton);
                                refreshButton(pauseButton);
                                refreshButton(resetButton);
                                refreshButton(slowButton);
                                refreshButton(mediumButton);
                                refreshButton(fastButton);
                                refreshButton(exitButton);
                                
                                controlPanel.invalidate();
                                controlPanel.validate();
                                controlPanel.repaint();
                                
                                speedPanel.invalidate();
                                speedPanel.validate();
                                speedPanel.repaint();
                                
                                bottomPanel.invalidate();
                                bottomPanel.validate();
                                bottomPanel.repaint();
                                
                                creditsLabel.setVisible(true);
                                creditsLabel.invalidate();
                                creditsLabel.validate();
                                creditsLabel.repaint();
                            }
                        });
                    } catch (Exception e) {
                        log.error("Error during button refresh", e);
                    }
                }
            },
            1000
        );
        
        updateCounters(panel.getGeneration(), panel.getPopulation());
        
		// another very ugly fix for weird component visibility behaviour
        new javax.swing.Timer(800, new ActionListener() {
            private int count = 0;
            public void actionPerformed(ActionEvent e) {
                if (count++ < 5) {
                    bottomPanel.invalidate();
                    bottomPanel.validate();
                    bottomPanel.repaint();
                } else {
                    ((javax.swing.Timer)e.getSource()).stop();
                }
            }
        }).start();
        
        log.debug("MainUI initialized with buttons and panels");
    }
    
    /**
     * Helper method to refresh button visibility
     */
    private void refreshButton(JButton button) {
        button.setVisible(true);
        button.invalidate();
        button.validate();
        button.repaint();
    }
    
    /**
     * Helper method to style control buttons
     */
    private void styleControlButton(JButton button) {
        button.setOpaque(true);
        button.setBorderPainted(true);
        button.setContentAreaFilled(true);
        button.setFocusable(true);
        button.setVisible(true);
        
        button.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    }
    
    /**
     * Helper method to style speed buttons
     */
    private void styleSpeedButton(JButton button) {
        button.setOpaque(true);
        button.setBorderPainted(true);
        button.setContentAreaFilled(true);
        button.setFocusable(true);
        button.setVisible(true);
        
        button.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
        
        button.setMargin(new Insets(8, 8, 8, 8));
        button.setHorizontalAlignment(SwingConstants.CENTER);
    }
    
    /**
     * Helper method to update speed button selection
     */
    private void updateSpeedButtonSelection(JButton slowButton, JButton mediumButton, JButton fastButton) {
        // reset all buttons to normal state
        slowButton.setFont(new Font(slowButton.getFont().getName(), Font.PLAIN, slowButton.getFont().getSize()));
        mediumButton.setFont(new Font(mediumButton.getFont().getName(), Font.PLAIN, mediumButton.getFont().getSize()));
        fastButton.setFont(new Font(fastButton.getFont().getName(), Font.PLAIN, fastButton.getFont().getSize()));
        
        slowButton.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
        mediumButton.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
        fastButton.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
        
        // determine which button is selected
        JButton selectedButton;
        if (panel.getDelay() == 500) {
            selectedButton = slowButton;
        } else if (panel.getDelay() == 100) {
            selectedButton = fastButton;
        } else {
            selectedButton = mediumButton;
        }
        
        // make selected button stand out with border
        selectedButton.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
    }
    
    private Dimension getScreenSize() {
        if (Panel.rootSize != null) {
            return Panel.rootSize;
        }
        return new Dimension(800, 600); // default fallback
    }
    
    private int calculateFontSize(Dimension screenSize, int defaultSize) {
        if (Util.isKindle()) {
            if (screenSize.width <= 600) {
                return Math.max(defaultSize - 4, 8);
            } else if (screenSize.width <= 800) {
                return Math.max(defaultSize - 2, 10);
            }
        }
        return defaultSize;
    }
    
    public void updateCounters(int generation, int population) {
        generationLabel.setText("Generation: " + String.valueOf(generation));
        populationLabel.setText("Population: " + String.valueOf(population));
    }
}