Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods...

17
Java Programming Working with Sound and Images

Transcript of Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods...

Page 1: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Java Programming

Working with Sound and Images

Page 2: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Topics

Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString() method to draw strings Use Graphics and Graphics 2D objects Display images Play sound files

Page 3: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Drawing methods: Applets and Applications Call repaint() whenever an application needs updating

the screen (i.e. you have something to draw) repaint() calls update() to schedule painting of the

graphics window update() calls the application’s paint() method to paint

the screen Override this method to do you own painting of graphics Typically want to call super.paint() as first statement

Java does not guarantee that the application will get repainted (depends on CPU load and thread priorities)

Page 4: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Drawing Methods: Components

Override paintComponent() to do your own drawing of a JPanel or other component you must create a derived class

Typically you want to first call the parent paintComponent() method to do the standard work, then add you graphics

Public void paintComponent(Graphics gr) {super.paintComponent(gr);….// your work here

}

Page 5: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Drawing methods: Text

Use drawString() to output text to a graphics window

drawString() x/y coordinate points to lower left corner of graphics string

Public void paintComponent(Graphics gr) {Super.paintComponent(gr);

Graphics2D gr2D = (Graphics2D) gr;Gr2.drawString(“My string!”, 100, 20)

}

Page 6: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

IMAGES

Page 7: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Adding Images to Swing Applets

Images formats supported by Java include:Graphics Interchange Format (GIF)

Maximum of 256 colors

Join Photographic Experts Group (JPEG) Stores mostly photographs

Portable Network Graphics (PNG) Stores images in a lossless form

Page 8: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Adding Images to Swing App’s.

Use class Image to hold a reference to an image

Use Graphics object passed to paint() or paintComponent() to display images

//assume myPicture is a reference to an Image

Graphics2D gr2D = (Graphics2D) gr;

If (myPicture != null)

gr2D.drawImage(myPicture, 0, 0 this)

Page 9: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Loading Images into Applets

Use Applet method getImage() to load an image

Image myPicture;

imageText = getParameter(“ImageName”); //html param

myPicture = getImage(getCodebase(), imageText);

Page 10: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Loading Images into Applications

Applications need to use Toolkit class to gain access to image processing methods

Toolkit kit = Toolkit.getDefaultToolkit();

myPicture = kit.getImage(“Images/myPicture.png”);

Page 11: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

SOUND

Page 12: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Adding Sound to Swing Applets

Java 2 supports the following sound formats AU, AIFF, WAV, MIDI

Use Applet’s play() method to retrieve and play sound

No warning is given is sound file is not found Hint use File class to verify file existence…

play (getCodeBase(), “kaboom.au”); // two argplay(URL); // single argument constructor

Page 13: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Adding Sound to Swing App. (cont)

You can also use class AudioClip to load and play sounds.

Is more versatile that applet’s play() methodAudioClip works with Applets and ApplicationsAudioClip supports methods play(), loop(), and

stop()

Page 14: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Adding Sound to Swing Applet

Use Applet method getAudioClip() to load sound file

AudioClip anyYelp;

// applet home URL

anyYelp = getAudioClip(getCodeBase(),“Sounds/doh.wav”);

//html page home URL

anyYelp = getAudioClip(getDocumentBase(),“Sounds/doh.wav”);

anyYelp.play(); //or anyYelp.loop();

Page 15: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Adding Sound to Swing Application

Use class method JApplet.newAudioClip() to load sound file

AudioClip anyYelp;

URL soundFile = new URL("file:Sounds/doh.wav");

anyYelp = JApplet.newAudioClip(soundFile);

anyYelp.play(); //or anyYelp.loop();

Page 16: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Summary

Adding Sound and Image graphics can make applications more lively and fun

Sound and Image processing is typically done in dedicated threads to avoid downgrading application performance

Java has an immensely rich set of classes to support multimedia applications (we have only scratched the surface)

Page 17: Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()

Other topics

URLs and their exceptions When not to call super.paint() ImageIcon (scaling images) When there is no need to use threads