1

I am trying to make a simple Java GUI that allows for a slideshow and music playback. All elements are working for the slideshow and playback except the JPanel is not properly resizing to the right as the window is expanded. Any suggestions on how to remedy this?

Additional information: I am using NetBeans IDE 8.2's GUI Builder.

Code for the GUI:

import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;



/**
*
* @author austi
*/
public class UserInterface extends javax.swing.JFrame implements KeyListener {

int _currentImage = 0;
String[] _imageLocations = {
                            "C:/Users/austi/Desktop/Encrypted Pictures Folder/Unencrypted/02115_malignestarrysky_1280x720.jpg",
                            "C:/Users/austi/Desktop/Encrypted Pictures Folder/Unencrypted/BmaY4y.jpg",
                            "C:/Users/austi/Desktop/Encrypted Pictures Folder/Unencrypted/pqWQmf.jpg",
                            "C:/Users/austi/Desktop/Encrypted Pictures Folder/Unencrypted/PsVLyq.jpg",
                            "C:/Users/austi/Desktop/Encrypted Pictures Folder/Unencrypted/z57Erk.jpg"
                           };

ImageIcon[] _images = new ImageIcon[5];

String _songLocation = "C:/Users/austi/Desktop/Encrypted Pictures Folder/Unencrypted/12_Man_Of_The_Year.wav";
Clip _song;
Boolean _songPlaying = false, _songAtStart = true;



/**
 * Creates new form UserInterface
 */
public UserInterface() {
    initComponents();
    readImages();
    loadSong();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);

}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    ImageFrame = new javax.swing.JPanel();
    lblImageContainer = new javax.swing.JLabel();
    btnNext = new javax.swing.JButton();
    btnPrevious = new javax.swing.JButton();
    btnStartMusic = new javax.swing.JButton();
    btnEndMusic = new javax.swing.JButton();
    filler5 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));
    filler6 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));
    filler7 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));
    filler8 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));
    filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
    filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
    btnRestartMusic = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setAlwaysOnTop(true);
    setSize(new java.awt.Dimension(0, 0));

    ImageFrame.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
    ImageFrame.setFocusable(false);

    lblImageContainer.setFocusable(false);

    javax.swing.GroupLayout ImageFrameLayout = new javax.swing.GroupLayout(ImageFrame);
    ImageFrame.setLayout(ImageFrameLayout);
    ImageFrameLayout.setHorizontalGroup(
        ImageFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(lblImageContainer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    ImageFrameLayout.setVerticalGroup(
        ImageFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(lblImageContainer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    btnNext.setText("Next");
    btnNext.setFocusable(false);
    btnNext.setName("btnNext"); // NOI18N
    btnNext.setPreferredSize(new java.awt.Dimension(76, 25));
    btnNext.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnNextActionPerformed(evt);
        }
    });

    btnPrevious.setText("Previous");
    btnPrevious.setFocusable(false);
    btnPrevious.setName("btnPrevious"); // NOI18N
    btnPrevious.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnPreviousActionPerformed(evt);
        }
    });

    btnStartMusic.setText("Start Music");
    btnStartMusic.setFocusable(false);
    btnStartMusic.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnStartMusicActionPerformed(evt);
        }
    });

    btnEndMusic.setText("End Music");
    btnEndMusic.setToolTipText("");
    btnEndMusic.setFocusPainted(false);
    btnEndMusic.setFocusable(false);
    btnEndMusic.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnEndMusicActionPerformed(evt);
        }
    });

    filler5.setFocusable(false);

    filler6.setFocusable(false);

    filler7.setFocusable(false);

    filler8.setFocusable(false);

    filler1.setFocusable(false);

    filler2.setFocusable(false);

    btnRestartMusic.setText("Restart Music");
    btnRestartMusic.setToolTipText("");
    btnRestartMusic.setEnabled(false);
    btnRestartMusic.setFocusable(false);
    btnRestartMusic.setName("btnRestartMusic"); // NOI18N
    btnRestartMusic.setPreferredSize(new java.awt.Dimension(104, 25));
    btnRestartMusic.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnRestartMusicActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(filler7, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(filler5, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnPrevious))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(btnStartMusic)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(btnRestartMusic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(btnEndMusic)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(filler2, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(ImageFrame, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(filler6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(filler8, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(ImageFrame, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btnEndMusic, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnRestartMusic, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnStartMusic, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(filler1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(filler2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(filler5, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                        .addComponent(filler6, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                        .addComponent(btnPrevious)
                        .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(filler7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(filler8, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE))))
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        

private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if(_currentImage > 0){
        _currentImage--;
        ChangeImage();
    }
}                                           

private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
    if(_currentImage < _images.length - 1){
        _currentImage++;
        ChangeImage();
    }
}                                       

private void btnStartMusicActionPerformed(java.awt.event.ActionEvent evt) {                                              
    _song.start();
    _songPlaying = true;
    btnRestartMusic.setEnabled(true);
}                                             

private void btnEndMusicActionPerformed(java.awt.event.ActionEvent evt) {                                            
    _song.stop();
    _songPlaying = false;
}                                           

private void btnRestartMusicActionPerformed(java.awt.event.ActionEvent evt) {                                                
    btnRestartMusic.setEnabled(false);
    _song.stop();
    _songPlaying = false;
    _song.setFramePosition(0);
}                                               

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new UserInterface().setVisible(true);
        }
    });
}

private void readImages(){
    for(int i = 0; i < _images.length; i++){
        ///This comment is the first possible implementaion of decrypting and never writing the unencrypted files
        ///Image decImage = newImageIcon(BufferedImage(Dencryt))???
        ///ImageIcon icon = new ImageIcon(decImage);
        ImageIcon icon = new ImageIcon(_imageLocations[i]);
        Image img = icon.getImage();
        Image newImg = img.getScaledInstance(lblImageContainer.getWidth(), lblImageContainer.getHeight(), Image.SCALE_SMOOTH);
        ImageIcon finalImage = new ImageIcon(newImg);
        _images[i] = finalImage;

    }
    ChangeImage();
}

private void ChangeImage(){
    lblImageContainer.setIcon(_images[_currentImage]);
}

private void loadSong(){

    try{
        File file = new File(_songLocation);
        AudioInputStream sound = AudioSystem.getAudioInputStream(file);
         // load the sound into memory (a Clip)
            _song = AudioSystem.getClip();
            _song.open(sound);
            System.out.println("Done");

    }catch(Exception e){

    }


}

// Variables declaration - do not modify                     
private javax.swing.JPanel ImageFrame;
private javax.swing.JButton btnEndMusic;
private javax.swing.JButton btnNext;
private javax.swing.JButton btnPrevious;
private javax.swing.JButton btnRestartMusic;
private javax.swing.JButton btnStartMusic;
private javax.swing.Box.Filler filler1;
private javax.swing.Box.Filler filler2;
private javax.swing.Box.Filler filler5;
private javax.swing.Box.Filler filler6;
private javax.swing.Box.Filler filler7;
private javax.swing.Box.Filler filler8;
private javax.swing.JLabel lblImageContainer;
// End of variables declaration                   

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
    int kc = e.getKeyCode();
    switch(kc){

        case 39:
            if(_currentImage < _images.length - 1){
                _currentImage++;
                ChangeImage();
            }
            break;

        case 37:
            if(_currentImage > 0){
                _currentImage--;
                ChangeImage();
            }
            break;

        case 32:
            if(_songPlaying){
                _song.stop();
            }
            else{
                _song.start();
                btnRestartMusic.setEnabled(true);

            }

            _songPlaying = !_songPlaying;
            break;

    }

}

@Override
public void keyReleased(KeyEvent e) {

}

}

sid34
  • 37
  • 5
  • 2
    1) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). .. – Andrew Thompson Nov 03 '16 at 05:48
  • 2
    .. 4) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Nov 03 '16 at 05:48

0 Answers0