Creating the About Dialog Box
Creating the frame & menus for the media player. In other words creating a subclass of Frame.
Creating a subclass of WindowAdapter
Acting upon various user inputs i.e events provided. Also contains the logic for playing a Audio Clip.
This class had been used to create a dialog box that will select media files to the system's default playlist.
This class is designed to create a dialog box where there will be start button to play all the songs in the database,& another button to close it.
This is the main controlling class of this package. Creates a frame window named 'Gojkid Media Player'.
package MediaPlayer;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="AboutDialog" width=250 height=250>
</applet>
*/
public class AboutDialog extends Dialog implements ActionListener{
Font f;
@SuppressWarnings("LeakingThisInConstructor")
AboutDialog(Frame parent,String title)
{
super(parent, title, false);
setLayout(new FlowLayout());
setSize(300, 200);
//Setting up the font
f=new Font("Dialog",Font.BOLD,12);
setFont(f);
//Creating a button to close this dialog box
Button b;
add(b = new Button("Close"));
b.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae) {
dispose();
}
@Override
public void paint(Graphics g) {
g.drawString("Simple Wav media player", 20, 70);
g.drawString("Version: 1.0", 20, 100);
g.drawString("Vendor: Gojkid", 20, 130);
g.drawString("Homepage: http://gojkid.in", 20, 160);
}
}
Creating the frame & menus for the media player. In other words creating a subclass of Frame.
package MediaPlayer;
import java.awt.*;
class MenuFrame extends Frame {
String msg = "";
String path;
Font f;
Button replay,stop;
MenuFrame(String title) {
super(title);
f=new Font("Monospaced",Font.BOLD,14);
setFont(f);
// create menu bar and add it to frame
MenuBar mbar = new MenuBar();
setMenuBar(mbar);
// create the menu items
Menu file = new Menu("File");
MenuItem item1, item2,item3,item6,item7;
file.add(item1 = new MenuItem("Open..."));
file.add(item6 = new MenuItem("Edit Playlist..."));
file.add(item7 = new MenuItem("Play all..."));
file.add(new MenuItem("-"));
file.add(item2 = new MenuItem("Close..."));
file.add(item3 = new MenuItem("Quit..."));
mbar.add(file);
Menu help = new Menu("Help");
MenuItem item4,item5;
help.add(item4 = new MenuItem("About..."));
help.add(new MenuItem("-"));
help.add(item5 = new MenuItem("Close..."));
mbar.add(help);
replay=new Button("Replay");
add(replay);
stop=new Button("Stop");
add(stop);
// create an object to handle action and item events
MyMenuHandler handler = new MyMenuHandler(this);
// register it to receive those events
item1.addActionListener(handler);
item6.addActionListener(handler);
item7.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
replay.addActionListener(handler);
stop.addActionListener(handler);
// create an object to handle window events
MyWindowAdapter adapter = new MyWindowAdapter(this);
// register it to receive those events
addWindowListener(adapter);
setLayout(new FlowLayout());
setSize(600, 300);
setVisible(true);
}
@Override
public void paint(Graphics g)
{
setBackground(new Color(200,200,200));
if(msg.contains("\\"))
g.drawString("Now Playing :"+msg, 10, 75);
else
g.drawString(msg, 10, 150);
}
@Override
public void dispose() {
}
}
Creating a subclass of WindowAdapter
package MediaPlayer;
import java.awt.event.*;
class MyWindowAdapter extends WindowAdapter {
MenuFrame menuFrame;
public MyWindowAdapter(MenuFrame menuFrame) {
this.menuFrame = menuFrame;
}
@Override
public void windowClosing(WindowEvent we) {
menuFrame.dispose();
}
}
Acting upon various user inputs i.e events provided. Also contains the logic for playing a Audio Clip.
package MediaPlayer;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.net.*;
class MyMenuHandler implements ActionListener, ItemListener {
MenuFrame menuFrame;
AudioClip bach;
Button play, stop;
String msg = "";
public MyMenuHandler(MenuFrame menuFrame) {
this.menuFrame = menuFrame;
}
// Handle action events
@Override
public void actionPerformed(ActionEvent ae) {
String arg = (String)ae.getActionCommand();
// Activate file selector when open is selected.
if(arg.equals("Open...")) {
FileDialog fd = new FileDialog(menuFrame, "File Selector",FileDialog.LOAD);
fd.setVisible(true);
msg=fd.getDirectory()+fd.getFile();
try{
//Select the file to be played
bach = Applet.newAudioClip(new URL("file:"+msg));
}
catch (MalformedURLException mfe){
}
//Playing the file
bach.play();
}
// Activate Playlist Dialog when Edit playlist is selected
else if(arg.equals("Edit Playlist..."))
{
PlaylistDialog pd = new PlaylistDialog(menuFrame, "Playlist Editor");
pd.setVisible(true);
}
// Activate Play All Dialog when Play All is selected
else if(arg.equals("Play all..."))
{
PlayAllDialog pad=new PlayAllDialog(menuFrame,"Play all files from database");
pad.setVisible(true);
}
// Activate About Dialog when About is selected.
else if(arg.equals("About...")){
AboutDialog ad = new AboutDialog(menuFrame, "About");
ad.setVisible(true);
}
//Playing the file again after its stopped
else if(arg.equals("Replay"))
{
try{
//Select the file to be played
bach = Applet.newAudioClip(new URL("file:"+msg));
}
catch (MalformedURLException mfe){
}
//Playing the file
bach.play();
}
//Stopping the Audio clip when stop button is pressed
else if(arg.equals("Stop"))
{
if(bach!=null){
bach.stop();
bach=null;
}
else
msg="Error : File Not Selected";
}
//Exiting JVM when Quit is selected
else if(arg.equals("Quit..."))
{
msg=" Thank you , for using our media player";
dispose();
System.exit(1);
}
menuFrame.msg = msg;
menuFrame.repaint();
}
@Override
public void itemStateChanged(ItemEvent ie) {
menuFrame.repaint();
}
private void dispose() {
}
}
This class had been used to create a dialog box that will select media files to the system's default playlist.
package MediaPlayer;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/*
<applet code="PlaylistDialog" width=600 height=600>
</applet>
*/
public class PlaylistDialog extends Dialog implements ActionListener{
Font f;
Button open,close,reset;
MenuFrame menuframe;
String msg;
Graphics g;
private static String path="C:\\playlist.txt"; //path of your playlist file
@SuppressWarnings("LeakingThisInConstructor")
PlaylistDialog(Frame parent,String title)
{
super(parent, title, false);
setLayout(new FlowLayout());
setSize(600, 600);
//Setting up the font
f=new Font("Dialog",Font.BOLD,12);
setFont(f);
add(open = new Button("Add to playlist"));
open.addActionListener(this);
add(reset = new Button("Reset Playlist"));
reset.addActionListener(this);
add(close = new Button("Close"));
close.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae) {
String arg = (String)ae.getActionCommand();
// Activate file selector when open is selected.
if(arg.equals("Add to playlist")) {
FileDialog fd = new FileDialog(menuframe, "File Selector",FileDialog.LOAD);
fd.setVisible(true);
msg=fd.getDirectory()+fd.getFile();
try{
FileWriter fw = new FileWriter(path, true);
fw.write(msg);
fw.write("\n");
fw.close();
}
catch (Exception e){
}
}
// Flush out the elements of the playlist
else if(arg.equals("Reset Playlist"))
{
try{
FileWriter fw = new FileWriter(path);
fw.write("");
fw.close();
}
catch (Exception e){
}
}
// Activate About Dialog when About is selected.
else if(arg.equals("Close")){
dispose();
}
}
@Override
public void paint(Graphics g) {
if(msg!=null)
g.drawString(msg+" had been added to playlist successfully", 20, 130);
}
}
This class is designed to create a dialog box where there will be start button to play all the songs in the database,& another button to close it.
package MediaPlayer;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.applet.*;
import java.net.*;
/*
<applet code="PlayAllDialog" width=600 height=600>
</applet>
*/
public class PlayAllDialog extends Dialog implements ActionListener{
Font f;
Button close,start;
MenuFrame menuframe;
String t;
Graphics g;
AudioClip bach;
private static String path="C:\\playlist.txt"; //path of your playlist file
@SuppressWarnings("LeakingThisInConstructor")
PlayAllDialog(Frame parent,String title)
{
super(parent, title, false);
setLayout(new FlowLayout());
setSize(600, 600);
//Setting up the font
f=new Font("Dialog",Font.BOLD,12);
setFont(f);
add(start = new Button("Start"));
start.addActionListener(this);
add(close = new Button("Close Window"));
close.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae) {
String arg = (String)ae.getActionCommand();
// Activate file selector when open is selected.
if(arg.equals("Start")){
try
{
FileReader fr1 = new FileReader(path);
BufferedReader br1 = new BufferedReader(fr1);
while ((t = br1.readLine()) != null) {
bach = Applet.newAudioClip(new URL("file:"+t));
bach.play();
Thread.sleep(10000L);
bach.stop();
}
}
catch(Exception e)
{
}
}
else if(arg.equals("Close Window"))
{
dispose();
}
}
@Override
public void paint(Graphics g) {
if(t!=null)
g.drawString("Now playing :"+t, 20, 130);
}
}
This is the main controlling class of this package. Creates a frame window named 'Gojkid Media Player'.
package MediaPlayer;
import java.applet.*;
import java.awt.*;
public class MediaPlayer extends Applet{
Frame f;
@Override
public void init() {
f = new MenuFrame("Gojkid Media Player");
int width = 600;
int height = 300;
setSize(width, height);
f.setSize(width, height);
f.setVisible(true);
}
@Override
public void start() {
f.setVisible(true);
}
@Override
public void stop() {
f.setVisible(false);
}
@Override
public void destroy(){
f.setVisible(false);
}
}
0 Comments On This Entry
Recent Entries
-
Implementing Gauss-Elimination Method to solve simultaneous linear equations
on Jun 27 2012 11:19 PM
-
-
-
-
Recent Comments
My Blog Links
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
|
|



Leave Comment









|