-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainFrame.java
322 lines (279 loc) · 10.3 KB
/
MainFrame.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
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
/* This file contains everything that is needed for GUI */
/* Both frontend and backend */
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.io.File;
class Utils implements Music
{
static boolean[][] generateResultsFromCheckBoxes(JCheckBox[][] checkBoxList)
{
boolean results[][] = new boolean[myInstruments.length][checkBoxesInTheRow];
for (int i = 0; i < myInstruments.length; i++)
{
for (int j = 0; j < checkBoxesInTheRow; j++)
{
if(checkBoxList[i][j].isSelected())
{
results[i][j] = true;
System.out.print("X ");
}
else {
results[i][j] = false;
System.out.print("o ");
}
}
System.out.println("-------" + myInstruments[i].name + "-------");
}
System.out.println("-------------------------------");
return results;
}
static boolean containsTrue(boolean[][] array)
{
try {
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
if(array[i][j]) return true;
}
}
return false;
} catch (ArrayIndexOutOfBoundsException err)
{
return false;
}
}
}
public class MainFrame extends JFrame implements Music {
//Frame that contains only main panel
private MainPanel mainPanel = new MainPanel();
private JMenuBar menuBar = new JMenuBar();
private JMenu menu = new JMenu("File");
private JMenuItem importItem = new JMenuItem("Import");
private JMenuItem exportAs = new JMenuItem("Export as");
private JFrame parentFrame = new JFrame();
private JFileChooser fileChooser = new JFileChooser();
private void init()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setVisible(true);
setResizable(false);
}
public MainFrame()
{
super("Music");
init();
exportAs.addActionListener(new ExportAsListener());
importItem.addActionListener(new ImportItemListener());
menu.add(importItem);
menu.add(exportAs);
menuBar.add(menu);
setJMenuBar(menuBar);
add(mainPanel);
File cwd = new File(".");
fileChooser.setFileFilter(new FileNameExtensionFilter("Midi Files", "mid"));
fileChooser.setCurrentDirectory(cwd);
pack();
}
class ExportAsListener implements ActionListener{
public void actionPerformed(ActionEvent e)
{
System.out.println("Export as clicked");
boolean[][] results = Utils.generateResultsFromCheckBoxes(mainPanel.musicPanel.checkBoxList);
System.out.println(Utils.containsTrue(results));
fileChooser.setDialogTitle("Select file to export");
int userSelection = fileChooser.showSaveDialog(parentFrame);
if (userSelection == JFileChooser.APPROVE_OPTION)
{
File outFile = fileChooser.getSelectedFile();
musicalDevil.exportToFile(results, outFile.getAbsolutePath());
}
}
}
class ImportItemListener implements ActionListener {
public void actionPerformed(ActionEvent e)
{
fileChooser.setDialogTitle("Select file to import");
int userSelection = fileChooser.showSaveDialog(parentFrame);
if (userSelection == JFileChooser.APPROVE_OPTION)
{
File inFile = fileChooser.getSelectedFile();
musicalDevil.play(musicalDevil.importFromFile(inFile.getAbsolutePath()), mainPanel.toolBar.toolBarLeftPanel.bpm);
}
}
}
}
class MainPanel extends JPanel {
// Main panel contains all gui elements
public MusicPanel musicPanel = new MusicPanel(); // Public because there is checkboxes.
public ToolBar toolBar = new ToolBar(musicPanel.checkBoxList); // Public because BPM. Something wrong, but let's continue.
private InstrumentsPanel instrumentsPanel = new InstrumentsPanel();
private void init()
{
setLayout(new BorderLayout());
setBorder(BorderFactory.createEmptyBorder(0,5,10,5));
}
public MainPanel()
{
init();
setBackground(Color.LIGHT_GRAY);
add(instrumentsPanel, BorderLayout.WEST);
add(toolBar, BorderLayout.NORTH);
add(musicPanel, BorderLayout.CENTER);
}
}
class InstrumentsPanel extends JPanel implements Music {
// Panel on the left that contains labels with instruments names
private void init()
{
BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
setLayout(boxLayout);
setBackground(Color.WHITE);
setBorder(BorderFactory.createEmptyBorder(0,5,0,10));
}
public InstrumentsPanel()
{
init();
for (int i = 0; i < myInstruments.length; i++)
{
JLabel label = new JLabel(myInstruments[i].name);
label.setFont(new Font("Calibri", Font.PLAIN, 24));
label.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
add(label);
}
}
}
class MusicPanel extends JPanel implements Music {
// Big panel with checkboxes
// Public checkboxes to access them from any part of GUI.
public JCheckBox[][] checkBoxList = new JCheckBox[myInstruments.length][checkBoxesInTheRow];
private void init()
{
GridLayout gridLayout = new GridLayout(myInstruments.length, 16, 1, 1);
setLayout(gridLayout);
setBackground(Color.GRAY);
}
public MusicPanel()
{
init();
for (int i = 0; i < myInstruments.length; i++)
{
for (int j = 0; j < checkBoxesInTheRow; j++)
{
JCheckBox box = new JCheckBox();
box.setName(myInstruments[i].name + Integer.toString(i));
box.setSelected(false);
box.setBackground(Color.LIGHT_GRAY);
checkBoxList[i][j] = box;
add(box);
}
}
}
}
class ToolBar extends JPanel implements Music {
// Panel on the top with buttons, contains two panels left and right.
public ToolBarLeftPanel toolBarLeftPanel; // To get access from main panel.
private void init()
{
setLayout(new BorderLayout());
setBackground(Color.LIGHT_GRAY);
}
public ToolBar(JCheckBox[][] checkBoxList)
{
init();
toolBarLeftPanel = new ToolBarLeftPanel(checkBoxList);
add(toolBarLeftPanel, BorderLayout.WEST);
}
}
class ToolBarLeftPanel extends JPanel implements Music {
// Some terrible code for scaling icons
private ImageIcon originalIconDown = new ImageIcon(getClass().getResource("src/icons/down-arrow.png"));
private Image rawIconDown = originalIconDown.getImage();
private Image scaledIconDown = rawIconDown.getScaledInstance(20, 20, Image.SCALE_DEFAULT);
private ImageIcon tempIconDown = new ImageIcon(scaledIconDown);
// Same code for the second icon
private ImageIcon originalIconUp = new ImageIcon(getClass().getResource("src/icons/up-arrow.png"));
private Image rawIconUp = originalIconUp.getImage();
private Image scaledIconUp = rawIconUp.getScaledInstance(20, 20, Image.SCALE_DEFAULT);
private ImageIcon tempIconUp = new ImageIcon(scaledIconUp);
// Buttons
private JButton startBtn = new JButton("Start");
private JButton stopBtn = new JButton("Stop");
private JButton tempDownBtn = new JButton("", tempIconDown);
private JButton tempUpBtn = new JButton("", tempIconUp);
private JFrame popUpFrame = new JFrame();
private JLabel bpmSign = new JLabel("BPM:");
private JTextField bpmField = new JTextField();
// For buttons backend
int bpm = 120; // Bits per minute
JCheckBox[][] checkBoxList;
boolean[][] results; // = new boolean[myInstruments.length][checkBoxesInTheRow];
void init()
{
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
}
public ToolBarLeftPanel(JCheckBox[][] checkBoxList)
{
init();
this.checkBoxList = checkBoxList;
startBtn.addActionListener(new StartListener());
stopBtn.addActionListener(new StopListener());
tempDownBtn.addActionListener(new tempoDownListener());
tempUpBtn.addActionListener(new tempoUpListener());
bpmField.setText(Integer.toString(bpm));
bpmSign.setFont(new Font("Calibri", Font.PLAIN, 20));
add(startBtn); add(stopBtn);
add(bpmSign);add(tempDownBtn); add(bpmField); add(tempUpBtn);
}
void showLowBpmPopUp()
{
JOptionPane.showMessageDialog(popUpFrame, "BPM can't be lower than zero", "Low BPM", JOptionPane.OK_OPTION);
}
void showIncorrectBpmPopUp()
{
JOptionPane.showMessageDialog(popUpFrame, "1. BPM can't be lower than zero.\n2. BPM must be integer.", "Incorrect BPM", JOptionPane.OK_OPTION);
}
class StartListener implements ActionListener, Music {
public void actionPerformed(ActionEvent e)
{
results = Utils.generateResultsFromCheckBoxes(checkBoxList);
try {
bpm = Integer.parseInt(bpmField.getText());
} catch (NumberFormatException err) {
showIncorrectBpmPopUp();
}
if(bpm<1) showLowBpmPopUp();
else {
musicalDevil.stop();
musicalDevil.play(musicalDevil.buildMusic(results), bpm);
}
}
}
class StopListener implements ActionListener {
public void actionPerformed(ActionEvent e)
{
musicalDevil.stop();
}
}
class tempoDownListener implements ActionListener {
public void actionPerformed(ActionEvent e)
{
if(bpm<1) showLowBpmPopUp();
else {
bpm -= 10;
bpmField.setText(Integer.toString(bpm));
}
}
}
class tempoUpListener implements ActionListener {
public void actionPerformed(ActionEvent e)
{
bpm += 10;
bpmField.setText(Integer.toString(bpm));
}
}
}