Title: CS12320 Lecture 19
1CS12320 - Lecture 19
- Reyer Zwiggelaar
- rrz_at_aber.ac.uk
2In This Lecture
3Introduction to Graphics
- Programs have graphical components
- A picture must be digitised
- A picture is made up of pixels
- A collection of pixels is an image
- An image is normally a rectangular array
4Introduction to Graphics
- Black and white
- One bit per pixel (0 white and 1 black)
- Colour
- Three values between 0 and 255
- Known as RGB values
5Introduction to Graphics
(0,0)
80
x
40
(80,40)
y
6Applets
- A Java application is a stand-alone program with
a main method - An applet is a Java program that is intended to
transported over the web and executed using a web
browser - Applets can also be viewed using an appletviewer
- Applets do not have a main method
- The paint method is automatically executed
whenever the applets contents must be redrawn
7Applets
- The paint methods parameter is of type Graphics
- Defines a graphics context
- Draw shapes and text
- The Graphics class has several methods for
drawing shapes - The class that defines the applet extends the
Applet class (inheritance)
8Applets
- An applet is embedded into an HTML file using a
tag that references the bytecode file of the
applet class - It is actually the bytecode version of the
program that is transported across the web - The applet is executed by a Java interpreter that
is part of the browser
9HelloWorld.java
import java.awt. import java.applet.Applet publ
ic class HelloWorld extends Applet public void
init() repaint() public void
paint(Graphics gc) gc.drawString("Hello
World!", 16, 16)
10HelloWorld.java
import java.awt. import java.applet.Applet publ
ic class HelloWorld extends Applet public void
init() repaint() public void
paint(Graphics gc) gc.drawString("Hello
World!", 16, 16)
The AWT package defines objects for
developing graphical user interfaces
11HelloWorld.java
import java.awt. import java.applet.Applet publ
ic class HelloWorld extends Applet public void
init() repaint() public void
paint(Graphics gc) gc.drawString("Hello
World!", 16, 16)
Applet is the base class for all applets it
provides all the code needed to create a
window and respond to the mouse etc.
12HelloWorld.java
import java.awt. import java.applet.Applet publ
ic class HelloWorld extends Applet public void
init() repaint() public void
paint(Graphics gc) gc.drawString("Hello
World!", 16, 16)
Our new class inherits all of this code from
Applet, we only need to say how our Applet is
different from all others
13HelloWorld.java
import java.awt. import java.applet.Applet publ
ic class HelloWorld extends Applet public void
init() repaint() public void
paint(Graphics gc) gc.drawString("Hello
World!", 16, 16)
The init method is called when the applet starts
(a bit like main) to initialise the applet
14HelloWorld.java
import java.awt. import java.applet.Applet publ
ic class HelloWorld extends Applet public void
init() repaint() public void
paint(Graphics gc) gc.drawString("Hello
World!", 16, 16)
The paint method is called when the applet need
to redraw its contents e.g. when the window
has been obscured by another
15HelloWorld.java
import java.awt. import java.applet.Applet publ
ic class HelloWorld extends Applet public void
init() repaint() public void
paint(Graphics gc) gc.drawString("Hello
World!", 16, 16)
Here we have used the drawString method to
request that the current Graphics context object
display a String
16HelloWorld.java
import java.awt. import java.applet.Applet publ
ic class HelloWorld extends Applet public void
init() repaint() public void
paint(Graphics gc) gc.drawString("Hello
World!", 16, 16)
At these coordinates
17HelloWorld.java
import java.awt. import java.applet.Applet publ
ic class HelloWorld extends Applet public void
init() repaint() public void
paint(Graphics gc) gc.drawString("Hello
World!", 16, 16)
repaint is a method defined by Applet, it causes
the window to be redrawn
18HelloWorld.java
import java.awt. import java.applet.Applet publ
ic class HelloWorld extends Applet public void
init() repaint() public void
paint(Graphics gc) gc.drawString("Hello
World!", 16, 16)
19Drawing Shapes
- More shapes available in Graphics class
- Filled versus unfilled
- Size, coordinates
- Square bounding box
20A Line
(0,0)
80
10
x
10
40
(80,40)
y
gc.drawLine(10, 10, 80, 40) gc.drawLine(80, 40,
10, 10)
21A Rectangle
(0,0)
10
x
10
30
70
y
gc.drawRect(10, 10, 70, 30)
22An Oval
(0,0)
80
x
40
40
60
y
gc.drawOval(80, 40, 40, 60)
23An Arc
(0,0)
80
x
40
40
60
y
gc.drawOval(80, 40, 40, 60,0,90)
24A Filled Oval
(0,0)
80
x
40
40
60
y
gc.fillOval(80, 40, 40, 60)
25More on Colour
- A colour is defined in a Java program using an
object created from the Color class - The Color class also contains predefined colours
- Every graphics context has a current foreground
colour - Every drawing surface has a background colour
26More on Colour
- Using a predefined colour
- gc.setColor(Color.blue)
- Using a custom colour
- Color green new Color(0, 255, 0)
- gc.setColor(green)
- Or alternatively
- gc.setColor(new Color(0, 255, 0))
27More on Fonts
- A font defines the style in which text is
displayed - A font is defined in a Java program using an
object created from the Font class - To change the current font
gc.setFont(newFont("Courier",Font.BOLD,32))
Font name
Font style
Font size
28In This Lecture
29In The Next Lecture
- Abstract classes
- Interfaces