Practical Exercise 12 – Images and Sounds

advertisement
Practical Exercise 12 – Images and Sounds
Introduces




Image objects
getImage( ), getDocumentBase( ), drawImage( ) methods
Sound objects
getAudioClip( ) and getDocumentBase( ) methods
Working with images
import java.applet.*;
import java.awt.*;
public class Prac12Images extends Applet
{
Image
pic1, pic2, pic3;
//image variables declared
public void init()
{
// Images can be imported or created and can be either .gif, .jpg or .png
//Next lines get images stored in the same directory as the HTML.
pic1 = getImage(getDocumentBase(), "HotWired.gif");
pic2 = getImage(getDocumentBase(), "Watermelon.jpg");
pic3 = getImage(getDocumentBase(), "Magic.png");
} // end of init
public void paint(Graphics g)
{
//The later images are drawn on top of the earlier ones.
g.drawImage(pic1, 20, 20, this);
int width = pic1.getWidth(this);
int height = pic1.getHeight(this);
// width of image
// height of image
// Draw picture scaled up two times
g.drawImage(pic1, 100, 200, width*2, height*2, this);
} // end paint
} // end Prac8aImages
You can draw any gif, jpg or png image onto your applet window.
An image is an object which you must:
1. Declare : Image <image1>, <image2>, ….. ;
2. Instantiate : To do this, you need to tell the class the name of the image file and where
it is. For the time being you should store all images in the same directory as your
project files.
The method getDocumentBase() gives the directory of the html file in your project and
getCodeBase() gives the directory of the class file – both of which are the same if you are using a
Java Project.
Claremont College 2015, adapted from Rosny College 2009
Page 1 of 4
The method getImage() places the image in your class.
Painting an image
To paint the image in the window you use one of the drawImage() methods of paint().
The simplest of these methods is illustrated by:
g.drawImage(picture, 20, 20, this);
where the two numbers are the x and y coordinates of the top left of the image, and this again
tells the class to draw the image in this applet.
Another drawImage() method allows you to specify the width and height of the image drawn in
the window :
g.drawImage(pic1, 100, 200, width*2, height*2, this);
Here the 4th and 5th parameters tell the class the width and height of the image on the window.
In the class above we used the getWidth( ) and getHeight( ) methods to find the width and height
of the original image and then multiply these by two so that the final drawn image was twice the
size of the original.
Working with sounds
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
// A program to demonstrate how sounds are played in a Java program.
// Files of type .au, .wav and .mid now play in Java Applets.
// The user can control play of the sound by clicking on the appropriate Button.
public class Prac8bSounds extends Applet implements ActionListener
{
AudioClip
sound;
Button
playSound, loopSound, stopSound;
int
action;
// load the sound when the applet begins executing
public void init()
{
sound = getAudioClip(getDocumentBase(), "cima22.au"); //
playSound = new Button("Play");
add(playSound);
playSound.addActionListener(this);
loopSound = new Button("Loop");
add(loopSound);
loopSound.addActionListener(this);
stopSound = new Button("Stop");
add(stopSound);
stopSound.addActionListener(this);
} // end init
public void paint(Graphics g)
{
g.drawString("Testing Sound files", 20, 100);
switch(action)
{
Claremont College 2015, adapted from Rosny College 2009
Page 2 of 4
case 1: g.drawString("The sound is playing", 20,120);
break;
case 2: g.drawString("The sound is looping", 20,120);
break;
case 3: g.drawString("The sound has stopped", 20,120);
break;
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == playSound)
// action has a value of 1, 2 or 3
// to indicate what is
// happening to the sound
{
sound.play();
action = 1;
}
else if(e.getSource() == loopSound)
{
sound.loop();
action = 2;
}
else if(e.getSource() == stopSound)
{
sound.stop();
action = 3;
} // end switch
repaint();
}
}
Getting a sound
Java applets will play these three types of sound files:
.au
Format for sound files on UNIX operating systems, standard audio file format
for Java programming
.wav
Format for sound files developed by IBM and Microsoft - de facto standard for
sound files on PCs.
Musical Instrument Digital Interface – a standard used by electronic music
.mid
industry for controlling devices such as synthesizers and sound cards that emit
music.
A sound is an object which you must:
 Declare : Sound <sound1>, < sound 2>, ….. ;
 Instantiate : To do this, you need to tell the class the name of the sound file and where it
is. For the time being you should store all sounds in the same directory as your project
files.
The method getDocumentBase() gives the directory of the html file in your project and
getCodeBase() gives the directory of the class file – both of which are the same if you are using a
Java Project.
The method getAudioClip() places the sound in your class.
When running Prac12Sounds your output should be like the output below:
Claremont College 2015, adapted from Rosny College 2009
Page 3 of 4
C Standard
Either download some images you can use or create your own and save them into the bin folder.
In Prac12Images, replace “HotWired.gif”, “Watermelon.png” and “Magic.png” with the names of
the files you have downloaded.
B Standard
Download an appropriate sound file from the internet and save it into the bin folder of your
project.
In Prac12Sounds, replace “cima22.au” with the name of the file you downloaded.
Try using files of type .wav and .mid in your program.
A Standard
Either modify one of your programs from an earlier pracs or come up with something new that
uses both images and sounds.
Have a bit of fun, and remember that you will likely use images and sounds in your major
project.
Claremont College 2015, adapted from Rosny College 2009
Page 4 of 4
Download