0

I have used snippets of code from various sources to compile the following code:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.util.Calendar;

public class ShowImage extends Panel {
    static Calendar cal = Calendar.getInstance(); //gets system time

BufferedImage  image;
private static final String holidaysimages[] = { //declares the image array
  "santa.jpg", "valentine.jpg", "bunny.jpg", "bunny.jpg", "summer.jpg", "summer.jpg", 
  "summer.jpg", "summer.jpg", "turkey.jpg", "turkey.jpg", "turkey.jpg", "santa.jpg"};
private static final String holidayssounds[] = { //declares the sound array
      "santa.wav", "valentine.wav", "easter.wav", "easter.wav", "summer.wav", "summer.wav", 
      "summer.wav", "summer.wav", "turkey.wav", "turkey.wav", "turkey.wav", "santa.wav"};
  public ShowImage() throws Exception {

try {
    String imageName= holidaysimages[cal.get(Calendar.MONTH)]; //uses the month to get the appropriate picture
    File input = new File(imageName);
    image = ImageIO.read(input);
} catch (IOException ie) {
  System.out.println("Error:"+ie.getMessage()); //prints error message if the image does not exist
}
  }

  public void paint(Graphics g) {
g.drawImage( image, 0, 0, null); //displays the image
  }

  static public void main(String args[]) throws Exception {
JFrame frame = new JFrame("Happy Holidays");
Panel panel = new ShowImage();
frame.getContentPane().add(panel);
frame.setSize(500, 500); //sets  frame size
frame.setVisible(true); 

new AePlayWave(holidayssounds[cal.get(Calendar.MONTH)]).start(); //plays the sound
  }
}

import java.io.File; 
import java.io.IOException; 
import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.FloatControl; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.SourceDataLine; 
import javax.sound.sampled.UnsupportedAudioFileException; 

public class AePlayWave extends Thread { 

private String filename;

private Position curPosition;

private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb 

enum Position { 
    LEFT, RIGHT, NORMAL
};

public AePlayWave(String wavfile) { 
    filename = wavfile; //gets the file name from main
    curPosition = Position.NORMAL;
} 

public AePlayWave(String wavfile, Position p) { 
    filename = wavfile; //tells the system that the file is a wav file
    curPosition = p;
} 

public void run() { 

    File soundFile = new File(filename);
    if (!soundFile.exists()) { //if the file is not found prints the error
        System.err.println("Wave file not found: " + filename);
        return;
    } 

    AudioInputStream audioInputStream = null;
    try { 
        audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (UnsupportedAudioFileException e1) { 
        e1.printStackTrace();
        return;
    } catch (IOException e1) { 
        e1.printStackTrace();
        return;
    } 

    AudioFormat format = audioInputStream.getFormat();
    SourceDataLine auline = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

    try { 
        auline = (SourceDataLine) AudioSystem.getLine(info);
        auline.open(format);
    } catch (LineUnavailableException e) { 
        e.printStackTrace();
        return;
    } catch (Exception e) { 
        e.printStackTrace();
        return;
    } 

    if (auline.isControlSupported(FloatControl.Type.PAN)) { 
        FloatControl pan = (FloatControl) auline
                .getControl(FloatControl.Type.PAN);
        if (curPosition == Position.RIGHT) 
            pan.setValue(1.0f);
        else if (curPosition == Position.LEFT) 
            pan.setValue(-1.0f);
    } 

    auline.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

    try { 
        while (nBytesRead != -1) { 
            nBytesRead = audioInputStream.read(abData, 0, abData.length);
            if (nBytesRead >= 0) 
                auline.write(abData, 0, nBytesRead);
        } 
    } catch (IOException e) { 
        e.printStackTrace();
        return;
    } finally { 
        auline.drain();
        auline.close();//closes the file
    } 

} 
} 

I realise it is quite a bit of code to look through but it would really help me a lot to understand this code. I only need help with the AePlayWav class because it is the more complicated class. I have included documentation for what I do understand but do not want to use any code that is above my level of knowledge. Any help in the documentation would help me a lot.

thanks,

Jaro

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by NormR1
0

Have you read the API doc for the classes and methods that the code uses? Did you have questions about what the doc says?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.