我从sun下载了 simplePlayApplet 的源代码,由于它格式很乱,所以我对它进行了重新编排。但是在完成编排之后,在编译器中出现了一些语法错误,不知道应该怎样改正,请高手指点!以下是经过处理后的源代码:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import java.util.Properties;
import javax.media.*;
//import com.sun.media.util.JMFSecurity;
/** * This is a Java Applet that demonstrates how to create a simple
* media player with a media event listener. It will play the
* media clip right away and continuously loop. * * */
public class SimplePlayerApplet extends Applet implements ControllerListener {
// media Player
Player player = null;
// component in which video is playing
Component visualComponent = null;
// controls gain, position, start, stop
Component controlComponent = null;
// displays progress during download
Component progressBar = null;
boolean firstTime = true;
long CachingSize = 0L;
Panel panel = null;
int controlPanelHeight = 0;
int videoWidth = 0;
int videoHeight = 0;
/** * Read the applet file parameter and create the media
* player. */
public void init() {
//$
System.out.println("Applet.init() is called");
setLayout(null);
setBackground(Color.white);
panel = new Panel();
panel.setLayout( null );
add(panel);
panel.setBounds(0, 0, 320, 240);
// input file name from html param
String mediaFile = null;
// URL for our media file
MediaLocator mrl = null;
URL url = null;
// Get the media filename info.
// The applet tag should contain the path to the
// source media file, relative to the html page.
if ((mediaFile = getParameter("FILE")) == null)
Fatal("Invalid media file parameter");
try {
url = new URL(getDocumentBase(), mediaFile);
mediaFile = url.toExternalForm();
}
catch (MalformedURLException mue) { }
try {
// Create a media locator from the file name
if ((mrl = new MediaLocator(mediaFile)) == null)
Fatal("Can't build URL for " + mediaFile);
/* try { JMFSecurity.enablePrivilege.invoke(JMFSecurity.privilegeManager, JMFSecurity.writePropArgs);
JMFSecurity.enablePrivilege.invoke(JMFSecurity.privilegeManager, JMFSecurity.readPropArgs);
JMFSecurity.enablePrivilege.invoke(JMFSecurity.privilegeManager, JMFSecurity.connectArgs);
}
catch (Exception e) {} */
// Create an instance of a player for this media
try { player = Manager.createPlayer(mrl); }
catch (NoPlayerException e) {
System.out.println(e);
Fatal("Could not create player for " + mrl);
}
// Add ourselves as a listener for a player's events
player.addControllerListener(this);
}
catch (MalformedURLException e) {
Fatal("Invalid media file URL!");
}
catch (IOException e) {
Fatal("IO exception creating player for " + mrl);
}
// This applet assumes that its start() calls
// player.start(). This causes the player to become
// realized. Once realized, the applet will get
// the visual and control panel components and add
// them to the Applet. These components are not added
// during init() because they are long operations that
// would make us appear unresposive to the user. }
/** * Start media file playback. This function is called the
* first time that the Applet runs and every
* time the user re-enters the page. */
public void start() {
//$
System.out.println("Applet.start() is called");
// Call start() to prefetch and start the player.
if (player != null) player.start();
}
/** * Stop media file playback and release resource before
* leaving the page. */
public void stop() {
//$
System.out.println("Applet.stop() is called");
if (player != null) {
player.stop();
player.deallocate();
}
}
public void destroy() {
//$
System.out.println("Applet.destroy() is called");
player.close();
}
/** * This controllerUpdate function must be defined in order to
* implement a ControllerListener interface. This
* function will be called whenever there is a media event */
public synchronized void controllerUpdate(ControllerEvent event) {
// If we're getting messages from a dead player,
// just leave
if (player == null) return;
// When the player is Realized, get the visual
// and control components and add them to the Applet
if (event instanceof RealizeCompleteEvent) {
if (progressBar != null) {
panel.remove(progressBar);
progressBar = null;
}
int width = 320;
int height = 0;
if (controlComponent == null)
if (( controlComponent = player.getControlPanelComponent()) != null) {
controlPanelHeight = controlComponent.getPreferredSize().height;
panel.add(controlComponent);
height += controlPanelHeight;
}
if (visualComponent == null)
if (( visualComponent = player.getVisualComponent())!= null){
panel.add(visualComponent);
Dimension videoSize = visualComponent.getPreferredSize();
videoWidth = videoSize.width;
videoHeight = videoSize.height;
width = videoWidth;
height += videoHeight;
visualComponent.setBounds(0, 0, videoWidth, videoHeight);
}
panel.setBounds(0, 0, width, height);
if (controlComponent != null) {
controlComponent.setBounds(0, videoHeight, width, controlPanelHeight);
controlComponent.invalidate();
}
} else if (event instanceof CachingControlEvent) {
if (player.getState() > Controller.Realizing) return;
// Put a progress bar up when downloading starts,
// take it down when downloading ends.
CachingControlEvent e = (CachingControlEvent) event;
CachingControl cc = e.getCachingControl();
// Add the bar if not already there ...
if (progressBar == null) {
if ((progressBar = cc.getControlComponent()) != null) {
panel.add(progressBar);
panel.setSize(progressBar.getPreferredSize());
validate();
}
}
} else if (event instanceof EndOfMediaEvent) {
// We've reached the end of the media; rewind and
// start over
player.setMediaTime(new Time(0));
player.start();
} else if (event instanceof ControllerErrorEvent) {
// Tell TypicalPlayerApplet.start() to call it a day
player = null;
Fatal(((ControllerErrorEvent)event).getMessage());
} else if (event instanceof ControllerClosedEvent) {
panel.removeAll();
}
}
void Fatal (String s) {
// Applications will make various choices about what
// to do here. We print a message
System.err.println("FATAL ERROR: " + s);
throw new Error(s);
// Invoke the uncaught exception
// handler System.exit() is another // choice.
}
}
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import java.util.Properties;
import javax.media.*;
//import com.sun.media.util.JMFSecurity;
/** * This is a Java Applet that demonstrates how to create a simple
* media player with a media event listener. It will play the
* media clip right away and continuously loop. * * */
public class SimplePlayerApplet extends Applet implements ControllerListener {
// media Player
Player player = null;
// component in which video is playing
Component visualComponent = null;
// controls gain, position, start, stop
Component controlComponent = null;
// displays progress during download
Component progressBar = null;
boolean firstTime = true;
long CachingSize = 0L;
Panel panel = null;
int controlPanelHeight = 0;
int videoWidth = 0;
int videoHeight = 0;
/** * Read the applet file parameter and create the media
* player. */
public void init() {
//$
System.out.println("Applet.init() is called");
setLayout(null);
setBackground(Color.white);
panel = new Panel();
panel.setLayout( null );
add(panel);
panel.setBounds(0, 0, 320, 240);
// input file name from html param
String mediaFile = null;
// URL for our media file
MediaLocator mrl = null;
URL url = null;
// Get the media filename info.
// The applet tag should contain the path to the
// source media file, relative to the html page.
if ((mediaFile = getParameter("FILE")) == null)
Fatal("Invalid media file parameter");
try {
url = new URL(getDocumentBase(), mediaFile);
mediaFile = url.toExternalForm();
}
catch (MalformedURLException mue) { }
try {
// Create a media locator from the file name
if ((mrl = new MediaLocator(mediaFile)) == null)
Fatal("Can't build URL for " + mediaFile);
/* try { JMFSecurity.enablePrivilege.invoke(JMFSecurity.privilegeManager, JMFSecurity.writePropArgs);
JMFSecurity.enablePrivilege.invoke(JMFSecurity.privilegeManager, JMFSecurity.readPropArgs);
JMFSecurity.enablePrivilege.invoke(JMFSecurity.privilegeManager, JMFSecurity.connectArgs);
}
catch (Exception e) {} */
// Create an instance of a player for this media
try { player = Manager.createPlayer(mrl); }
catch (NoPlayerException e) {
System.out.println(e);
Fatal("Could not create player for " + mrl);
}
// Add ourselves as a listener for a player's events
player.addControllerListener(this);
}
catch (MalformedURLException e) {
Fatal("Invalid media file URL!");
}
catch (IOException e) {
Fatal("IO exception creating player for " + mrl);
}
// This applet assumes that its start() calls
// player.start(). This causes the player to become
// realized. Once realized, the applet will get
// the visual and control panel components and add
// them to the Applet. These components are not added
// during init() because they are long operations that
// would make us appear unresposive to the user. }
/** * Start media file playback. This function is called the
* first time that the Applet runs and every
* time the user re-enters the page. */
public void start() {
//$
System.out.println("Applet.start() is called");
// Call start() to prefetch and start the player.
if (player != null) player.start();
}
/** * Stop media file playback and release resource before
* leaving the page. */
public void stop() {
//$
System.out.println("Applet.stop() is called");
if (player != null) {
player.stop();
player.deallocate();
}
}
public void destroy() {
//$
System.out.println("Applet.destroy() is called");
player.close();
}
/** * This controllerUpdate function must be defined in order to
* implement a ControllerListener interface. This
* function will be called whenever there is a media event */
public synchronized void controllerUpdate(ControllerEvent event) {
// If we're getting messages from a dead player,
// just leave
if (player == null) return;
// When the player is Realized, get the visual
// and control components and add them to the Applet
if (event instanceof RealizeCompleteEvent) {
if (progressBar != null) {
panel.remove(progressBar);
progressBar = null;
}
int width = 320;
int height = 0;
if (controlComponent == null)
if (( controlComponent = player.getControlPanelComponent()) != null) {
controlPanelHeight = controlComponent.getPreferredSize().height;
panel.add(controlComponent);
height += controlPanelHeight;
}
if (visualComponent == null)
if (( visualComponent = player.getVisualComponent())!= null){
panel.add(visualComponent);
Dimension videoSize = visualComponent.getPreferredSize();
videoWidth = videoSize.width;
videoHeight = videoSize.height;
width = videoWidth;
height += videoHeight;
visualComponent.setBounds(0, 0, videoWidth, videoHeight);
}
panel.setBounds(0, 0, width, height);
if (controlComponent != null) {
controlComponent.setBounds(0, videoHeight, width, controlPanelHeight);
controlComponent.invalidate();
}
} else if (event instanceof CachingControlEvent) {
if (player.getState() > Controller.Realizing) return;
// Put a progress bar up when downloading starts,
// take it down when downloading ends.
CachingControlEvent e = (CachingControlEvent) event;
CachingControl cc = e.getCachingControl();
// Add the bar if not already there ...
if (progressBar == null) {
if ((progressBar = cc.getControlComponent()) != null) {
panel.add(progressBar);
panel.setSize(progressBar.getPreferredSize());
validate();
}
}
} else if (event instanceof EndOfMediaEvent) {
// We've reached the end of the media; rewind and
// start over
player.setMediaTime(new Time(0));
player.start();
} else if (event instanceof ControllerErrorEvent) {
// Tell TypicalPlayerApplet.start() to call it a day
player = null;
Fatal(((ControllerErrorEvent)event).getMessage());
} else if (event instanceof ControllerClosedEvent) {
panel.removeAll();
}
}
void Fatal (String s) {
// Applications will make various choices about what
// to do here. We print a message
System.err.println("FATAL ERROR: " + s);
throw new Error(s);
// Invoke the uncaught exception
// handler System.exit() is another // choice.
}
}
解决方案 »
- 看《java编程思想》过程中 一个疑问
- 环境变量设置问题;
- modle为 false的 JDialog 显示不出来包含的label
- 如何用JAVAAPPLET编写一个界面美观的音频播放器?
- 那位兄弟能告诉我,怎么在java程序中调用winzip的文件压缩功能
- =====linux上的j2se安装问题,帮忙呀……
- 请教一个关于ArrayList的问题!
- 怎样用Java得到系统的所有的逻辑盘符?
- 用URL和URLConnection对象如何向Web服务器上的文件中写入信息?
- 我很想学JAVA,但我是菜鸟,请问各位大虾,能推荐些好书吗?
- Java能做出很cool的图形界面吗?
- 如何用jdbc访问access数据库,请高手给指点.在线等待!
public void start() {
//$
System.out.println("Applet.start() is called");
// Call start() to prefetch and start the player.
if (player != null) player.start();
}
1.在这段程序的第一行最前面提示语法错误
2.start后面提示缺少分号
3.()的中间语法错误
4.下面的stop(),destroy()方法也有类似的错误