Methods &
Graphics
GEEN163 Introduction to
Computer Programming
“First we thought the PC was a
calculator. Then we found out how to
turn numbers into letters with ASCII and we thought it was a typewriter.
Then we discovered graphics, and we
thought it was a television. With the
World Wide Web, we've realized it's a
brochure.”
Douglas Adams
Reference Variables
double
Widget
pigeon = 47.5;
hawk;
pigeon
hawk
47
null
Reference Variables
double pigeon = 47.5;
Widget hawk;
hawk = new Widget();
pigeon
47
hawk
hawk
Widget
Widget data
Primitive Variables
int cobra, mamba;
cobra
?
mamba
?
Primitive Variables
int cobra, mamba;
cobra = 17;
cobra
17
mamba
?
Primitive Variables
int cobra, mamba;
cobra = 17;
mamba = cobra;
cobra
17
mamba
17
Primitive Variables
int cobra, mamba;
cobra = 17;
mamba = cobra;
cobra = 5;
cobra
5
mamba
17
Object Variables
Widget frog, toad;
frog
null
toad
null
Object Variables
Widget frog, toad;
frog = new Widget();
frog
toad
null
Widget
data
Object Variables
Widget frog, toad;
frog = new Widget();
toad = frog;
frog
toad
Widget
data
Object Variables
Widget frog, toad;
frog = new Widget();
toad = frog;
frog.setColor(Red);
frog
toad
Widget
data
Using Methods
• You have used several Java methods
Math.sqrt( dog )
Keyboard.nextDouble()
• Static methods take action on a class
• Dynamic methods take action on an object
• When you have Java perform a method, it
is referred to as executing or calling the
method
Parameters
• When calling a method in Java, you write:
Classname.methodname( parameters )
• Dynamic methods use an object not class
• The method name is always followed by
(parentheses)
• Some methods have parameters or data
to be used by the method
• Math.sqrt( rabbit ) takes the
square root of rabbit
Multiple Parameters
• Some methods have no parameters and
some have several
• Multiple parameters are separated by
commas
double kitty = 2.0, lion;
lion = Math.pow( kitty, 3.0 );
• The pow method returns the first
parameter raised to the second parameter
• lion is kitty3.0
Return Values
• Some methods produce a value that is
returned to the calling program
snake = Math.sin( rat ) + bird;
ferret = keyboard.nextDouble();
• Some methods do not return a value.
These methods are usually called on a
single line and are not part of an equation
page.setColor( java.awt.BLUE );
What is the value of toad?
25%
25%
...
ab
e
th
of
no
ne
ye
llo
w
W
W
id
re
d
25%
id
ge
t
ge
t
l
1. null
2. red Widget
3. yellow Widget
4. none of the above
25%
nu
l
Widget frog, toad;
frog = new Widget();
toad = frog;
frog.setColor(Red);
frog = null;
Applets and Applications
• The Java programs you have written so far
were Java applications
• A Java applet is a program that runs in a
web browser. Applets are usually
embedded in a web page.
• A Java program is an applet when it
extends the java.awt.Applet class
• The javax.swing.JApplet class
extends Applet and you can extend it
Applet Example
public class MyWebProg extends
javax.swing.JApplet {
public void init() {
// do something
}
}
Inheritance
• A new class can inherit all the properties
and abilities of another class
• A child class inherits from a parent class
• Any class can be a parent class
• The child class can add extra features or
change some features of the parent class
• A class can inherit from a class that
inherits from another class to any depth
Ancestors
• Imagine you have a class called Mammal
• A Dog class might inherit from Mammal
• The Mammal class might inherit from the
class Vertebrate
• All classes inherit from the class Object
Object
Vertebrate
Mammal
Dog
javax.swing.JApplet inherits from
1. java.awt.Applet
2. Object
3. All of the above
4. None of the above
of
No
ne
of
t
he
th
ea
b.
..
ab
o.
..
Ob
je
ct
Al
l
ja
va
.a
w
t.A
pp
l
e.
..
25% 25% 25% 25%
No main Method
• Applets do not need a main method
• When an applet runs in a browser, the
browser creates an object of the applet
and then calls the init method
• There are many methods for an applet
including start, stop, destroy and
paint
Simplified Browser
You can imagine that a web browser creates
an object of your applet program and then
calls the init and start methods.
MyWebProg myApplet = new MyWebProg();
myApplet.init();
myApplet.start();
// etc.
paint Method
• The web browser calls the paint method
when it wants to display an applet’s graphics
public void paint( Graphics peacock ) {
peacock.setColor(java.awt.Color.BLUE);
peacock.fillRect( 10, 20, 100, 150 );
}
• The paint method may be called many times,
particularly when you resize the window
Graphics Methods
• There are many methods that can be used
to draw simple shapes on the screen
• Most Graphics methods do not return a
value
• You can set the color to be used to draw a
shape. This color will be used for all
shapes until you change the color
• The Graphics methods are not an effective tool
for drawing cool interactive graphics
Colors
• The java.awt.Color class defines colors
• You can define a color using RGB values
or you can use a predefined constant
• Some Color constants are:
– java.awt.Color.BLUE
– java.awt.Color.GREEN
– java.awt.Color.ORANGE
– java.awt.Color.GRAY
– java.awt.Color.YELLOW
Graphics Coordinates
• The screen has a coordinate system with
the origin in the upper left corner
• Coordinates are given in pixels (Picture
Elements)
• An X coordinate specifies the distance
from the left edge
• A Y coordinate specified the distance from
the top edge
• The screen size depends on the device
setColor
• The setColor method of java.awt.Graphics
objects sets the color that will be used when
drawing shapes
gobj.setColor(java.awt.Color.BLUE);
• where gobj is an object of the type
java.awt.Graphics
drawLine
• The drawLine method of java.awt.Graphics
objects draws a line from x1,y1 to x2,y2
gobj.drawLine(5, 3, 17, 23);
• The line is drawn using the current color
5
3
23
17
drawRect
• The drawRect method of java.awt.Graphics
objects draws a rectangle of width and height
whose upper left corner is x,y
gobj.drawRect( x, y, width, height);
gobj.drawRect( 5, 3, 10, 15 );
5
3
15
10
fillRect
• The fillRect method of java.awt.Graphics
objects draws a rectangle of width and
height whose upper left corner is x,y and
fills it with the current color
gobj.fillRect( 5, 3, 10, 15 );
5
3
15
10
drawOval
• The drawOval method of java.awt.Graphics
objects draws a circle or oval to fit in a box
of width and height whose upper left is x,y
gobj.drawOval( x, y, width, height );
gobj.drawOval( 5, 3, 10, 15 );
5
3
15
10
fillOval
• fillOval is just like drawOval, but it colors in
the circle with the current color
gobj.fillOval( 5, 3, 10, 15 );
5
3
15
10
drawArc
• The drawArc method of java.awt.Graphics
objects draws part of an oval from start
angle for arc angle degrees
• Angles are in degrees not radians
• Zero degrees in the 3 o'clock position
gobj.drawArc( x, y, width, height,
startAngle, arcAngle );
drawArc
• The drawArc method of java.awt.Graphics
objects draws part of an oval from
gobj.drawArc( 5, 3, 10, 15, 45, 135);
fillArc
• The fillArc method is just like the drawArc
method except it fills the arc with the
current color
gobj.fillArc( 5, 3, 10, 15, 45, 135);
drawString
• The drawString method of java.awt.Graphics
objects writes the text of a String starting at
the specified x,y location
page.drawString( String, x, y);
page.drawString(“Whatever”, 5, 3 );
5
3
Whatever
drawString is a
25%
25%
25%
25%
de
te
...
bo
th
ca
n
no
t
be
m
ic
dy
na
m
st
at
ic
m
et
ho
d
et
ho
d
1. static method
2. dynamic method
3. both
4. cannot be
determined
Drawing Pictures
• You can draw simple pictures using the
methods of the Graphics
• If shapes overlap, the shape from the later
method call will be on top
Running Applets in a Web Browser
• A web page can include HTML to embed an
applet
• The applet or object HTML tag can be used
<applet code="MyWebProg.class"
width=250 height=300 />
Homework
• Write a Java applet that draws a picture
using the Graphics methods. You can
draw any picture you like as long as it is
not:
– a snowman (in the textbook)
– a smiley face
– overly simple
• Your program must be unique
• Due by the beginning of lab on Thursday,
September 1