lecture 3 a first graphic program. features of a simple graphic program

19
Lecture 3 A First Graphic Program

Upload: spencer-thrift

Post on 01-Apr-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 3 A First Graphic Program. Features of a simple graphic program

Lecture 3A First Graphic Program

Page 2: Lecture 3 A First Graphic Program. Features of a simple graphic program

Features of a simple graphic program

Page 3: Lecture 3 A First Graphic Program. Features of a simple graphic program

Features of a simple graphic program• Gapp and Gcanvas are ours – Are New

Developed.• Inherited from JFrame and panel which are part of

the Java Foundation Classes - in particular they are part of the swing” GUI components.

• when a Gapp object is created, a new GUI window appears on the screen.

Page 4: Lecture 3 A First Graphic Program. Features of a simple graphic program

Features of a simple graphic program• A Gcanvas/JPanel is simply something we can

draw on, but it isn’t allowed to be on screen by itself, it must be part of a JFrame. So what we actually need is an instance of Gapp with an instance of Gcanvas “inside” it (Figure 23).

Page 5: Lecture 3 A First Graphic Program. Features of a simple graphic program

simple graphic program – Main()• Main() The main entry point for the system is in

class Gapp.

Page 6: Lecture 3 A First Graphic Program. Features of a simple graphic program

simple graphic program- initComponents() • sets the window’s screen position and size • (lines 22,23), sets up some Java apparatus to allow the

program to shut down cleanly.• (lines 27-31) and creates an instance of a Gcanvas (graphics

canvas) .• line 25. This GCanvas object is “added” to the Gapp object

and hence becomes part of that window.

Page 7: Lecture 3 A First Graphic Program. Features of a simple graphic program

simple graphic program- Paintcomponent()• it is passed a Graphics object (“g”) - line 12. • drawLine() which is a member of the Graphics class. • The Graphics class is part of the standard Java class library

and its member functions are termed “graphics primitives”.• There exist a whole range of methods for drawing and

coloring in a variety of shapes (boxes, polygons, ovals etc.) • Take a look at the Java API documentation for full details.

Page 8: Lecture 3 A First Graphic Program. Features of a simple graphic program

simple graphic program- Paintcomponent()

The drawLine() method takes 4 parameters, x1, y1, x2, y2 which represent the start and end points of the line you wish to draw.

Page 9: Lecture 3 A First Graphic Program. Features of a simple graphic program

First Program

Page 10: Lecture 3 A First Graphic Program. Features of a simple graphic program

First Program

Page 11: Lecture 3 A First Graphic Program. Features of a simple graphic program

First Program

Page 12: Lecture 3 A First Graphic Program. Features of a simple graphic program

First Program

Page 13: Lecture 3 A First Graphic Program. Features of a simple graphic program

First Program - Running

Page 14: Lecture 3 A First Graphic Program. Features of a simple graphic program

Graphics Primitives• A graphics primitive is a drawing function which the

system makes available to the applications programmer. Example is the Java “drawLine()” method.

Page 15: Lecture 3 A First Graphic Program. Features of a simple graphic program

Graphics Primitives

Page 16: Lecture 3 A First Graphic Program. Features of a simple graphic program

Graphics Primitives

Page 17: Lecture 3 A First Graphic Program. Features of a simple graphic program

Graphics Primitives

Page 18: Lecture 3 A First Graphic Program. Features of a simple graphic program

Graphics Primitives

Page 19: Lecture 3 A First Graphic Program. Features of a simple graphic program