INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM
ICS201, SECTIONS 52 (002
Semester)
INTRODUCTION TO COMPUTER SCIENCE
LAB #02 Practice with Drawing and Manipulating Graphics Shapes
Instructor: Bashir M. Ghandi
To gain experience with:
In
ICS102 we learnt how to write applets that display graphical shapes, images and
play sound.
We
can equally do same and even more with applications. For example, the following is an application that displays a
rectangle.
import java.awt.*; public class FrameTest { public
static void main(String[] args) { MyFrame
frame = new MyFrame(); frame.setTitle("Graphics
Using Frames"); frame.setSize(400,
400); frame.show(); } } class MyFrame extends Frame { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Rectangle rect = new Rectangle
(50,50,100,80); g2.draw(rect); } } |
With
applets, most of the management tasks are handled by the super class Applet. However, with applications, the programmer
does most of the management himself.
For example, the above applications cannot be terminated by simply
clicking the close window button ,“x”.
To
close the window, we need to know about the Window handling interface called WindowListener
and how to implement it and add it to the frame.
This
is an interface in the java.awt.event package that specify the methods
for controlling a particular window. It
has the following method specifications:
Method |
Purpose |
windowActivated(WindowEvent e) |
Invoked
when a window is activated. |
windowClosed(WindowEvent e) |
Invoked
when a window has been closed. |
windowClosing(WindowEvent e) |
Invoked
when a window is in the process of being closed. |
windowDeactivated(WindowEvent e) |
Invoked
when a window is de-activated. |
windowDeiconified(WindowEvent e) |
Invoked
when a window is de-iconified. |
windowIconified(WindowEvent e) |
Invoked
when a window is iconified. |
windowOpened(WindowEvent e) |
Invoked
when a window has been opened. |
Most
of the times we do not need all the above methods in our applications, thus we
are forced to implement the ones that we do not need as do nothing
methods. Alternatively, we could
extends the WindowAdapter class which is an implementation of WindowListener
interface where all the methods are implemented as do nothing methods. In this way, we only need to override the
methods that we need. The following
shows how the windows listener may be implemented so that the application is
terminated when the window is closed.
import
java.awt.event.*; class
WindowClosingListener extends WindowAdapter { public void
windowClosing(WindowEvent event) { System.exit(0); } } |
Once
a window event listener is implemented, it can be installed to a frame using
its addWindowListener method.
The following shows a modified version of Example1 in which the window
can be closed. Notice that the WindowClosingListener
is placed inside the MyFrame class.
This is usually how it is done since most of the times, the listener need
to have access to the fields of the class.
This is not a problem since the class only used by the outer class
anyway.
Example
1:
import java.awt.*; import java.awt.event.*; public class DrawingApplication { public
static void main(String[] args) { MyFrame
frame = new MyFrame(); frame.setTitle("Drawing
Using Application"); frame.setSize(400,
400); frame.show(); } } class MyFrame extends Frame { public
MyFrame() { WindowClosingListener
listener = new WindowClosingListener(); addWindowListener(listener); }
public void paint(Graphics g) { Graphics2D
g2 = (Graphics2D)g; Rectangle
rect = new Rectangle (50,50,100,80); g2.draw(rect); } //This
is an inner class class
WindowClosingListener extends WindowAdapter { public
void windowClosing(WindowEvent event) { System.exit(0); } } } |
To
display an image, we need to first load the image into memory. However, for applications, we need to make
use of the Toolkit class, which
is in the java.awt package as follows:
Image image =
Toolkit.getDefaultToolkit().getImage(URL of the image);
Example
2:
import java.awt.*; import java.awt.event.*; import java.awt.*; import java.awt.event.*; public class ImageApplication { public
static void main(String[] args) { MyFrame
frame = new MyFrame(); frame.setTitle("Image
Display Using Application"); frame.setSize(400,
400); frame.show(); } } class MyFrame extends Frame { private
Image image; public
MyFrame() { WindowClosingListener
listener = new WindowClosingListener(); addWindowListener(listener); image
= Toolkit.getDefaultToolkit().getImage("images/kfupmtwr.jpg"); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.drawImage(image,50,50,this); } class
WindowClosingListener extends WindowAdapter { public
void windowClosing(WindowEvent event) { System.exit(0); } } } |
You can also use the AffineTransform class of the java.awt.geom. package to transform the image (rotate, translate, etc).
Plying
music is slightly more difficult than displaying images.
First
we need to use the url of the image file to create a URL object
using the URL class of the java.net package.
While
creating the RRL object, we must handle the MalformedURLException
which the constructor of the URL class may generates.
Next
we use the static method newAudioClip of the Applet class to
create an Audio clip object.
Finally,
we use the play, loop and stop method of the AudioClip object
to play the music.
Now
since it is not wise to reload the audio clip each time the paint method is
called, it is a good idea to implement the windowActivated method of the
windowListener and load the audio clip inside that method.
The
following shows how this may be done.
Example
3:
import
java.awt.*; import
java.awt.event.*; import
java.applet.Applet; import
java.applet.AudioClip; import
java.net.URL; import
java.net.MalformedURLException; public
class AudioApplication { public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.setTitle("Testing Music
Application"); frame.setSize(400, 400); frame.show(); } }
class
MyFrame extends Frame { AudioClip
clip; public MyFrame() { WindowClosingListener listener =
new WindowClosingListener(); addWindowListener(listener); }
public void paint(Graphics g) { clip.loop(); } class WindowClosingListener extends
WindowAdapter { public void
windowClosing(WindowEvent event) { System.exit(0); } public
void windowActivated(WindowEvent event) {
String relativeURL = "audio/spacemusic.au"; StringBuffer
sb=new StringBuffer(System.getProperty("user.dir")); for (int i=0; i<sb.length();
i++)
if (sb.charAt(i) == '\\') sb.setCharAt(i, '/'); String baseURL =
"file:///"+ sb + "/"; try { URL fullURL = new URL(baseURL + relativeURL); clip = Applet.newAudioClip(fullURL);
}
catch(MalformedURLException e) {
System.err.println(e.getMessage());
}
} } } |
Write
an application to displays the message “Welcome to KFUPM” in a bold Sans Serif
font of size 32. The message should be
drawn in yellow color inside a blue rectangle.
Your
application should also display the KFUPM tower image below and about the
center of the message.
Finally,
your application should be playing the spacemusic file in the background.