hi, i've tried to make a smiple player using to buttons RESUME and SUS
PEND and a progressbar that will show me track duration so when the song is finished it's completed. but it doesnt works.it always shos the full progress bar?
this is te first class:
*************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class ImageAudioAnimation extends JApplet {
static final int MY_MINIMUM = 0;
static final int MY_MAXIMUM = 100;
private final static int broj_na_nacii = 7;
private int br = 0;
private ImageIcon[] ikoni = new ImageIcon[broj_na_nacii];
private AudioClip[] audioClips = new AudioClip[broj_na_nacii];
private AudioClip brAudioClip;
private int[] delays = {48000, 54000, 59000, 54000, 59000, 31000, 68000};
private Timer timer = new Timer(delays[0], new TimerListener());
private JLabel lblImage = new JLabel();
private JButton btnResume = new JButton("Resume");
private JButton btnSuspend = new JButton("Suspend");
private JComboBox cmbNacii = new JComboBox(new Object[]{"Denmark", "Germany", "China", "India", "Norway", "UK", "US"});
public void init() {
for (int i = 0; i < broj_na_nacii; i++) {
ikoni[i] = new ImageIcon(getClass().getResource("image/flag" + i + ".gif"));
audioClips[i] = Applet.newAudioClip(getClass().getResource("audio/anthem" + i + ".mid"));
}
cmbNacii.setRenderer(new CountryCellRenderer());
JPanel panel = new JPanel();
final SwingProgressBarExample it = new SwingProgressBarExample();
panel.add(it);
panel.add(btnResume);
panel.add(btnSuspend);
panel.add(new JLabel("Izberi"));
panel.add(cmbNacii);
this.getContentPane().add(lblImage, BorderLayout.CENTER);
this.getContentPane().add(panel, BorderLayout.SOUTH);
for (int j= 0;j<6;j++)
for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
final int percent = i;
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
it.updateBar(percent);
}
});
java.lang.Thread.sleep(delays[j]/100);
} catch (InterruptedException e) {
;
}
}
btnResume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start();
}
});
btnSuspend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stop();
}
});
cmbNacii.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stop();
br = cmbNacii.getSelectedIndex();
tekovnaNacija(br);
timer.start();
}
});
timer.start();
lblImage.setIcon(ikoni[0]);
lblImage.setHorizontalAlignment(JLabel.CENTER);
brAudioClip = audioClips[0];
brAudioClip.play();
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
br = (br + 1) % broj_na_nacii;
tekovnaNacija(br);
}
}
private void tekovnaNacija(int index) {
lblImage.setIcon(ikoni[index]);
cmbNacii.setSelectedIndex(index);
brAudioClip = audioClips[index];
brAudioClip.play();
timer.setDelay(delays[index]);
}
public void start() {
timer.start();
brAudioClip.play();
}
public void stop() {
timer.stop();
brAudioClip.stop();
}
}
class CountryCellRenderer extends JLabel implements ListCellRenderer {
private static final Color HIGHLIGHT_COLOR = new Color(0, 0, 128);
public CountryCellRenderer() {
setOpaque(true);
setIconTextGap(12);
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
String entry = (String) value;
setText(entry);
setIcon(new ImageIcon(getClass().getResource("image/" + entry + ".gif")));
if (isSelected) {
setBackground(HIGHLIGHT_COLOR);
setForeground(Color.white);
} else {
setBackground(Color.white);
setForeground(Color.black);
}
return this;
}
}
************************************************** **
and this is the progress bar class:
******************************
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
public class SwingProgressBarExample extends JPanel {
JProgressBar pbar;
static final int MY_MINIMUM = 0;
static final int MY_MAXIMUM = 100;
public SwingProgressBarExample() {
pbar = new JProgressBar();
pbar.setMinimum(MY_MINIMUM);
pbar.setMaximum(MY_MAXIMUM);
add(pbar);
}
public void updateBar(int newValue) {
pbar.setValue(newValue);
}
public static void main(String args[]) {
int z=5000,j=4000;
final SwingProgressBarExample it = new SwingProgressBarExample();
JFrame frame = new JFrame("Progress Bar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setContentPane(it);
frame.pack();
frame.setVisible(true);
for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
final int percent = i;
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
it.updateBar(percent);
}
});
java.lang.Thread.sleep(10);
} catch (InterruptedException e) {
;
}
}
}
}