forked from vizay08/Java_physics_library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MDIApp.java
202 lines (179 loc) · 5.8 KB
/
MDIApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import javax.media.*;
import com.sun.media.ui.*;
import javax.media.protocol.*;
import javax.media.protocol.DataSource;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Vector;
public class MDIApp extends Frame {
/*************************************************************************
* MAIN PROGRAM / STATIC METHODS
*************************************************************************/
public static void main(String args[]) {
MDIApp mdi = new MDIApp();
}
static void Fatal(String s) {
MessageBox mb = new MessageBox("JMF Error", s);
}
/*************************************************************************
* VARIABLES
*************************************************************************/
JMFrame jmframe = null;
JDesktopPane desktop;
FileDialog fd = null;
CheckboxMenuItem cbAutoLoop = null;
Player player = null;
Player newPlayer = null;
String filename;
/*************************************************************************
* METHODS
*************************************************************************/
public MDIApp() {
super("Java Media Player");
// Add the desktop pane
setLayout( new BorderLayout() );
desktop = new JDesktopPane();
desktop.setDoubleBuffered(true);
add("Center", desktop);
setMenuBar(createMenuBar());
setSize(640, 480);
setVisible(true);
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (Exception e) {
System.err.println("Could not initialize java.awt Metal lnf");
}
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
} );
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));
}
private MenuBar createMenuBar() {
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String command = ae.getActionCommand();
if (command.equals("Open")) {
if (fd == null) {
fd = new FileDialog(MDIApp.this, "Open File",
FileDialog.LOAD);
fd.setDirectory("/movies");
}
fd.show();
if (fd.getFile() != null) {
String filename = fd.getDirectory() + fd.getFile();
openFile("file:" + filename);
}
} else if (command.equals("Exit")) {
dispose();
System.exit(0);
}
}
};
MenuItem item;
MenuBar mb = new MenuBar();
// File Menu
Menu mnFile = new Menu("File");
mnFile.add(item = new MenuItem("Open"));
item.addActionListener(al);
mnFile.add(item = new MenuItem("Exit"));
item.addActionListener(al);
// Options Menu
Menu mnOptions = new Menu("Options");
cbAutoLoop = new CheckboxMenuItem("Auto replay");
cbAutoLoop.setState(true);
mnOptions.add(cbAutoLoop);
mb.add(mnFile);
mb.add(mnOptions);
return mb;
}
/**
* Open a media file.
*/
public void openFile(String filename) {
String mediaFile = filename;
Player player = null;
// URL for our media file
URL url = null;
try {
// Create an url from the file name and the url to the
// document containing this applet.
if ((url = new URL(mediaFile)) == null) {
Fatal("Can't build URL for " + mediaFile);
return;
}
// Create an instance of a player for this media
try {
player = Manager.createPlayer(url);
} catch (NoPlayerException e) {
Fatal("Error: " + e);
}
} catch (MalformedURLException e) {
Fatal("Error:" + e);
} catch (IOException e) {
Fatal("Error:" + e);
}
if (player != null) {
this.filename = filename;
JMFrame jmframe = new JMFrame(player, filename);
desktop.add(jmframe);
}
}
}
class JMFrame extends JInternalFrame implements ControllerListener {
Player mplayer;
Component visual = null;
Component control = null;
int videoWidth = 0;
int videoHeight = 0;
int controlHeight = 30;
int insetWidth = 10;
int insetHeight = 30;
boolean firstTime = true;
public JMFrame(Player player, String title) {
super(title, true, true, true, true);
getContentPane().setLayout( new BorderLayout() );
setSize(320, 10);
setLocation(50, 50);
setVisible(true);
mplayer = player;
mplayer.addControllerListener((ControllerListener) this);
mplayer.realize();
addInternalFrameListener( new InternalFrameAdapter() {
public void internalFrameClosing(InternalFrameEvent ife) {
mplayer.close();
}
} );
}
public void controllerUpdate(ControllerEvent ce) {
if (ce instanceof RealizeCompleteEvent) {
mplayer.prefetch();
} else if (ce instanceof PrefetchCompleteEvent) {
if (visual != null)
return;
if ((visual = mplayer.getVisualComponent()) != null) {
Dimension size = visual.getPreferredSize();
videoWidth = size.width;
videoHeight = size.height;
getContentPane().add("Center", visual);
} else
videoWidth = 320;
if ((control = mplayer.getControlPanelComponent()) != null) {
controlHeight = control.getPreferredSize().height;
getContentPane().add("South", control);
}
setSize(videoWidth + insetWidth,
videoHeight + controlHeight + insetHeight);
validate();
mplayer.start();
} else if (ce instanceof EndOfMediaEvent) {
mplayer.setMediaTime(new Time(0));
mplayer.start();
}
}
}