This is my full codes here with some explanation.
public class SlideShow extends javax.swing.JFrame {
JPanel slides;
CardLayout layoutManager;
private JButton btnPrev;
private JButton btnNext;
private JButton btnHome;
private JButton btnSound;
public SlideShow() {
super();
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
btnPrev = new javax.swing.JButton();
btnNext = new javax.swing.JButton();
btnHome = new javax.swing.JButton();
btnSound = new javax.swing.JButton();
btnPrev.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/back+button.png")));
btnNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/next2.png")));
btnHome.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/home_icons.png")));
btnSound.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/Media-Controls-Volume-Down-icon.png")));
slides = new JPanel();
slides.setBackground(Color.WHITE);
slides.setBorder(BorderFactory.createLineBorder(Color.BLACK));
slides.setLayout(layoutManager = new CardLayout(0,0));
for(int i=2; i<=24; i++){
slides.add(i+".png", new JLabel(new ImageIcon(getClass().getResource("/resources/"+i+".png"))));
}
add(slides);
add(btnHome);
add(btnPrev);
add(btnNext);
add(btnSound);
btnPrev.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layoutManager.previous(slides);
}
});
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layoutManager.next(slides);
}
});
btnHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
close();
Frame fr = new Frame();
fr.setVisible(true);
}
});
btnSound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
music("././build/classes/resources/test.wav");
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(700,530);
}
public void close(){
super.dispose();
}
public static void music(String path)
{
AudioStream BGM;
AudioData MD;
AudioDataStream audiostream;
try{
BGM = new AudioStream (new FileInputStream(path));
MD = BGM.getData();
audiostream = new AudioDataStream(MD);
AudioPlayer.player.start(audiostream);
}catch(IOException error){}
}
public static void main(String args[]) {
SlideShow frame = new SlideShow();
frame.setVisible(true);
}
}
I have a slideshow with multiple images within my JFrame
. Every slide has a button which will output some sounds when it clicked. These slides are called within same JFrame. So, I didn't have to make many JFrame for each slides. I want to make different sounds for every slides. All I have to do is called the path of the slides images to match the sound.
My situation here is, basically, I want to shorten the variable of the ImageIcon so that I can return a specific path like 5.png to insert a sound. However, I can't do that without calling the full path in the ImageIcon
and somehow, it doesn't work at all even if I called the full path.
So, if I can get a specific path at slides
as a variable or something like that, I can use it to call different sound using same button. How to shorten it?
Or, is there a way to get a specific variable from slides
? How to call the variable though? There are 24 slides images in this app and how to differentiate it?
I have test this code JOptionPane.showMessageDialog(null, "Test!");
in the for loop
and it appears that this codes output 24 times before the actual slides appear. So, it means, that for loop
only input the image and I have no idea how to call it back so I can make something like if else
statement to put sounds at different slides.