Top Banner
Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete” Petersen Walter Daugherity Fall 2007
33

Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

Mar 06, 2018

Download

Documents

lamtram
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

Chapter 13Graphics classes

Bjarne StroustrupLawrence “Pete” Petersen

Walter DaugherityFall 2007

Page 2: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

2

Abstract

• Last time we introduced FLTK and demonstrated how to create a simple window and display a coordinate system plus basic shapes: square, circle, triangle, and ellipse; plus how to change colors and line style, add text, and how to move objects.

Page 3: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

3

Abstract

• Today we will continue with our discussion of FLTK and look at how we implement and display a point, line, lines, a grid, and polylines.

• We will also clear the window, add a second window, and work some more with colors

Page 4: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

4

Overview• Graphing

– Model – Code organization

• Interface classes– Point– Line– Lines– Grid– Open Polylines– Closed Polylines– Color– Text

Page 5: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

5

Display model

• Objects (such as graphs) are “attached to” (“placed in”) a window.

• The “display engine” invokes display commands (such as “draw line from x to y”) for the objects in a window

• Objects such as, Square adds vectors of lines to the window to draw

Open_polyline

Square

“window”

DisplayEngine

attach()

attach()

draw()

draw()

draw()

Page 6: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

6

// Graphing interface:struct Point { … };…

// window interface:class Window {…};…

FLTK headers

Graph codeWindow code

FLTK code

chapter12.cpp:

Window.h:

Window.cpp:

#include “Graph.h”#include “Window.h”int main() { … }

Graph.cpp:

Graph.h:

struct Point { … };

// GUI interface:struct In_box { … };…

GUI code

GUI.cpp:

Code organization

Page 7: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

7

Source files• Header

– File that contains interface information (declaration)– #include in user and implementer

• .cpp (“code file”/”implementation file”)– File that contains code implementing interfaces defined in

headers and/or uses such interfaces– #includes headers

• Read the Graph.h header– And later the graph.cpp implementation file

• Don’t read the Window.h header or the window.cppimplementation file– Naturally, some of you will take a peek– Beware: heavy use of yet unexplained C++ features

Page 8: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

8

Design note• The ideal of program design is to represent concepts

directly in code– We take this ideal very seriously

• For example:– Window – a window as we see it on the screen

• Will look different on different operating systems (not our business)

– Line – a line as you see it in a window on the screen– Point – a coordinate point– Shape – what’s common to shapes

• (imperfectly explained for now; many more details later)– Color – as you see it on the screen

Page 9: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

9

Pointnamespace Graph_lib // our graphics interface is in Graph_lib{

struct Point // a Point is simply a pair of ints (the coordinates){

int x, y;Point(int xx, int yy) : x(xx), y(yy) { }

}; // Notice the ‘;’ why is that there???}

Page 10: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

10

Implementation of line// Just the constructor:

Line::Line(Point p1, Point p2) // construct a line from p1 to p2{

add(p1); // add p1 to this shapeadd(p2); // add p2 to this shape

}

// add() is provided by Shape // Shape can hold points and “knows how to” display lines

Page 11: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

11

Line

struct Shape { /* hold lines represented as pairs of points */ };

struct Line : Shape // a Line is a Shape defined by just two Points

{Line(Point p1, Point p2);

};

Page 12: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

12

Line example// draw two lines:Using namespace Graph_lib;

Window win(Point(100,100),600,400,"Canvas"); // make awindow

Line horizontal(Point(100,100),Point(200,100)); // make a lineLine vertical(Point(150,50),Point(150,150)); // make a line

win.attach(horizontal); // attach the lines to the windowwin.attach(vertical);

wait_for_button(); // Display!

Page 13: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

13

Line example

Page 14: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

14

Clean up the Window// Before we go on, let’s first clean up our window.

win.detach(horizontal); // detach the horizontal linewin.detach(vertical); // detach the vertical linewait_for_button();

// now our “old” lines don’t appear in the window// (“don’t mess up new examples”)

Page 15: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

15

Clean Window

Page 16: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

16

Linesstruct Lines : Shape { // a Lines is a set of independent lines

void add(Point p1, Point p2); // add line from p1 to p2void draw_lines() const; // to be called by Window to draw Lines

};

// Lines is “derived from” Shape// Lines “inherit from” Shape// Lines is “a kind of” Shape// Shape is “the base” of Lines

// We’ll get back to this.// This is the key to what is called “object-oriented programming”

Page 17: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

17

Lines Example

Lines x;x.add(Point(100,100), Point(200,100)); // horizontal linex.add(Point(150,50), Point(150,150)); // vertical line

