📄 applet.java
字号:
* @return the retrieved audio clip, or null on failure * @see #getAudioClip(URL) */ public AudioClip getAudioClip(URL url, String name) { try { return getAudioClip(new URL(url, name)); } catch (MalformedURLException e) { return null; } } /** * Returns a descriptive string with applet defined information. The * implementation in this class returns <code>null</code>, so subclasses * must override to return information. * * @return a string describing the author, version, and applet copyright */ public String getAppletInfo() { return null; } /** * Returns the locale for this applet, if it has been set. If no applet * specific locale has been set, the default locale is returned. * * @return the locale for this applet * @see Component#setLocale(Locale) * @since 1.1 */ public Locale getLocale() { return super.getLocale(); } /** * Returns a list of parameters this applet supports. Each element of * the outer array is an array of three strings with the name of the * parameter, the data type or valid values, and a description. This * method is optional and the default implementation returns null. * * @return the list of parameters supported by this applet */ public String[][] getParameterInfo() { return null; } /** * Loads and plays the audio clip pointed to by the specified URL. This does * nothing if the URL does not point to a valid audio clip. * * @param url the URL of the audio clip * @throws NullPointerException if url is null * @see #getAudioClip(URL) */ public void play(URL url) { AudioClip ac = getAudioClip(url); try { ac.play(); } catch (Exception ignored) { } } /** * Loads and plays the audio clip pointed to by the specified absolute URL, * and relative path from that URL. This does nothing if the URL cannot be * constructed, or if it does not point to a valid audio clip. * * @param url the base URL of the audio clip * @param name the name of the audio clip relative to the URL * @see #getAudioClip(URL, String) * @see #play(URL) */ public void play(URL url, String name) { try { getAudioClip(url, name).play(); } catch (Exception ignored) { } } /** * This method is called when the applet is first loaded, before start(). * The default implementation does nothing; override to do any one-time * initialization. * * @see #start() * @see #stop() * @see #destroy() */ public void init() { } /** * This method is called when the applet should start running. This is * normally each time a web page containing it is loaded. The default * implemention does nothing; override for your applet to be useful. * * @see #init() * @see #stop() * @see #destroy() */ public void start() { } /** * This method is called when the applet should stop running. This is * normally when the next web page is loaded. The default implementation * does nothing; override for your applet to stop using resources when * it is no longer visible, but may be restarted soon. * * @see #init() * @see #start() * @see #destroy() */ public void stop() { } /** * This method is called when the applet is being unloaded. The default * implementation does nothing; override for your applet to clean up * resources on exit. * * @see #init() * @see #start() * @see #stop() */ public void destroy() { } /** * Gets the AccessibleContext associated with this applet, creating one if * necessary. This always returns an instance of {@link AccessibleApplet}. * * @return the accessibility context of this applet * @since 1.3 */ public AccessibleContext getAccessibleContext() { if (accessibleContext == null) accessibleContext = new AccessibleApplet(); return accessibleContext; } /** * Read an applet from an object stream. This checks for a headless * environment, then does the normal read. * * @param s the stream to read from * @throws ClassNotFoundException if a class is not found * @throws IOException if deserialization fails * @throws HeadlessException if this is a headless environment * @see GraphicsEnvironment#isHeadless() * @since 1.4 */ private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { if (GraphicsEnvironment.isHeadless()) throw new HeadlessException(); s.defaultReadObject(); } /** * This class provides accessibility support for Applets, and is the * runtime type returned by {@link #getAccessibleContext()}. * * @author Eric Blake (ebb9@email.byu.edu) * @since 1.3 */ protected class AccessibleApplet extends AccessibleAWTPanel { /** * Compatible with JDK 1.4+. */ private static final long serialVersionUID = 8127374778187708896L; /** * The default constructor. */ protected AccessibleApplet() { } /** * Get the role of this accessible object, a frame. * * @return the role of the object * @see AccessibleRole#FRAME */ public AccessibleRole getAccessibleRole() { return AccessibleRole.FRAME; } /** * Get the state set of this accessible object. In addition to the default * states of a Component, the applet can also be active. * * @return the role of the object * @see AccessibleState */ public AccessibleStateSet getAccessibleStateSet() { AccessibleStateSet s = super.getAccessibleStateSet(); if (isActive()) s.add(AccessibleState.ACTIVE); return s; } } // class AccessibleApplet private static class URLAudioClip implements AudioClip { // The URL we will try to play. // This is null if we have already tried to create an // audio input stream from this URL. private URL url; // The real audio clip. This is null before the URL is read, // and might be null afterward if we were unable to read the URL // for some reason. private Clip clip; public URLAudioClip(URL url) { this.url = url; } private synchronized Clip getClip() { if (url == null) return clip; try { clip = AudioSystem.getClip(); clip.open(AudioSystem.getAudioInputStream(url)); } catch (LineUnavailableException _) { // Ignore. } catch (IOException _) { // Ignore. } catch (UnsupportedAudioFileException _) { // Ignore. } url = null; return clip; } public void loop() { Clip myclip = getClip(); if (myclip != null) myclip.loop(Clip.LOOP_CONTINUOUSLY); } public void play() { Clip myclip = getClip(); if (myclip != null) myclip.start(); } public void stop() { Clip myclip = getClip(); if (myclip != null) { myclip.stop(); myclip.setFramePosition(0); } } }} // class Applet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -