soundandimage.java

来自「Java the UML Way 书中所有源码」· Java 代码 · 共 48 行

JAVA
48
字号
/*
 * SoundAndImage.java   E.L. 2001-08-13
 *
 * The image file duke.running.gif and the audio file spacemusic.au
 * should be in the same directory as this applet.
 * The image file is from version 1.3 of SDK (demo/jfc/Java2D/images/),
 * while the music file is found in version 1.4 (demo/applets/Animator/audio/)
 *
 */
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.applet.*;

public class SoundAndImage extends JApplet {
  public void init() {
    Container content = getContentPane();
    URL codebase = getCodeBase(); // where the applet resides

    AudioClip music = getAudioClip(codebase, "spacemusic.au");
    music.play();

    Image theImage = getImage(codebase, "duke.running.gif");
    Drawing theDrawing = new Drawing(theImage);
    content.add(theDrawing);
  }
}

class Drawing extends JPanel {
  private Image image;

  public Drawing(Image newImage) {
    image = newImage;
  }

  public void paintComponent(Graphics window) {
    super.paintComponent(window);
    /*
     * The arguments to drawImage() are:
     * First the image, then
     * the x- and y-coordinates for the top-left corner,
     * the width and the height of the image (it may be resized),
     * background color,
     * ImageObserver, see the online API-documentation.
     */
    window.drawImage(image, 0, 0, 200, 200, Color.red, this);
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?