Download presentation
Presentation is loading. Please wait.
Published byEugenia Briggs Modified over 9 years ago
1
Java Programming Working with Sound and Images
2
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
3
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)
4
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 }
5
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) }
6
IMAGES
7
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
8
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)
9
Loading Images into Applets Use Applet method getImage() to load an image Image myPicture; imageText = getParameter(“ImageName”); //html param myPicture = getImage(getCodebase(), imageText); …
10
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”);
11
SOUND
12
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 arg play(URL); // single argument constructor
13
Adding Sound to Swing App. (cont) You can also use class AudioClip to load and play sounds. Is more versatile that applet’s play() method AudioClip works with Applets and Applications AudioClip supports methods play(), loop(), and stop()
14
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();
15
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();
16
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)
17
Other topics URLs and their exceptions When not to call super.paint() ImageIcon (scaling images) When there is no need to use threads
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.