13

Paint the window and play a sound in a loop

Download the background image and the audio file.

Copy bluewave.jpg in a folder called images and loop.au in a folder called sounds.

  1. Java/Canvas
    1. Canvas.java
    2. images
      1. bluewave.jpg
    3. sounds
      1. loop.au

Edit Canvas.java.

  1. // <applet code="Canvas" width="500" height="300">
  2. // <param name="background" value="images/bluewave.jpg"/>
  3. // <param name="sound" value="sounds/loop.au"/>
  4. // </applet>
  5.  
  6. import java.applet.*;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9.  
  10. public class Canvas extends Applet implements MouseListener {
  11.  
  12.     private Image image;
  13.  
  14.     private AudioClip sound;
  15.    
  16.     private Boolean playing = false;
  17.  
  18.     private int imageWidth, imageHeight;
  19.  
  20.     public void init() {
  21.         String p;
  22.  
  23.         p = getParameter("background");
  24.  
  25.         if (p != null) {
  26.             MediaTracker tracker = new MediaTracker(this);
  27.             image = getImage(getCodeBase(), p);
  28.             tracker.addImage(image, 0);
  29.             try {
  30.                 tracker.waitForID(0);
  31.             }
  32.             catch (InterruptedException e) {
  33.             }
  34.  
  35.             imageWidth = image.getWidth(this);
  36.             imageHeight = image.getHeight(this);
  37.         }
  38.  
  39.         p = getParameter("sound");
  40.  
  41.         if (p != null) {
  42.             sound = getAudioClip(getCodeBase(), p);
  43.             playing = true;
  44.         }
  45.        
  46.         addMouseListener(this);
  47.     }
  48.  
  49.     public void mouseClicked(MouseEvent e) {
  50.         if (playing)
  51.             stop();
  52.         else
  53.             start();
  54.         playing = !playing;
  55.     }
  56.  
  57.     public void mouseEntered(MouseEvent e) {}
  58.  
  59.     public void mouseExited(MouseEvent e) {}
  60.  
  61.     public void mousePressed(MouseEvent e) {}
  62.  
  63.     public void mouseReleased(MouseEvent e) {}
  64.  
  65.     public void paint(Graphics g) {
  66.  
  67.         int width = getSize().width;
  68.         int height = getSize().height;
  69.  
  70.         int y = 0;
  71.  
  72.         while (y < height) {
  73.             int x = 0;
  74.  
  75.             while (x < width) {
  76.                 g.drawImage(image, x, y, this);
  77.                 x += imageWidth;
  78.             }
  79.             y += imageHeight;
  80.         }
  81.     }
  82.  
  83.     public void start() {
  84.         if (sound != null)
  85.             sound.loop();
  86.     }
  87.  
  88.     public void stop() {
  89.         if (sound != null)
  90.             sound.stop();
  91.     }
  92. }
$ javac Canvas.java
$ appletviewer Canvas.java

The image is replicated to cover the entire background of the window. Try resizing it. You should also be hearing a sound loop. Click in the window to pause the player. Click again to restart it.

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].