-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUIDriver.java
126 lines (107 loc) · 3.95 KB
/
GUIDriver.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
/**
* @version 1.0.0, 8 May 2023
* @author Dylan Nguyen and Andrew Kim
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.File;
import java.util.ArrayList;
public class GUIDriver {
private String selected = "dashboard";
public GUIDriver() {
// create the frame
MainFrame mainGUI = new MainFrame();
mainGUI.setIconImage(new ImageIcon("resources/icon.png").getImage());
// create the container; background color is the sidebar color
JPanel gridPanel = new JPanel();
mainGUI.add(gridPanel);
// load options before render
Options.loadOptions();
// buttons placed in arraylist to allow for programmatic selection
ArrayList<SidebarButton> buttons = new ArrayList<SidebarButton>();
// use a 2x3 grid layout
gridPanel.setLayout(new GridBagLayout());
gridPanel.setBackground(Color.WHITE);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.SOUTH;
// create the sidebar buttons
SidebarButton dashboardButton = new SidebarButton(new File("resources/dashboard.png"));
dashboardButton.setText("dashboard"); // used kind of like a button id
c.gridy = 0;
gridPanel.add(dashboardButton, c);
buttons.add(dashboardButton);
SidebarButton stocksButton = new SidebarButton(new File("resources/stocks.png"));
stocksButton.setText("stocks");
c.gridy++; // move to the next row
gridPanel.add(stocksButton, c);
buttons.add(stocksButton);
SidebarButton matthewButton = new SidebarButton(new File("resources/matthew.png"));
matthewButton.setText("matthew");
c.gridy++;
gridPanel.add(matthewButton, c);
buttons.add(matthewButton);
SidebarButton pughButton = new SidebarButton(new File("resources/clicker/best_teacher.jpg"));
pughButton.setText("mr pugh");
c.gridy++;
gridPanel.add(pughButton, c);
buttons.add(pughButton);
SidebarButton optionsButton = new SidebarButton(new File("resources/options.png"));
optionsButton.setText("options");
c.gridy++;
gridPanel.add(optionsButton, c);
buttons.add(optionsButton);
// create the display panel, which is the main content area
DisplayPanel displayPanel = new DisplayPanel();
displayPanel.setPreferredSize(new Dimension(600, 550));
c.gridx = 1; // 2nd column
c.gridy = 0;
c.gridheight = buttons.size();
gridPanel.add(displayPanel, c);
// resize the display panel when the window is resized
gridPanel.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent evt) {
displayPanel.setPreferredSize(new Dimension(gridPanel.getWidth() - 76, gridPanel.getHeight()));
displayPanel.revalidate();
}
});
// add action listeners to the buttons to switch display on click
for (SidebarButton button : buttons) {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (SidebarButton b : buttons)
b.deselect();
selected = button.getText();
button.select();
displayPanel.openByID(selected);
mainGUI.setWindowTitle(selected);
}
});
}
mainGUI.add(gridPanel);
mainGUI.setSize(mainGUI.getMinimumSize().width + 10, mainGUI.getMinimumSize().height + 10);
// Open fullscreen if option is enabled
if (Options.getStartFullscreen())
mainGUI.setExtendedState(JFrame.MAXIMIZED_BOTH);
// set the default display to the dashboard
dashboardButton.select();
displayPanel.displayDashboard();
displayPanel.reloadOpenPanel();
mainGUI.setWindowTitle(selected);
// reload the displayPanel every couple seconds for live updates
// made with help from github copilot
Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(displayPanel.getCurrentPanel() != "options" && displayPanel.getCurrentPanel() != "mr pugh")
displayPanel.openByID(selected);
}
});
timer.setRepeats(true);
timer.start();
}
}