Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Default Audio JApplet in a JFrame?

    Hi everyone, I'm relatively new to Java, and I have a question.
    So, for one of my programs, I wanted to have a JApplet in my JFrame, but am having trouble. I searched for a while, and eventually found some code that allowed me to implement a JApplet in JFrame, but the problem is that the part of the JApplet I want is to play music, and for some reason, when I put it in a JFrame, it gives me an error:
    java.lang.NullPointerException
    at java.applet.Applet.getDocumentBase(Unknown Source)
    at Muse.init(FrameTest.java:51)
    at FrameTest.<init>(FrameTest.java:11)
    at FrameTest.main(FrameTest.java:16)

    This is the code I'm using (Don't worry about the whole part about changing songs and playing, the error is where the try catch is):
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.AudioClip;
    
    public class SimpleFrame extends JFrame {
    
    	public SimpleFrame() throws HeadlessException {
    		super();
    		Music m = new Music();
    		m.init();
    		add(m, BorderLayout.NORTH);
    	}
    
    	public static void main(String[] args) {
    		SimpleFrame fa = new SimpleFrame();
    		fa.setSize(800, 600);
    		fa.setVisible(true);
    	}
    
    }
    
    class Music extends JApplet implements ActionListener
    {
    	private JComboBox playing; //a jcombobox
    	private int[] choice = new int[2]; //an integer array for different inputs
    	private AudioClip[] sounds = new AudioClip[9]; //audio array for all songs
    	private JButton play, stop; //jbuttons for playing and stopping
    	private AudioClip current, last; //AudioClips for the current playing music and the last played
    
    	
    	public void init() //init for the audio applet
    	{
    		setLayout(new FlowLayout());//sets layout		
    		String options[] = {"Track 1", "Track 2", "Track 3", "Track 4", "Track 5", "Track 6", "Track 7", "Track 8", "Track 9"};
    		//string array of music options
    		playing = new JComboBox(options); 
    		playing.setSelectedIndex(8); //sets number of options
    		playing.addActionListener(this); //adds ActionListener to the ComboBox
    		this.add(playing); //adds the ComboBox to the Applet
    		Buttons alot = new Buttons();	
    		play = new JButton("Play");
    		play.addActionListener(alot); //adds ActionListener to the button
    		add(play); //adds this button to the Applet
    		stop = new JButton("Stop");
    		stop.addActionListener(alot); //Adds ActionListener to the stop button
    		add(stop); //adds it to the Applet
    		//all the sounds
    		try
    		{
    			sounds[0] = getAudioClip( getDocumentBase(), "Track1.wav" );
    			sounds[1] = getAudioClip( getDocumentBase(), "Track2.wav" );
    			sounds[2] = getAudioClip( getDocumentBase(), "Track3.wav" );
    			sounds[3] = getAudioClip( getDocumentBase(), "Track4.wav" );
    			sounds[4] = getAudioClip( getDocumentBase(), "Track5.wav" );
    			sounds[5] = getAudioClip( getDocumentBase(), "Track9.wav" );
    			sounds[6] = getAudioClip( getDocumentBase(), "Track7.wav" );
    			sounds[7] = getAudioClip( getDocumentBase(), "Track8.wav" );
    			sounds[8] = getAudioClip( getDocumentBase(), "Track6.wav" );
    			current = sounds[8];
    		} //Try catch for all the audio files
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		JComboBox select = (JComboBox)e.getSource(); //detects users selection
            String name = (String)select.getSelectedItem(); //a string for the name of the selection
            choice[1] = Integer.parseInt(name);
            if(choice[1] != choice[0])
            {	
            	choice[0] = choice[1]; //sets choice[0] to the last played
            	changeMusic(); //changes the music if there is a new choice
            }
        }
    	public void changeMusic() //a method to change music from the last
    	{
    		last = current;
    		current = sounds[choice[1] - 1];
    	}
    	private class Buttons implements ActionListener //a class for the jbuttons
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if(e.getSource() == play) //if play is pressed
    			{
    				last.stop(); //end
    				current.loop(); // loops current clip
    			}
    			else if(e.getSource() == stop) //if stop is pressed
    			{
    				last.stop(); //stops the current clip
    			}
    		}
    	}
    }
    Apparently it's a problem with the getDocumentBase(), but I'm not sure what it is. A side note, if it helps any, the applet itself and all the components load. So, there a few things that can help me. One, if you can solve this problem. Two, if there is another way to use a JApplet with a JFrame (I am even OK with having a separate window for the applet).

    Also, just keep in mind that I am new, and make plenty of mistakes often, so this may be a very simple fix to you, but not for me. :)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Audio JApplet in a JFrame?

    Java Code:
    Music m = new Music();
    This doesn't make any sense in an application.

    Usually (J)Applets are designed to run in a very specific environment: a web browser. It is in cooperation with this environment that things like getDocumentBase() are implemented. I am not surprised it throws a runtime exception when it is called outside of a browser. After all, what should the URL be in such a case where there is no web page?

    If your intention is to play audio you could consider the static newAudioClip() method. Being a static method it does not require that any JApplet instance be created.

Similar Threads

  1. convert JFrame to JApplet
    By hrayr in forum AWT / Swing
    Replies: 1
    Last Post: 05-06-2012, 04:00 PM
  2. jframe inside japplet
    By ali.mohebbi in forum Java Applets
    Replies: 1
    Last Post: 07-19-2011, 01:31 AM
  3. JApplet in JFrame...
    By Rolf83 in forum Java Applets
    Replies: 8
    Last Post: 06-03-2011, 06:15 PM
  4. Making a JApplet into a JFrame into a .jar
    By aqualicy in forum Java Applets
    Replies: 2
    Last Post: 05-14-2010, 05:29 AM
  5. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 07:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •