Import Javax.media.;
Import java.awt.;
Import java.awt.event.;
Class MediaPlayer extends Frame implements ActionListener,
Controllerlistener, ItemListener
{
Player player;
Component VC, CC;
Boolean first = True, loop = false;
String CurrentDirectory;
MediaPlayer (String title)
{
Super (title);
Addwindowlistener
(New Windowadapter ()
{
public void windowclosing (WindowEvent e) {
The user clicks the Close button of the Window System menu
Call Dispose to execute windowclosed
Dispose ();
} public void windowclosed (WindowEvent e) {
if (player! = null) player.close ();
System.exit (0);
}
});
Menu m = new Menu ("file");
MenuItem mi = new MenuItem ("open");
Mi.addactionlistener (this);
M.add (MI);
M.addseparator ();
Checkboxmenuitem cbmi = new Checkboxmenuitem ("loop", false);
Cbmi.additemlistener (this);
M.add (CBMI);
M.addseparator ();
mi = new MenuItem ("Exit");
Mi.addactionlistener (this);
M.add (MI);
MenuBar MB = new MenuBar ();
Mb.add (m);
Setmenubar (MB);
SetSize (200, 200);
SetVisible (TRUE);
}
public void actionperformed (ActionEvent e)
{
if (E.getactioncommand (). Equals ("Exit"))
{
Call Dispose to execute windowclosed
Dispose ();
Return
}
FileDialog fd = new FileDialog (This, "Open media file",
Filedialog.load);
Fd.setdirectory (CurrentDirectory);
Fd.show ();
If the user discards the selection file, returns
if (fd.getfile () = = null) return;
CurrentDirectory = Fd.getdirectory ();
if (player! = null)
Player.close ();
Try
{
Player = Manager.createplayer (new Medialocator ("File:" + fd.getdirectory () + fd.getfile ()));
}
catch (java.io.IOException E2)
{
SYSTEM.OUT.PRINTLN (E2);
Return
}
catch (noplayerexception E2)
{
SYSTEM.OUT.PRINTLN ("Cannot find player.");
Return
}
if (player = = null)
{
System.out.println ("Unable to create player.");
Return
}
First = false;
Settitle (Fd.getfile ());
Player.addcontrollerlistener (this);
Player.prefetch ();
}
public void Controllerupdate (Controllerevent e)
{
The Controllerclosedevent event occurs when Player.close () is called.
If a visual part is present, the part should be dismantled (for the sake of consistency,
We do the same for the Control Panel Assembly)
if (e instanceof controllerclosedevent)
{
if (VC! = null)
{
Remove (VC);
VC = NULL;
}
if (cc! = null)
{
Remove (CC);
CC = NULL;
}
Return
}
if (e instanceof endofmediaevent)
{
if (loop)
{
Player.setmediatime (New Time (0));
Player.start ();
}
Return
}
if (e instanceof prefetchcompleteevent)
{
Player.start ();
Return
}
if (e instanceof realizecompleteevent)
{
VC = Player.getvisualcomponent ();
if (VC! = null)
Add (VC);
CC = Player.getcontrolpanelcomponent ();
if (cc! = null)
Add (cc, Borderlayout.south);
Pack ();
}
}
public void itemstatechanged (ItemEvent e)
{
loop =!loop;
}
public void Paint (Graphics g)
{
if (first)
{
int w = GetSize (). width;
int h = getsize (). Height;
G.setcolor (Color.Blue);
G.fillrect (0, 0, W, h);
Font f = new Font ("Dialoginput", Font.Bold, 16);
G.setfont (f);
FontMetrics fm = G.getfontmetrics ();
int swidth = Fm.stringwidth ("* * welcome");
G.setcolor (Color.White);
g.DrawString ("Welcome * * *",
(w-swidth)/2,
(H + getinsets (). Top)/2);
}
Call the Paint () method of the superclass frame, the paint () method calls the individual containers contained in the frame
and the Paint () method of the part, including the Control Panel assembly.
Super.paint (g);
}
Do not perform background cleanup to avoid flashing control Panel parts
public void Update (Graphics g)
{
Paint (g);
}
public static void Main (String [] args) {
New MediaPlayer ("Media Player 1.0");
}
}
Java Fifth time job