10 package com.generalrobotix.ui.view.tdview;
13 import java.awt.event.*;
14 import java.awt.image.*;
18 import javax.media.control.*;
19 import javax.media.format.*;
23 import com.sun.image.codec.jpeg.*;
31 @SuppressWarnings(
"serial")
34 public boolean appFlag_=
false;
36 private Component cmpVisual_=
null;
37 private Component cmpOpe_=
null;
39 private FramePositioningControl frameCtrl_=
null;
40 private FrameGrabbingControl frameGrabCtrl_=
null;
42 private Object waitSync =
new Object();
43 private boolean stateTransitionOK =
true;
45 private final String STR_TITLE_=
"SMPlayer";
46 private final String STR_RIGHT_=
"(C) 2000 Kernel Inc";
72 this.addWindowListener(
74 public void windowClosing(WindowEvent evt) {
77 public void windowClosed(WindowEvent evt) {
85 MenuBar bar=
new MenuBar();
91 item.addActionListener(
new ActionListener(){
92 public void actionPerformed(ActionEvent e){
99 item.addActionListener(
new ActionListener(){
100 public void actionPerformed(ActionEvent e){
107 item.addActionListener(
new ActionListener(){
108 public void actionPerformed(ActionEvent e){
114 this.setMenuBar(bar);
118 if(_load(fileName)==
false)_load(
null);
121 this.setVisible(
true);
137 if (url.indexOf(
":") > 0 && (ml =
new MediaLocator(url)) !=
null)
140 if (url.startsWith(
File.separator)) {
141 if ((ml =
new MediaLocator(
"file:" + url)) !=
null)
144 String file =
"file:" + System.getProperty(
"user.dir") +
File.separator + url;
145 if ((ml =
new MediaLocator(file)) !=
null)
172 if (cmpVisual_!=
null){
173 this.getContentPane().remove(cmpVisual_);
177 this.getContentPane().remove(cmpOpe_);
182 p_.removeControllerListener(
this);
198 private boolean _load(String fileName) {
199 Component newVisual=
null;
200 Component newOpe=
null;
207 MediaLocator ml=_createMediaLocator(fileName);
213 newVisual =
new JPanel();
214 ((JPanel)newVisual).setPreferredSize(
new Dimension(160,120));
215 newVisual.setBackground(Color.black);
216 newOpe=
new JLabel(STR_RIGHT_);
217 this.setTitle(STR_TITLE_);
219 this.setTitle(
MessageBundle.
get(
"SimpleMoviePlayer.title.openning") + ml +
"...");
221 p_ = Manager.createProcessor(ml);
222 }
catch (Exception e) {
223 System.err.println(
"Failed to create a processor from the given url: " + e);
227 p_.addControllerListener(
this);
231 if (!_waitForState(Processor.Configured)) {
232 System.err.println(
"Failed to configure the processor.");
237 p_.setContentDescriptor(
null);
240 TrackControl tc[] = p_.getTrackControls();
243 System.err.println(
"Failed to obtain track controls from the processor.");
248 TrackControl videoTrack =
null;
250 for (
int i = 0;
i < tc.length;
i++) {
251 if (tc[
i].getFormat() instanceof VideoFormat) {
257 if (videoTrack ==
null) {
258 System.err.println(
"The input media does not contain a video track.");
266 cnts=p_.getControls();
267 for(
int i=0;
i<cnts.length;
i++){
268 if(cnts[
i] instanceof FramePositioningControl){
269 frameCtrl_=(FramePositioningControl)cnts[
i];
278 if (!_waitForState(Controller.Prefetched)) {
279 System.err.println(
"Failed to realize the processor.");
284 javax.media.Renderer renderer=
null;
286 cnts=videoTrack.getControls();
287 for(
int i=0;
i<cnts.length;
i++){
288 if(cnts[
i] instanceof javax.media.Renderer){
289 renderer=(javax.media.Renderer)cnts[
i];
296 cnts=renderer.getControls();
297 for(
int i=0;
i<cnts.length;
i++){
298 if(cnts[
i] instanceof FrameGrabbingControl){
299 frameGrabCtrl_=(FrameGrabbingControl)cnts[
i];
306 newVisual = p_.getVisualComponent();
308 JPanel panel=
new JPanel();
309 panel.setLayout(
new BorderLayout());
311 if ((cc = p_.getControlPanelComponent()) !=
null) {
312 panel.add(
"Center", cc);
314 JButton btn=
new JButton(
MessageBundle.
get(
"SimpleMoviePlayer.button.save"));
315 btn.addActionListener(
new ActionListener(){
316 public void actionPerformed(ActionEvent e){
321 panel.add(
"East", btn);
324 this.setTitle(STR_TITLE_ +
" -" + ml);
329 this.getContentPane().setLayout(
new BorderLayout());
330 cmpVisual_=newVisual;
331 if (cmpVisual_!=
null) {
332 getContentPane().add(
"Center", cmpVisual_);
336 if (cmpOpe_ !=
null) {
337 getContentPane().add(
"South", cmpOpe_);
342 if(frameCtrl_!=
null){
355 if (p_ ==
null)
return;
356 if (p_.getState()==Controller.Started)
360 JFileChooser chooser =
361 new JFileChooser(System.getProperty(
"user.dir"));
363 chooser.setDialogType(JFileChooser.SAVE_DIALOG);
364 filter.addExtension(
"jpg");
365 filter.setDescription(
"Jpeg Image");
366 chooser.setFileFilter(filter);
367 int returnVal = chooser.showSaveDialog(
this);
368 if(returnVal == JFileChooser.APPROVE_OPTION) {
370 new FileOutputStream(
371 chooser.getSelectedFile().getAbsolutePath()
373 JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(output);
374 enc.encode(_convertBufferedImage(frameGrabCtrl_.grabFrame() ));
378 }
catch(Exception exception){
390 MessageBundle.
get(
"SimpleMoviePlayer.dialog.message.save") + exception.getMessage()
400 JFileChooser chooser =
new JFileChooser(System.getProperty(
"user.dir"));
404 filter.addExtension(
"mpg");
405 filter.addExtension(
"avi");
406 filter.addExtension(
"mov");
407 filter.setDescription(
"Movie Files");
408 chooser.setFileFilter(filter);
409 int returnVal = chooser.showOpenDialog(
this);
410 if(returnVal == JFileChooser.APPROVE_OPTION) {
411 if(!_load(
"file:" + chooser.getSelectedFile().getAbsolutePath())){
435 System.out.println(
"Can't create Image in this format!");
438 BufferedImage bimg=
new BufferedImage(img.getWidth(
null),img.getHeight(
null),BufferedImage.TYPE_INT_RGB);
439 Graphics2D g=bimg.createGraphics();
440 g.drawImage(img,0,0,
null);
454 synchronized (waitSync) {
456 while (p_.getState() != state && stateTransitionOK)
458 }
catch (Exception e) {}
460 return stateTransitionOK;
470 if (evt instanceof ConfigureCompleteEvent ||
471 evt instanceof RealizeCompleteEvent ||
472 evt instanceof PrefetchCompleteEvent) {
473 synchronized (waitSync) {
474 stateTransitionOK =
true;
475 waitSync.notifyAll();
477 }
else if (evt instanceof ResourceUnavailableEvent) {
478 synchronized (waitSync) {
479 stateTransitionOK =
false;
480 waitSync.notifyAll();
492 public static void main(String [] args) {
494 if (args.length == 0) {
static final String get(String key)
static void main(String[] args)
Image createImage(Buffer buffer)
SimpleMoviePlayer(boolean appFlag)
#define null
our own NULL pointer
BufferedImage _convertBufferedImage(Buffer buf)
boolean _load(String fileName)
boolean _waitForState(int state)
void controllerUpdate(ControllerEvent evt)
MediaLocator _createMediaLocator(String url)
SimpleMoviePlayer(String fileName, boolean appFlag)
SimpleMoviePlayer(String fileName)
output(gif_dest_ptr dinfo, int code)