Wednesday, May 21, 2008

A Simple Graphics Editor in JAVA

After a bit of practice on JMF..now I wanna get a hand on java awt package. So, what I thought is I am going to make a simple graphics editor in java...that can draw different 2D geometrical figures and manipulate those figures. I have just started it...lets see how far can I get. If you guys have any suggestions please feel free to leave comments.

thanks.

Sunday, May 11, 2008

A simple media player in Java

I have been using JMF to write some simple java programs that can play some audio and video file. Finally, I decided that I am going to make a simple media player in java. Currently I am working on that..my player is ready to go but with minimal features. Once I am done with all the major functions, I will post the whole code here. If you guys doing the same thing and need some help please feel free to leave any comments. I will be more than happy to discuss with you guys.

Here is my first file. Copy and paste this code segment in a java file. This file basically implemnts the JMF package.

import java.awt.*;
import java.io.*;
import javax.media.*;
import java.net.URL;
import javax.swing.*;

public class DilipPlayer extends JPanel{
//Global Variable declaration
Player mediaPlayer;
Component video, controls;

public DilipPlayer(URL mediaURL){
setLayout(new BorderLayout());
Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
//add a line of code here if u want the lightweight swing component support
try
{
//Create a player to play the media specified in the mediaURL
mediaPlayer = Manager.createRealizedPlayer(mediaURL);
//get the components for the video and the playback
video = mediaPlayer.getVisualComponent();
controls = mediaPlayer.getControlPanelComponent();
if(video != null)
add(video, BorderLayout.CENTER); //and video component
if(controls != null)
add(controls, BorderLayout.SOUTH); //and controls component
mediaPlayer.start(); //start playing the media
}//end try
catch(NoPlayerException nomediaPlayer){
System.out.println("No media player found");
}//end catch1
catch(CannotRealizeException cannotrelaizeexception){
System.out.println("Could not realize media player");
}//end catch2
catch(IOException ioexception){
System.out.println("Cannot read from the source");
}//end catch3
}//end constructor
//This code is being written here for testing purpose..might work..might not work..
//method to implement stop
public void stopPlaying(){
mediaPlayer.stop();
}//end method stopMusic

public void pauseMusic(){

}//end method pauseMusic
}//end class


The second file with main method and UI programming.

/*
* DilipPlayer.java May 12, 2008
*
* Copyright (c) 2008 by Dilip Yogi. All rights reserved.
*
* I reserve all rights to this software. You may use it and
* distribute it freely, provided you do not remove this header
* and attribute partial credit to the author, Dilip Yogi (yogidilip@gmail.com.
*/
import javax.swing.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import javax.media.*;

public class DilipPlayer3 extends JFrame implements ActionListener{

//Global variable declaration
JFrame mediatest = new JFrame("Dipware Media Player Version 2.0");
DilipPlayer mediapanel;
JMenuBar menubar;
JMenu fileMenu, playerMenu,aboutMenu;
JMenuItem openSubMenu, exitSubMenu, aboutSubMenu, playpauseSubMenu, stopSubMenu, recordSubMenu;
JMenuItem fullScreenSubMenu, playlistSubMenu, toolbarSubMenu;
JToolBar toolbar;
//buttons for toolbar
JButton openButton, playButton, pauseButton, stopButton, fullscreenButton, playlistButton, recordButton;
URL mediaURL;

//constructor declaration. All the UI designing stuffs is being done here
public DilipPlayer3(){
//Swing components creation and attachment
menubar = new JMenuBar();
fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
playerMenu = new JMenu("Player");
playerMenu.setMnemonic('P');
aboutMenu = new JMenu("About");
aboutMenu.setMnemonic('A');
openSubMenu = new JMenuItem("Open", 'O');
exitSubMenu = new JMenuItem("Exit", 'X');
aboutSubMenu = new JMenuItem("About Dipware Player", 'B');
playpauseSubMenu = new JMenuItem("Play/Pause", 'P');
stopSubMenu = new JMenuItem("Stop", 'S');
recordSubMenu = new JMenuItem("Record", 'R');
fullScreenSubMenu = new JMenuItem("Full Screen", 'Z');
playlistSubMenu = new JMenuItem("PlayList", 'L');
toolbarSubMenu = new JMenuItem("View Toolbar", 'T');

menubar.add(fileMenu);
menubar.add(playerMenu);
menubar.add(aboutMenu);

//add sub menus
fileMenu.add(openSubMenu);
fileMenu.add(exitSubMenu);
fileMenu.insertSeparator(1);
playerMenu.add(playpauseSubMenu);
playerMenu.add(stopSubMenu);
playerMenu.add(recordSubMenu);
playerMenu.add(fullScreenSubMenu);
playerMenu.add(playlistSubMenu);
playerMenu.add(toolbarSubMenu);
playerMenu.insertSeparator(3);
playerMenu.insertSeparator(5);
aboutMenu.add(aboutSubMenu);

//create toolbar and buttons in toolbar here
toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL);

openButton = new JButton("Open");
playButton = new JButton("Play");
//playButton.setIcon(new ImageIcon("play.gif","play"));
pauseButton = new JButton("Pause");
stopButton = new JButton("Stop");
recordButton = new JButton("Rec");
fullscreenButton = new JButton("Full Screen");
playlistButton = new JButton("List");

toolbar.add(openButton);
toolbar.add(playButton);
toolbar.add(pauseButton);
toolbar.add(stopButton);
toolbar.add(recordButton);
toolbar.add(fullscreenButton);
openButton.addActionListener(this);
playButton.addActionListener(this);
pauseButton.addActionListener(this);
stopButton.addActionListener(this);
recordButton.addActionListener(this);
fullscreenButton.addActionListener(this);
playlistButton.addActionListener(this);

//add the ActionListener here
openSubMenu.addActionListener(this);
exitSubMenu.addActionListener(this);
playpauseSubMenu.addActionListener(this);
stopSubMenu.addActionListener(this);
recordSubMenu.addActionListener(this);
fullScreenSubMenu.addActionListener(this);
playlistSubMenu.addActionListener(this);
toolbarSubMenu.addActionListener(this);
aboutSubMenu.addActionListener(this);

//mediatest frame is being made visible here
mediatest.pack();
mediatest.setVisible(true);
mediatest.setSize(700, 450);
mediatest.setJMenuBar(menubar);
mediatest.add(toolbar, BorderLayout.NORTH);
}//end constructor

