Graphics Classes: Bjarne Stroustrup
Graphics Classes: Bjarne Stroustrup
Graphics classes
Bjarne Stroustrup
www.stroustrup.com/Programming
Abstract
Chapter 12 demonstrated how to create simple windows and
display basic shapes: rectangle, circle, triangle, and ellipse. It
showed how to manipulate such shapes: change colors and line
style, add text, etc.
Chapter 13 shows how these shapes and operations are
implemented, and shows a few more examples. In Chapter 12,
we were basically tool users; here we become tool builders.
Stroustrup/Programming 2
Overview
Graphing
Model
Code organization
Interface classes
Point
Line
Lines
Grid
Open Polylines
Closed Polylines
Color
Text
Unnamed objects
Stroustrup/Programming 3
Display model
Open_polyline draw()
attach()
Display
draw()
Engine
attach() window
Rectangle draw()
Stroustrup/Programming 4
Code organization
FLTK headers
Point.h:
struct Point { };
FLTK code
Graph.cpp:
Window.cpp:
Graph code
Window code
chapter12.cpp:
#include "Graph.h" GUI.cpp:
#include "Window.h"
int main() { } GUI code
Stroustrup/Programming 5
Source files
Header
File that contains interface information (declarations)
#include in user and implementer
Stroustrup/Programming 6
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 whats common to shapes
(imperfectly explained for now; all details in Chapter 14)
Color as you see it on the screen
Stroustrup/Programming 7
Point
namespace 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) { }
}; // Note the ';'
}
Stroustrup/Programming 8
Line
struct Shape {
// hold lines represented as pairs of points
// knows how to display lines
};
Stroustrup/Programming 9
Line example
// draw two lines:
using namespace Graph_lib;
win.wait_for_button(); // Display!
Stroustrup/Programming 10
Line example
Stroustrup/Programming 11
Line example
Individual lines are independent
horizontal.set_color(Color::red);
vertical.set_color(Color::green);
Stroustrup/Programming 12
Lines
struct Lines : Shape { // a Lines object is a set of lines
// We use Lines when we want to manipulate
// all the lines as one shape, e.g. move them all
// together with one move statement
void add(Point p1, Point p2); // add line from p1 to p2
void draw_lines() const; // to be called by Window to draw Lines
};
Terminology:
Lines is derived from Shape
Lines inherits from Shape
Lines is a kind of Shape
Shape is the base of Lines
This is the key to what is called object-oriented programming
Well get back to this in Chapter 14
Stroustrup/Programming 13
Lines Example
Lines x;
x.add(Point(100,100), Point(200,100)); // horizontal line
x.add(Point(150,50), Point(150,150)); // vertical line
Stroustrup/Programming 14
Lines example
Stroustrup/Programming 15
Implementation: Lines
void Lines::add(Point p1, Point p2) // use Shapes add()
{
Shape::add(p1);
Shape::add(p2);
}
Note
fl_line is a basic line drawing function from FLTK
FLTK is used in the implementation, not in the interface to our classes
We could replace FLTK with another graphics library
Stroustrup/Programming 16
Draw Grid
(Why bother with Lines when we have Line?)
// A Lines object may hold many related lines
// Here we construct a grid:
int x_size = win.x_max();
int y_size = win.y_max();
int x_grid = 80; // make cells 80 pixels wide
int y_grid = 40; // make cells 40 pixels high
Lines grid;
for (int x=x_grid; x<x_size; x+=x_grid) // veritcal lines
grid.add(Point(x,0),Point(x,y_size));
for (int y = y_grid; y<y_size; y+=y_grid) // horizontal lines
grid.add(Point(0,y),Point(x_size,y));
win.attach(grid); // attach our grid to our window (note grid is one object)
Stroustrup/Programming 17
Grid
Stroustrup/Programming 19
Draw red grid
grid.set_color(Color::red);
Stroustrup/Programming 20
Line_style
struct 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;
};
Stroustrup/Programming 21
Example: colored fat dash grid
grid.set_style(Line_style(Line_style::dash,2));
Stroustrup/Programming 22
Polylines
struct Open_polyline : Shape { // open sequence of lines
void add(Point p) { Shape::add(p); }
};
Open_polyline opl;
opl.add(Point(100,100));
opl.add(Point(150,200));
opl.add(Point(250,250));
opl.add(Point(300,200));
Stroustrup/Programming 24
Closed_polyline
Closed_polyline cpl;
cpl.add(Point(100,100));
cpl.add(Point(150,200));
cpl.add(Point(250,250));
cpl.add(Point(300,200));
Stroustrup/Programming 25
Closed_polyline
cpl.add(Point(100,250));
Stroustrup/Programming 26
Text
Stroustrup/Programming 27
Add text
Stroustrup/Programming 28
Implementation: Text
void Text::draw_lines() const
{
fl_draw(lab.c_str(), point(0).x, point(0).y);
}
Stroustrup/Programming 29
Color matrix
Stroustrup/Programming 31
Color matrix (16*16)
Stroustrup/Programming 33