jmf
JavaStudy Techtips Translation
Java Developer Connection SM (JDC) 테크 팁 ,2 월 19, 2002.
이 팁은 Java TM 2 SDK, Standard Edition, v 1.3 를 사용하여 개발되었습니다.
본 문서의 원문은 Tech Tips 에 있습니다.
|
JavaStudy Techtips Translation list
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.media.*;
import javax.media.protocol.*;
public class MediaPlayer extends JFrame implements ActionListener, ControllerListener{
private JPanel contentPanel;
private Component controlComponent;
private Component visualComponent;
private Player player;
public MediaPlayer(String title) {
super(title);
/* 열기 메뉴 추가 */
JMenuBar menuBar = new JMenuBar();
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
JMenu menu = new JMenu("파일");
JMenuItem item = new JMenuItem("열기");
item.setActionCommand("open");
item.addActionListener(this);
menu.add(item);
menuBar.add(menu);
/*메뉴바 설정 */
setJMenuBar(menuBar);
setSize(200, 200);
setVisible(true);
}
/* Player를 만들고, 리스너를 등록한다. */
public void makePlayer(String filePath) {
MediaLocator locator = new MediaLocator(filePath);
try {
/* DataSource 생성 */
DataSource dataSource = Manager.createDataSource(locator);
/* 현재 동작중인 Player가 있다면 닫는다. */
if(player != null) {
player.close();
}
/* Player 생성 */
player = Manager.createPlayer(dataSource);
/* 리스너 등록 */
player.addControllerListener(this);
}catch(IOException e){
System.out.println(e);
}catch(NoDataSourceException e) {
System.out.println("DataSource를 만들 수 없습니다.");
}catch(NoPlayerException e) {
System.out.println("Player를 만들 수 없습니다.");
}
}
/* Player의 상태변경 감지 */
public synchronized void controllerUpdate(ControllerEvent e) {
/* Player가 Realized 상태일 때 컴포넌트를 프레임에 추가한다. */
if(e instanceof RealizeCompleteEvent ) {
contentPanel = new JPanel(new BorderLayout());
/* 기본 조정 컴포넌트 */
controlComponent = player.getControlPanelComponent();
if(controlComponent != null) {
contentPanel.add(controlComponent, BorderLayout.SOUTH);
}
/* Visual 컴포넌트 */
visualComponent = player.getVisualComponent();
if(visualComponent != null) {
contentPanel.add(visualComponent, BorderLayout.CENTER);
}
/* 프레임에 컴포넌트 추가 */
setContentPane(contentPanel);
pack();
}
}
public void actionPerformed(ActionEvent e) {
/* 열기 메뉴를 선택했을 경우 */
if(e.getActionCommand().equals("open")) {
FileDialog dialog = new FileDialog(this, "파일열기", FileDialog.LOAD);
dialog.show();
if(dialog.getFile() != null) {
String filePath = "file:/" + dialog.getDirectory() + dialog.getFile();
/* Player를 만들고, 재생시키다. */
makePlayer(filePath);
player.start();
}
}
}
public static void main(String[] args) {
MediaPlayer player = new MediaPlayer("Media Player");
player.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
/* Look & Feel 변경 */
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(player);
}catch(Exception e) {
System.out.println(e);
}
}
}// end class MediaPlayer
'Document' 카테고리의 다른 글
JUnit (0) | 2008.08.04 |
---|---|
SWT 실행파일 배포 (0) | 2008.07.25 |
Web Editor (0) | 2008.07.25 |
AWT[2/2] (0) | 2008.07.25 |
AWT[1/2] (0) | 2008.07.25 |