Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
Hi everyone on the forum.

I currently have this code in my "About Box" Java which allows for an image to display:

ImageIcon icon = createImageIcon("icon.gif", "The app icon.");
appIcon = new JLabel (icon);

I was wandering if it is possible to make it so that when the image is clicked on a brief aif sound clip is played, as a sort of easter egg. Is this possible? If so, does anyone know how to do it?
Ideally I would like it to play a random sound from an arraylist of sounds, but if this isn't possible then one sound will suffice.
 

coconn06

macrumors regular
Jun 14, 2003
197
0
King of Prussia, PA
Take a look here for sample code on loading and playing a sound clip within your app (this example sets up it's own Swing application that loads files from a directory and allows you to select which one you want to play).

As for getting the clip to play when you click the image: I'm not sure if you can register a listener for an image exactly, so you might need to place the image within it's own JPanel and register a listener on the panel. Something like this (doing this from memory so forgive my errors - check the Java docs):

Code:
JPanel imagePanel = new JPanel();
/* Code to add the image to the panel goes here */
imagePanel.addMouseListener(this); // this class must implement the MouseListener interface
public void mouseClicked(MouseEvent e) {
    soundPlayer.playRandomSound();
}

This code will set the current class as a MouseListener for your JPanel containing the image, so when the mouse is clicked it will call the mouseClicked method in your class.
 

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
OK, I've made some progress. I now have:

ImageIcon icon = createImageIcon("icon.gif", "The app icon.");
appIcon = new JLabel (icon);

JPanel imagePanel = new JPanel ();
imagePanel.add(appIcon);
imagePanel.addMouseListener(new MouseAdapter () {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(frame, "Hello.");
}
});

I've used the option pane as a way to show that the MouseListener works before implementing the sound code. However, I get an error on the JOptionPane line stating "cannot resolve symbol". Anyone know what I'm doing wrong?
 

Dave the Great

macrumors regular
Jan 27, 2004
160
0
stadidas said:
OK, I've made some progress. I now have:

ImageIcon icon = createImageIcon("icon.gif", "The app icon.");
appIcon = new JLabel (icon);

JPanel imagePanel = new JPanel ();
imagePanel.add(appIcon);
imagePanel.addMouseListener(new MouseAdapter () {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(frame, "Hello.");
}
});

I've used the option pane as a way to show that the MouseListener works before implementing the sound code. However, I get an error on the JOptionPane line stating "cannot resolve symbol". Anyone know what I'm doing wrong?

You might want to check out your import statements and make sure you have defined 'frame'.
 

coconn06

macrumors regular
Jun 14, 2003
197
0
King of Prussia, PA
Here's an app I wrote quickly which works, and shows the dialog box when the image is clicked. I hope this helps.

Code:
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class MyFrame extends JFrame {
    
    public MyFrame() {
        ImageIcon image = new ImageIcon("C:\\image.jpg");
        JLabel label = new JLabel(image);

        JPanel imagePanel = new JPanel ();
        imagePanel.add(label);
        imagePanel.addMouseListener(new MouseAdapter () {
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(null, "Hello.");
            }
        });
        
        setTitle("Test JFrame");
        Container content = getContentPane();
        content.add(imagePanel);
        this.pack();
        setVisible(true);
    }
    
    public static void main(String[] args) {
        MyFrame frame = new MyFrame();
    }
}
 

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
I now have this which works:

JPanel imagePanel = new JPanel ();
imagePanel.add(appIcon);
this.addMouseListener(new MouseAdapter () {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "Hello.");
}
});

Now, on to the sound! I'm going to use the AudioClip interface to achieve sound play back. Any problems I will let you know.
 

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
Nearly there. I have imported sun.audio.*; and I've now got this code:

JPanel imagePanel = new JPanel ();
imagePanel.add(appIcon);
this.addMouseListener(new MouseAdapter () {
public void mouseClicked(MouseEvent e) {
InputStream in = new FileInputStream(../david.aif);
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
JOptionPane.showMessageDialog(null, "Hello.");
}
});

I get an error on the InputStream line saying "illegal start of expression". I'm not quite sure how to fix this.
Also the "david.aif" file is in the root directory of the project, along with the java files, will the path I've used access this?

Any help appreciated.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
stadidas said:
I've used the quotation marks around ../david.aif, but now I just get "cannot resolve symbol.

Two things...

Post your entire class source.
Learn to use the Code tag, it will help with code readability.

My guess is you are missing imports or are referencing a variable that wasn't declared.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.