From 91be78b421ed690a984443b54f0274a50c288fb0 Mon Sep 17 00:00:00 2001 From: jakjm Date: Sat, 30 Mar 2019 17:58:47 -0400 Subject: [PATCH] Improving look and feel, fixing bugs --- .../java/Talkbox/configurer/SetUpPanel.java | 189 ++++++++++++++---- .../java/Talkbox/recording/MusicRecorder.java | 7 +- 2 files changed, 151 insertions(+), 45 deletions(-) diff --git a/src/main/java/Talkbox/configurer/SetUpPanel.java b/src/main/java/Talkbox/configurer/SetUpPanel.java index 53d3e7b..2cebdae 100644 --- a/src/main/java/Talkbox/configurer/SetUpPanel.java +++ b/src/main/java/Talkbox/configurer/SetUpPanel.java @@ -237,11 +237,9 @@ public void actionPerformed(ActionEvent event) { } //Otherwise if it was one of the setup buttons else if(event.getSource() instanceof SetUpButton){ - setUpFrame.hideSetupFrame(); - setUpFrame.setVisible(true); + setUpFrame.openSetupFrame((SetUpButton) event.getSource(), ((SetUpButton) event.getSource()).getConfiguration()); - setUpFrame.colorFrame.setVisible(false); this.logger.logMessage("Setup button pressed"); } //Up button @@ -436,6 +434,44 @@ public void hideSetupFrame() { * @param config */ public void openSetupFrame(SetUpButton button, ButtonConfiguration config) { + if(this.currentButton == button && this.isVisible() == true) { + + //Bringing setup frame to the front. + java.awt.EventQueue.invokeLater(new Runnable() { + @Override + public void run() { + setUpFrame.toFront(); + + //Moving up under panels. + if(recordingFrame.isVisible())recordingFrame.toFront(); + if(emojiFrame.isVisible())emojiFrame.toFront(); + if(imageFrame.isVisible())imageFrame.toFront(); + if(colorFrame.isVisible())colorFrame.toFront(); + if(fileSelector.isVisible())fileSelector.toFront(); + } + }); + return; + } + else if(this.isVisible() == true) { + JOptionPane.showMessageDialog(null,"Don't lose your work!\nEither cancel or confirm setup before editing a new button!"); + + //Bringing setup frame to the front. + java.awt.EventQueue.invokeLater(new Runnable() { + @Override + public void run() { + setUpFrame.toFront(); + + //Moving up under panels. + if(recordingFrame.isVisible())recordingFrame.toFront(); + if(emojiFrame.isVisible())emojiFrame.toFront(); + if(imageFrame.isVisible())imageFrame.toFront(); + if(colorFrame.isVisible())colorFrame.toFront(); + if(fileSelector.isVisible())fileSelector.toFront(); + } + }); + return; + } + setUpFrame.hideSetupFrame(); this.currentButton = button; this.currentColor = config.getButtonColor(); this.currentColorPanel.setBackground(this.currentColor); @@ -458,8 +494,7 @@ public void openSetupFrame(SetUpButton button, ButtonConfiguration config) { else { currentImagePath.setText("ImagePath: (none)"); } - - + setUpFrame.setVisible(true); } @@ -473,6 +508,7 @@ public void onFileSelected(File file) { if (FileIO.checkWaveFormat(file)) { currentAudioFile = file; currentSoundPath.setText("Sound Path: " + currentAudioFile.getPath()); + if(musicPlayer != null)musicPlayer.stop(); musicPlayer = new MusicPlayer(currentAudioFile); logger.logMessage("Sound file added."); } else { @@ -687,6 +723,9 @@ public class RecordingFrame extends JFrame implements ActionListener, WindowList public JLabel flashingLabel; public JButton stopButton; public MusicRecorder recorder; + private static final String RECORDING = " ● Recording ● "; + private static final String NOT_RECORDING = " Not recording"; + private volatile boolean isRecording = false; public RecordingFrame() { super("Record Sound"); this.setSize(250,200); @@ -701,8 +740,9 @@ public RecordingFrame() { this.add(recordButton); //Setting up recording indicator - flashingLabel = new JLabel(""); - flashingLabel.setForeground(Color.red); + flashingLabel = new JLabel(NOT_RECORDING); + flashingLabel.setHorizontalAlignment(SwingConstants.CENTER); + flashingLabel.setForeground(Color.black); this.add(flashingLabel); //Setting up the stop button @@ -712,9 +752,13 @@ public RecordingFrame() { this.add(stopButton); this.setVisible(false); } + /** + * Method for hiding the recording panel. + */ public void hideRecordingFrame() { this.setVisible(false); if(recorder.isRecording()) { + isRecording = false; recorder.stop(); //Resetting the buttons. @@ -732,6 +776,9 @@ public void actionPerformed(ActionEvent e) { recordButton.setEnabled(false); stopButton.setEnabled(true); recorder.record(); + isRecording = true; + Thread blinkThread = new Thread(new BlinkTask()); + blinkThread.start(); } else if(e.getSource() == stopButton) { logger.logMessage("Stopped recording."); @@ -746,6 +793,39 @@ else if(e.getSource() == stopButton) { currentSoundPath.setText("Sound Path: " + currentAudioFile.getPath()); if(musicPlayer != null && musicPlayer.isPlaying())musicPlayer.stop(); musicPlayer = new MusicPlayer(currentAudioFile); + isRecording = false; + } + } + /** + * BlinkTask for the blinking recording label. + * @author jordan + * @version March 30th 2019 + */ + public class BlinkTask implements Runnable { + public void run() { + //Flashing label blinking + flashingLabel.setText(""); + flashingLabel.setForeground(Color.red); + flashingLabel.setText(RECORDING); + long delayTime = System.currentTimeMillis() + 500; + boolean blink = true; + while(isRecording) { + if(System.currentTimeMillis() < delayTime) { + continue; + } + else { + delayTime = System.currentTimeMillis() + 500; + } + blink =! blink; + if(!blink) { + flashingLabel.setText(RECORDING); + } + else{ + flashingLabel.setText(""); + } + } + flashingLabel.setText(NOT_RECORDING); + flashingLabel.setForeground(Color.black); } } /** @@ -838,69 +918,85 @@ public ImageFrame() { this.add(orientations, BorderLayout.SOUTH); } + /** + * Opens the image for this button for selecting and orienting. + * @param image + */ public void openFor(BufferedImage image) { + if(this.iconImage == image) { + return; + } this.iconImage = image; this.originalImage = image; if(image != null) { imageLabel.setIcon(new ImageIcon(iconImage)); imageLabel.setText(""); + + if(this.iconImage.getWidth(null) == MAX_WIDTH && this.iconImage.getHeight(null) == MAX_HEIGHT) { + this.orientation = SQUARE; + } + else if(this.iconImage.getWidth(null) == MAX_WIDTH) { + this.orientation = HORIZONTAL; + } + else { + this.orientation = VERTICAL; + } } else { imageLabel.setIcon(null); imageLabel.setText("(No Image)"); } - - //Setting buttons and orientation. - this.orientation = SQUARE; - square.setEnabled(false); - horizontal.setEnabled(true); - vertical.setEnabled(true); } - public void actionPerformed(ActionEvent event) { - if(event.getSource() == openImage){ - fileSelector.setMode(FileSelector.PICTURE); - fileSelector.setSelectionListener(new OpenImageListener()); - fileSelector.setVisible(true); + public void setOrientation(int or) { + if(this.orientation == or) { + return; } - else if(event.getSource() == vertical) { - orientation = VERTICAL; - + orientation = or; + if(or == VERTICAL) { + //Resetting buttons vertical.setEnabled(false); square.setEnabled(true); horizontal.setEnabled(true); - - - if(originalImage != null) { - iconImage = scaleImage(originalImage); - imageLabel.setIcon(new ImageIcon(iconImage)); - } } - else if(event.getSource() == horizontal) { - orientation = HORIZONTAL; + else if(or == HORIZONTAL){ //Resetting buttons horizontal.setEnabled(false); vertical.setEnabled(true); square.setEnabled(true); - - if(originalImage != null) { - iconImage = scaleImage(originalImage); - imageLabel.setIcon(new ImageIcon(iconImage)); - } } - else if(event.getSource() == square) { - orientation = SQUARE; + else if(or == SQUARE) { //Resetting buttons square.setEnabled(false); horizontal.setEnabled(true); vertical.setEnabled(true); - - if(originalImage != null) { - iconImage = scaleImage(originalImage); - imageLabel.setIcon(new ImageIcon(iconImage)); - } + } + if(originalImage != null) { + iconImage = scaleImage(originalImage); + imageLabel.setIcon(new ImageIcon(iconImage)); + } + + + } + /** + * Action Listener for the ImageFrame's buttons + */ + public void actionPerformed(ActionEvent event) { + if(event.getSource() == openImage){ + fileSelector.setMode(FileSelector.PICTURE); + fileSelector.setSelectionListener(new OpenImageListener()); + fileSelector.setVisible(true); + } + else if(event.getSource() == vertical) { + setOrientation(VERTICAL); + } + else if(event.getSource() == horizontal) { + setOrientation(HORIZONTAL); + } + else if(event.getSource() == square) { + setOrientation(SQUARE); } else if(event.getSource() == confirmImage) { if(iconImage != null) { @@ -918,20 +1014,25 @@ else if(event.getSource() == confirmImage) { } } } + /** + * Scales the image to the correct size. + * @param image - the image to be scaled. + * @return the image scaled to the correct size. + */ public BufferedImage scaleImage(Image image) { if(imageFrame.orientation == ImageFrame.SQUARE) { - if(image.getWidth(null) > ImageFrame.MAX_WIDTH || image.getHeight(null) > ImageFrame.MAX_HEIGHT) { + if(image.getWidth(null) != ImageFrame.MAX_WIDTH || image.getHeight(null) != ImageFrame.MAX_HEIGHT) { image = image.getScaledInstance(ImageFrame.MAX_WIDTH,ImageFrame.MAX_HEIGHT,Image.SCALE_SMOOTH); } } else if(imageFrame.orientation == ImageFrame.HORIZONTAL) { - if(image.getWidth(null) > ImageFrame.MAX_WIDTH || image.getHeight(null) > (ImageFrame.MAX_HEIGHT / 2)) { + if(image.getWidth(null) != ImageFrame.MAX_WIDTH || image.getHeight(null) != (ImageFrame.MAX_HEIGHT / 2)) { image = image.getScaledInstance(ImageFrame.MAX_WIDTH,(ImageFrame.MAX_HEIGHT / 2),Image.SCALE_SMOOTH); } } else if(imageFrame.orientation == ImageFrame.VERTICAL) { - if(image.getWidth(null) > (ImageFrame.MAX_WIDTH / 2) || image.getHeight(null) > ImageFrame.MAX_HEIGHT) { + if(image.getWidth(null) != (ImageFrame.MAX_WIDTH / 2) || image.getHeight(null) != ImageFrame.MAX_HEIGHT) { image = image.getScaledInstance((ImageFrame.MAX_WIDTH / 2),ImageFrame.MAX_HEIGHT,Image.SCALE_SMOOTH); } } diff --git a/src/main/java/Talkbox/recording/MusicRecorder.java b/src/main/java/Talkbox/recording/MusicRecorder.java index 5d0505d..82d2c18 100644 --- a/src/main/java/Talkbox/recording/MusicRecorder.java +++ b/src/main/java/Talkbox/recording/MusicRecorder.java @@ -69,7 +69,12 @@ public ByteArrayOutputStream stop() { } return stream; } - + + /** + * Method for writing an audio Byte Array Stream to a file. + * @param format + * @param file + */ public static void writeToFile(ByteArrayOutputStream stream, AudioFormat format, File file) { try { byte[] rawData = stream.toByteArray();