• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

IN NEED FAST HELP!!

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the frantic subject line, but i have two short hours left before I am flushed down the perverbial toilet.
This applet plays a few audio clips. I need it to run as an applicaton, too. I don't know how to do that. Can someone be my hero and adjust the code acordingly? Thank You.
Andrew
***
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import Chapter8.MyFrameWithExitHandling;
import java.net.URL;
import java.applet.*;
public class Lab14_1 extends JApplet implements ActionListener
{
// Delcare Audio Files
protected AudioClip playAudio;
protected AudioClip loopAudio;
protected AudioClip stopAudio;
// Declare Buttons
private static JButton jplay = new JButton("PLAY");
private static JButton jloop = new JButton("LOOP");
private static JButton jstop = new JButton("STOP");
public void init()
{
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(jplay);
p.add(jloop);
p.add(jstop);
getContentPane().add(p);
// Register Listeners
jplay.addActionListener(this);
jloop.addActionListener(this);
jstop.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
// Get the file name
String filename = null;
if (e.getSource() == jplay)
filename = "Chapter14/Chimp.wav";
else if (e.getSource() == jloop)
filename = "Chaper14/Chimp.wav";
if
{
playAudio.stop();
loopAudio.loop();
}
if (e.getSource() == jstop)
{
playAudio.stop();
loopAudio.stop();
}
createClip(filename).play();
}
public AudioClip createClip(String filename)
{
// Get the URL for the file name
URL url = this.getClass().getResource("Chimp/" + filename);
// Return the audio clip
return Applet.newAudioClip(url);
}
public static void main(String [] args)
{
// Create a frame
MyFrameWithExitHandling frame = new MyFrameWithExitHandling(
"Lab14_1 Application");
// Crate an instance of the applet
Lab14_1 applet = new Lab14_1();
// Add applet to the fray
frame.getContentPane().add(applet, BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.pack();
frame.center();
frame.setVisible(true);
}
}
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Java class named "Lab14_1" huh? Will I be getting into trouble with your computer science teacher?
Converting an applet into a frame is routine.
Extend JFrame instead of JApplet.
Applet's have an "init" method, frame's have a constructor. Therefore, code a constructor and move the "init" code into it (or call your "init" method from the new constructor).
Applet's handle sizing, a frame doesn't. Therefore, you need to pack the frame. Add "this.pack();" as the last line of your new constructor.
I think applet's and frame's have different default layout managers. Therefore, make sure you explicitly set the layout manager to what you need. (You've already done that.)
That's about it. To make "Lab14_1" an application, give it a main method. In the main method create an instance of "Lab14_1" and show it.
If you want "Lab14_1" to be EITHER a frame or an applet, then have "Lab14_1" sub-class from JPanel. Then create an applet class that adds a new instance of your JPanel class to itself, and also create a JFrame class that adds a new instance of your JPanel class to itself. That way "Lab14_1" stuff can be done inside an applet or inside a frame.
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic