CS12320 Lecture 19 - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

CS12320 Lecture 19

Description:

gc.drawString('Hello World!', 16, 16); Applet is the base class for ... Here we have used the drawString. method to request that the current ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 30
Provided by: author4
Category:

less

Transcript and Presenter's Notes

Title: CS12320 Lecture 19


1
CS12320 - Lecture 19
  • Reyer Zwiggelaar
  • rrz_at_aber.ac.uk

2
In This Lecture
  • Applets

3
Introduction 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

4
Introduction 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

5
Introduction to Graphics
  • Coordinate system

(0,0)
80
x
40
(80,40)
y
6
Applets
  • 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

7
Applets
  • 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)

8
Applets
  • 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

9
HelloWorld.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)
10
HelloWorld.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
11
HelloWorld.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.
12
HelloWorld.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
13
HelloWorld.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
14
HelloWorld.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
15
HelloWorld.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
16
HelloWorld.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
17
HelloWorld.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
18
HelloWorld.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)
19
Drawing Shapes
  • More shapes available in Graphics class
  • Filled versus unfilled
  • Size, coordinates
  • Square bounding box

20
A Line
(0,0)
80
10
x
10
40
(80,40)
y
gc.drawLine(10, 10, 80, 40) gc.drawLine(80, 40,
10, 10)
21
A Rectangle
(0,0)
10
x
10
30
70
y
gc.drawRect(10, 10, 70, 30)
22
An Oval
(0,0)
80
x
40
40
60
y
gc.drawOval(80, 40, 40, 60)
23
An Arc
(0,0)
80
x
40
40
60
y
gc.drawOval(80, 40, 40, 60,0,90)
24
A Filled Oval
(0,0)
80
x
40
40
60
y
gc.fillOval(80, 40, 40, 60)
25
More 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

26
More 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))

27
More 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
28
In This Lecture
  • Applets

29
In The Next Lecture
  • Abstract classes
  • Interfaces
Write a Comment
User Comments (0)
About PowerShow.com