Topic: 求助~!请帮我分析下面的CODE块

  Print this page

1.求助~!请帮我分析下面的CODE块 Copy to clipboard
Posted by: 22750124
Posted on: 2005-12-15 23:33

package mediaplayer;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;
import javax.media.*;

/**
*
* @author 权威
* @function AMP媒体播放器
* @version 1.0
* @date 2005-12-9
*
*/

public class AMP
extends Frame
implements ActionListener,ItemListener,ControllerListener{

  private File oldFile,newFile;
  private BasicDirectoryModel bdm;
  private String[] s;
  private Player player;
  private Component visualcomponent,controlcomponent;
  private boolean firstState=true,loopState=false;
  private String currentDirectory;

  private AMPMenuBar mb=new AMPMenuBar();

  public AMP(String s) {
    super(s);
    //给菜单栏中加监听器
    mb.open.addActionListener(this);
    mb.exit.addActionListener(this);
    mb.loop.addActionListener(this);
    mb.aboutAMP.addActionListener(this);

    //将菜单栏加入窗体中
    this.setMenuBar(mb);
    this.addWindowListener(

        new WindowAdapter(){

          public void windowClosing(WindowEvent e){
            dispose();
            //这里没有用System.exit(0)
            //因为用户点击关闭按钮调用dispose()
            //以执行windowClosed
          }

          public void windowClosed(WindowEvent e){
            //关闭未关闭的player
            if(player!=null){
              player.close();
              bdm.renameFile(newFile,oldFile);
            }
            System.exit(0);
          }
        });

    this.setSize(500,300);
    this.setVisible(true);
  }

  public void actionPerformed(ActionEvent e){

    if(e.getActionCommand().equals("退出")){
      //调用dispose()以便执行windowClosed
      dispose();
      return;
    }

    if(e.getActionCommand().equals("关于 AMP 1.0 播放器")){
      JDialog jd=new JDialog(this,"关于 AMP 1.0 播放器",true);
      jd.getContentPane().add(new JLabel(" 欢迎您使用 AMP1.0 播放器"));
      jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
      jd.setSize(200,100);
      jd.setVisible(true);
    }

    if(e.getActionCommand().equals("打开...")){
      FileDialog openFD=new FileDialog(
          this,"打开媒体文件",FileDialog.LOAD);
      openFD.setDirectory(currentDirectory);
      openFD.setVisible(true);
      //如果用户放弃选择文件,则返回

      if(openFD.getFile()==null){
        return;
      }

      currentDirectory=openFD.getDirectory();

      if(player!=null){
        player.close();
        bdm.renameFile(newFile,oldFile);
      }

      JFileChooser openFile=new JFileChooser(currentDirectory);
      bdm=new BasicDirectoryModel(openFile);
      s=openFD.getFile().split("\\.");
      oldFile=new File(currentDirectory+openFD.getFile());
      newFile=new File(currentDirectory+s[0]+".mpg");

      //创建player
      try{
        if(bdm.renameFile(oldFile,newFile)){
          player=Manager.createPlayer(
              new MediaLocator(
                  "file:"+openFD.getDirectory()+
                  s[0]+".mpg"));
        }
      }catch(IOException ioe){
        System.err.println(ioe);
        bdm.renameFile(newFile,oldFile);
        return;
      }catch(NoPlayerException npe){
        System.err.println("不支持的格式");
        bdm.renameFile(newFile,oldFile);
        return;
      }

      if(player==null){
        System.out.println("无法创建播放器");
        bdm.renameFile(newFile,oldFile);
        return;
      }

      firstState=false;
      this.setTitle(openFD.getFile());
      player.addControllerListener(this);
      //player加载
      player.prefetch();
    }
  }

  public void itemStateChanged(ItemEvent e){
    //设置loopState的value
    loopState=!loopState;
  }

  public void controllerUpdate(ControllerEvent e){

    //调用player.close()时ControllerClosedEvent事件出现
    //如果存在视觉部件,则该部件应该拆除(为一致起见,
    //我们对控制面板部件也执行同样的操作)

    if(e instanceof ControllerClosedEvent){

      if(visualcomponent!=null){
        remove(visualcomponent);
        visualcomponent=null;
      }

      if(controlcomponent!=null){
        remove(controlcomponent);
        controlcomponent=null;
      }

      return;
    }

    if(e instanceof EndOfMediaEvent){
      //结束时的循环处理
      if(loopState){
        player.setMediaTime(new Time(0));
        player.start();
      }

      return;
    }

    if(e instanceof PrefetchCompleteEvent){
      player.start();
      return;
    }

    if(e instanceof RealizeCompleteEvent){

      visualcomponent=player.getVisualComponent();

      if(visualcomponent!=null){
        add(visualcomponent);
      }

      controlcomponent=player.getControlPanelComponent();

      if(controlcomponent!=null){
        add(controlcomponent,BorderLayout.SOUTH);
      }

      this.pack();
    }
  }

  public void paint(Graphics g){

    if(firstState){

      int width=this.getSize().width;
      int height=this.getSize().height;

      g.setColor(new Color(142,176,219));
      g.fillRect(0,0,width,height);

      Font f=new Font("DialogInput",Font.BOLD,26);
      g.setFont(f);

      FontMetrics fm=g.getFontMetrics();
      int swidth=fm.stringWidth("☆☆☆ 欢迎 ☆☆☆");

      g.setColor(Color.BLACK);
      g.drawString("☆☆☆ 欢迎 ☆☆☆",
          (width-swidth)/2,
          (height+this.getInsets().top)/2);
    }

      //调用超类JFrame的paintComponent(),
      //该paintComponent()方法将调用JFrame包含的各个容器具
      //和部件(包括控制面板部件)的paintComponent方法

    super.paint(g);
  }

  //不执行背景清除操作,以免控制面板部件闪烁
  public void update(Graphics g){
    paint(g);
  }

  /**
   * @param args
   */

  public static void main(String[] args) {
    new AMPJprogressBar("AMP 1.0 媒体播放器");
    new AMP("AMP 1.0 媒体播放器");
  }

}
为什么在连打开媒体文件后,会有部分媒体文件的名字没有被改回。仍为*.mpg

2.Re:求助~!请帮我分析下面的CODE块 [Re: 22750124] Copy to clipboard
Posted by: ftang
Posted on: 2005-12-16 04:27

man...what you want?

you want us read your code and tell where is the problem?

com'on...this is not fair...

why don't you write down what you want to do in this code and just post the code you think where the problem at?

that way it is easier for the "high-handers" like gongshi, why and floater to point out the problemMoon.

and plus you should "禁止在这个帖子中使用 Jute 标记" and "禁止在这个帖子中使用笑脸标记" so there would be no Smile in the code

3.Re:求助~!请帮我分析下面的CODE块 [Re: 22750124] Copy to clipboard
Posted by: 22750124
Posted on: 2005-12-17 19:36

thanks


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923