win.attach(x); // attach Lines x to Window winwait_for_button(); // Draw!

Page 18: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

18

Lines Example

Page 19: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

19

Implementation: Linesvoid Lines::add(Point p1, Point p2) // use Shape’s add(){

Shape::add(p1);Shape::add(p2);

}

void Lines::draw_lines() const// to somehow be called from Shape{

for (int i=1; i<number_of_points(); i+=2)fl_line(point(i-1).x,point(i-1).y,point(i).x,point(i).y);

}

// Note: fl_line is a basic line drawing function from FLTK// FLTK is used in the implementation, not in the interface to our

classes

Page 20: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

20

Draw Grid(Why bother with Lines when we have Line?)

// A Lines object may hold many related lines:

int x_size = win.x_max();int y_size = win.y_max(); int x_grid = 80;int y_grid = 40;

Lines grid;for (int x=x_grid; x<x_size; x+=x_grid)

grid.add(Point(x,0),Point(x,y_size));// A line is defined by two Points

for (int y = y_grid; y<y_size; y+=y_grid)grid.add(Point(0,y),Point(x_size,y));

win.attach(grid); // attach our grid to our windowwait_for_button(); // display!

Page 21: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

21

Grid

Page 22: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

22

Draw Red grid// Clean up a bit:

win.detach(x); // don't show cross (Lines object)win.set_label("red grid");grid.set_color(Color::red);wait_for_button();

Page 23: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

23

Red Grid

Page 24: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

24

Color// Map FLTK colors and scope them

struct Color {enum Color_type {

red=FL_RED,blue=FL_BLUE,green=FL_GREEN,// …

};

Color(Color_type cc) :c(Fl_Color(cc)) { }Color(int cc) :c(Fl_Color(cc)) { }

int as_int() const { return c; }private:

Fl_Color c;};

Page 25: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

25

Line_stylestruct Line_style {

enum Line_style_type {solid=FL_SOLID, // -------dash=FL_DASH, // - - - -dot=FL_DOT, // ....... dashdot=FL_DASHDOT, // - . - . dashdotdot=FL_DASHDOTDOT, // -..-..

};

Line_style(Line_style_type ss) :s(ss), w(0) { }Line_style(Line_style_type lst, int ww) :s(lst), w(ww) { }Line_style(int ss) :s(ss), w(0) { }

int width() const { return w; }int style() const { return s; }

private:int s;int w;

};

Page 26: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

26

Example: colored fat dash gridwin.set_label("fat dashed red grid");grid.set_style(Line_style(Line_style::dash,2));wait_for_button();

Page 27: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

27

Polylinesstruct Open_polyline : Shape { // open sequence of lines

void add(Point p) { Shape::add(p); }};

struct Closed_polyline : Shape { // closed sequence of linesvoid draw_lines() const{

Shape::draw_lines(); // draw closing line:fl_line(point(number_of_points()-1).x,point(number_of_points()1).y,point(0).x,point(0).y );

}void add(Point p) { Shape::add(p); }

};

Page 28: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

28

Closed_polyline

win.hide(); // get rid of grid window

Simple_window win10(Point(100,100),600,400,"Closed polyline 5");

Closed_polyline cpl;cpl.add(Point(100,100));cpl.add(Point(150,200));cpl.add(Point(250,250));cpl.add(Point(300,200)); cpl.add(Point(100,250));

win10.attach(cpl);wait_for_button();

Page 29: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

29

Closed_polyline

Page 30: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

30

Closed_polyline with label// we can have more than one window:

Simple_window win101(Point(10,500),600,400,"Closed polyline with text");

// add a text label:Text t(Point(200,200),"A closed polyline that isn't a polygon");t.set_color(Color::blue);

win101.attach(cpl);win101.attach(t);

wait_for_button();

Page 31: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

31

Page 32: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

32

Text

struct Text : Shape {// the point is the bottom left of the first letterText(Point x, const string& s) : lab(s) { add(x); }

void draw_lines() const;

void set_label(const string& s) { lab = s; }string label() const { return lab; }

private:string lab; // label

};

Page 33: Chapter 13 Graphics classes - CS Course Webpagescourses.cs.tamu.edu/daugher/cpsc121/07fall/slides/Chapter13_07c.pdf · Chapter 13 Graphics classes Bjarne Stroustrup Lawrence “Pete”

33

Implementation: Text

void Text::draw_lines() const{

fl_draw(lab.c_str(), point(0).x, point(0).y);}

// fl_draw() is a basic text drawing function from FLTK