-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.java
250 lines (206 loc) · 7.64 KB
/
GUI.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
import java.awt.EventQueue;
import javax.swing.JFrame;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import java.awt.Toolkit;
import java.awt.Color;
import java.awt.SystemColor;
import java.awt.Font;
import java.io.File;
import java.io.FileFilter;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.UIManager;
/**
* The GUI class for the audio recorder/player.
*/
public class GUI extends JFrame {
/**
* Main frame.
*/
private JFrame frmAudio;
/**
* Path to external resources, e.g. images.
*/
private static String resourcePath = "src/resources/";
/**
* List model and list for the recordings.
*/
public DefaultListModel LM = new DefaultListModel();
JList list = new JList(LM);
JScrollPane scrollpane = new JScrollPane(list);
/**
* Only allow .ogg and .mp3.
*/
FileFilter ff = new FileFilter() {
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
String name = pathname.getName().toLowerCase();
return name.endsWith(".ogg") || name.endsWith(".mp3");
}
};
/**
* The recorder.
*/
private Recorder audio;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI window = new GUI();
window.SetInitialFileList();
window.frmAudio.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Initialize the application.
*/
public GUI() {
initialize();
audio = new Recorder(this);
}
/**
* Initialize the contents of the frame.
*/
private void SetInitialFileList() {
File[] my_records;
my_records = new File(Recorder.recordingsPath).listFiles(ff);
for (File c : my_records) {
LM.addElement(c);
}
}
/**
* Sets up GUI.
*/
private void initialize() {
frmAudio = new JFrame();
frmAudio.setAlwaysOnTop(true);
frmAudio.getContentPane().setFont(new Font("Dialog", Font.PLAIN, 12));
frmAudio.getContentPane().setForeground(SystemColor.controlHighlight);
frmAudio.getContentPane().setBackground(Color.WHITE);
frmAudio.setTitle("Audio recorder");
frmAudio.setIconImage(Toolkit.getDefaultToolkit().getImage(resourcePath + "MicrophoneHot.png"));
frmAudio.setBounds(100, 100, 722, 456);
frmAudio.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmAudio.getContentPane()
.setLayout(
new FormLayout(new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("default:grow"), FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, RowSpec.decode("default:grow"),
FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
RowSpec.decode("default:grow"), FormFactory.RELATED_GAP_ROWSPEC, RowSpec.decode("default:grow"),
FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, }));
JButton record_btn = new JButton("");
record_btn.setHorizontalAlignment(SwingConstants.LEFT);
record_btn.setIcon(new ImageIcon(resourcePath + "RecordPressed (1).png"));
record_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
audio.record();
}
});
frmAudio.getContentPane().add(record_btn, "4, 4");
JButton volume_up_btn = new JButton("");
volume_up_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
audio.volumeUp();
}
});
JButton stop_btn = new JButton("");
stop_btn.setIcon(new ImageIcon(resourcePath + "Stop1Normal (1).png"));
stop_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
audio.stop();
}
});
frmAudio.getContentPane().add(stop_btn, "6, 4");
JButton play_btn = new JButton("");
play_btn.setIcon(new ImageIcon(resourcePath + "Play1Pressed (1).png"));
play_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
audio.play();
}
});
frmAudio.getContentPane().add(play_btn, "8, 4, left, default");
volume_up_btn.setIcon(new ImageIcon(resourcePath + "VolumeNormalRed.png"));
frmAudio.getContentPane().add(volume_up_btn, "4, 10");
JButton volume_down_btn = new JButton("");
volume_down_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
audio.volumeDown();
}
});
volume_down_btn.setIcon(new ImageIcon(resourcePath + "VolumeDisabled.png"));
frmAudio.getContentPane().add(volume_down_btn, "6, 10");
JButton mute_btn = new JButton("");
mute_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
audio.mute();
}
});
mute_btn.setIcon(new ImageIcon(resourcePath + "mute-2.png"));
frmAudio.getContentPane().add(mute_btn, "8, 10");
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
if (arg0.getValueIsAdjusting() == false) {
System.out.println(list.getSelectedValue());
}
}
});
list.setBorder(UIManager.getBorder("List.focusCellHighlightBorder"));
list.setBackground(Color.WHITE);
frmAudio.getContentPane().add(scrollpane, "4, 16, 12, 8, default, fill");
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setAutoscrolls(true);
// list.setBounds(MAXIMIZED_HORIZ, MAXIMIZED_VERT, MAXIMIZED_BOTH,
// MAXIMIZED_BOTH);
list.setVisibleRowCount(-1);
}
/**
* Adds a new file to the list.
* @param nameandpath
*/
public void addNewFiletoList(String nameandpath) {
LM.addElement(nameandpath);
}
/**
* Gets a selected file from the list.
* @return path to the file.
*/
public String getSelectedPlayItem() {
return list.getSelectedValue().toString();
}
}