FileDocCategorySizeDatePackage
JarResourceLoading.javaAPI DocExample2119Mon Jan 09 11:02:00 GMT 2006None

JarResourceLoading

public class JarResourceLoading extends JFrame implements ActionListener

Fields Summary
JButton
button
ImageIcon
buttonIcon
Clip
buhClip
public static final String
SOUND_PATH
public static final String
IMAGE_PATH
Constructors Summary
public JarResourceLoading()


       
        super ("Resources from .jar");
        // get image and make button
        URL imageURL = getClass().getClassLoader().getResource (IMAGE_PATH);
        System.out.println ("found image at " + imageURL);
        buttonIcon = new ImageIcon (imageURL);
        button = new JButton ("Click to Buh!", buttonIcon);
        button.setHorizontalTextPosition (SwingConstants.CENTER);
        button.setVerticalTextPosition (SwingConstants.BOTTOM);
        button.addActionListener (this);
        getContentPane().add (button);
        // load sound into Clip
        try {
            URL soundURL = getClass().getClassLoader().getResource (SOUND_PATH);
            System.out.println ("found sound at " + soundURL);
            Line.Info linfo = new Line.Info (Clip.class);
            Line line = AudioSystem.getLine (linfo);
            buhClip = (Clip) line;
            AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL);
            buhClip.open(ais);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

        System.out.println ("click!");
        if (buhClip != null) {
            buhClip.setFramePosition (0);
            buhClip.start();
        }
        else
            JOptionPane.showMessageDialog (this,
                                           "Couldn't load sound",
                                           "Error",
                                           JOptionPane.ERROR_MESSAGE);
    
public static final voidmain(java.lang.String[] args)

        JFrame frame = new JarResourceLoading();
        frame.pack();
        frame.setVisible(true);