//main method
public static void main(String [] args){
DilipPlayer3 obj1 = new DilipPlayer3();
}//end main method

//method openFile
public void openFile(){
//show open dialog
//Getting the URL and passing it to the player

JFileChooser filechooser = new JFileChooser();
int result= filechooser.showOpenDialog(null);
if(result== JFileChooser.APPROVE_OPTION)//user choose a file
{
mediaURL = null;
try{
mediaURL = filechooser.getSelectedFile().toURL();

}//end try
catch(MalformedURLException malformedURLException){
System.out.println("Could not create URL for the file");
}//end catch
if(mediaURL != null){mediatest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mediapanel = new DilipPlayer(mediaURL);
String currplay=mediaURL.toString();
JOptionPane.showMessageDialog(mediatest, "You are playing " +currplay);
mediatest.add(mediapanel);
}//end if
}//end if
}//end method openFile

//event handling in menus starts here

public void actionPerformed(ActionEvent e){
//write the implementation code here
if((e.getSource()==openSubMenu)||(e.getSource()== openButton)){
openFile();
}//open file
else if(e.getSource()==exitSubMenu){
//write code to close window
System.exit(0);
}//end if for exit here
else if((e.getSource() == playpauseSubMenu)||(e.getSource()==playButton)){
//write code here
JOptionPane.showMessageDialog(mediatest, "Keep patience, this feature is yet to implement");
}//end if for playpauseSubMenu
else if((e.getSource()== stopSubMenu)||(e.getSource()==stopButton)){
//write code here
stopMusic();
JOptionPane.showMessageDialog(mediatest, "This feature is in testing phase");
}//end if for stopSubMenu


else if((e.getSource()== recordSubMenu)||(e.getSource()== recordButton)){
JOptionPane.showMessageDialog(mediatest, "Keep patience, this feature is yet to implement");
}//end if recordSubMenu
else if((e.getSource() == fullScreenSubMenu)||(e.getSource()==fullscreenButton)){
JOptionPane.showMessageDialog(mediatest, "Keep patience, this feature is yet to implement");
}//end if for fullScreenSubMenu
else if((e.getSource() == playlistSubMenu)|| (e.getSource()== playlistButton))
{
JOptionPane.showMessageDialog(mediatest, "Keep patience, this feature is yet to implement");
}//end if for playlistSubMenu
else if(e.getSource()== toolbarSubMenu){
JOptionPane.showMessageDialog(mediatest, "Keep patience, this feature is yet to implement");
}
else if(e.getSource()== aboutSubMenu){
JOptionPane.showMessageDialog(mediatest, "Dipware Media Player Version 2.0. (C) 2008 Dilip Yogi");
}//end if for aboutSubMenu
}//end method actionPerformed

//this code is being tested
public void stopMusic(){
DilipPlayer obj1 = new DilipPlayer(mediaURL);
obj1.stopPlaying();
}//end stopMusic



}//end class

You have to download and install JMF (Java Media Framework) for this code to work.

thanks,

Wednesday, May 7, 2008

Using JMF in Eclipse

I was trying to use JMF API. I was thinking of making some applets that can play audio and video. I downloaded the JMF API from the sun site. Since long I have been using Eclipse as the IDE for java development. But, I was having hard time adding JMF in Eclipse. My java programs were not recognizing the jmf classes. Finally, here is how I solved the problem.
  • Open Eclipse
  • Go to Project Properties
  • Click on Java Build Path
  • Go to the tab libraries
  • Click on button "Add external Jar Files"
  • show the jmf.jar file and then click on open

















This should be all to add jmf in Eclipse. Now your java program should be able to recognize the jmf classes.