A display model
A graphical and graphical user interface directly corresponds to the idea of a screen display. The basic concepts are inherently graphical (and are two-dimensional, adapted to the rectangular area of a computer screen), which include coordinates, lines, rectangles, and circles. From a programmatic point of view, the goal is to establish a direct correspondence between objects in memory and screen images.
The basic model is as follows: We use the basic objects provided by the graphics system (such as lines) to assemble more complex objects, and then add these objects to a Window object that represents a physical screen, and finally, using a program to display the objects we add to the window on the screen, we can see the program as the screen itself. Or a "display engine," or "Our graphics library," or "GUI library," even as "the dwarf who paints behind the screen," and then draws on the screen the objects we want to add to the window.
#include "simple_windows.h" #include "Graph.h" int main () {using namespace Graph_lib; Point TL (100,100); Simple_windows win (TL, 600,400, "Canvas"); Polygon Poly;poly.add (Point (300,200));p Oly.add (Point (350,100));p Oly.add (Point (400,200));p Oly.set_color (color:: red); Win.attach (poly); Win.wait_for_button ();}
Coordinates
A computer screen is a rectangular area of pixels that is a point that can be set to a certain color. The most common way in a program is to model the screen as a rectangular area of pixels, with each pixel determined by X (horizontal) and y (vertical) coordinates. The left-most pixel has an x coordinate of 0 and increments to the right until the right-most pixel: The topmost pixel has a y-coordinate of 0, and gradually increments downward until the bottommost pixel. Notice that the y-coordinate is "growing downward". This may be a bit odd, especially for mathematicians. However, the screen (window) is different in size, and the upper-left corner may be the only thing in common between screens, because it is set as the Origin point.
By convention, the main () function contains code and exception handling that we want to execute (directly or indirectly):
int main () {try {//... here are our code ...} catch (exception& e) {//Some error reporting return 1; } catch (...) {//Some more error reporting return 2; }}
To make this main () compilation work, I need to define exception. If the header file Std lib Facilities.h or the standard library header file <stdexcept> is included as usual, you will get exception definition.
Axis
Axis xa (axis::x, point (20,300), 280,10, "x Axis"); Win.attach (XA); Win.set_label ("Canvas #2"); Win.wait_for_button ();
To do this, you create an axis object, add it to the window, and then display it, as shown in the image on the right. As you can see, axis::x is a horizontal line with a specified number (10) of ticks and a label "x Axis". Typically, labels are used to interpret the meaning of axes and scales.
Draw a function diagram
Now that we have a window with an axis, it seems like a good idea to draw a function. We create a shape to represent the sine function and add it to the window:
function sine (sin, 0, +, point (20,150), 1000,50,50); Win.attach (sine); Win.set_label ("Canvas #4"); win.wait_for_ button ();
In this code, a function object named sine draws a sine curve using the values produced by the standard library function sin ().
The Polygon function diagram is a way to represent data. A Polygon (polygon) is a sequence of points that are connected by lines to form the Polygon class. The first line joins the first point to the second point, the second line joins the second point to the third point ... The last line joins the last point to the first point:
Sine.set_color (Color::blue); Polygon Poly;poly.add (Point (300,200));p Oly.add (Point (350,100));p Oly.add (Point (400,200));p Oly.set_color (color:: Red);p Oly.set_style (line_style::d ash); Win.attach (poly); Win.set_label ("Canvas #5); Win.wait_for_button ();
The screen is a rectangle, the window is a rectangle, and a sheet of paper is a rectangle. In fact, many shapes in the real world are rectangles (or at least rounded rectangles), because rectangles are the easiest shape to handle.
Rectangle R (Point (200,200), 100,50);
Fill
The previous drawing shapes are drawn outlines, and we can also use a color fill shape:
R.set_fill_color (Color::yellow);p Oly.set_style (Line_style (line_style::d ash,4));
Any closed shape can be filled in. The rectangle is very special and it is very easy to fill.
Text
Finally, it is impossible for any drawing system to completely have no simple text output-drawing each character as a collection of lines and ensuring that characters are not clipped.
Text T (Point (150,150), "Hello, Graphical world!");
Using the basic graphical elements in this example, you can generate any complex, subtle display effect. Notice a common feature of all the code in this chapter: there are no loops and select statements, and all the data is "hard-coded". The output is just a simple combination of basic graphic elements. Once we start using data and algorithms to combine these basic graphs, we can get more complex and interesting output results.
We can also load images from the file:
Image II (Point (100,50), "image.jpg");
base classes and derived classes
Let's look at the base class and the derived class from a more technical point of view. When designing a graphical interface library, we rely on the following 3 key language mechanisms:
(i) derivation (derivation): Constructs a method of another class from one class so that the newly constructed class can replace the original class. Derived classes include all members of the base class, in addition to their own members. This is often referred to as inheritance because the derived class "inherits" all the members of its base class. In some contexts, a derived class is called a subclass, and a base class is called a parent class.
(ii) virtual function: Defines a function in a base class that has a function of exactly the same type and name in the derived class, and when the user calls the base class function, the function in the derived class is actually called.
(iii) Private and protected members (private and protected member): We keep the implementation details of the classes private to protect them from direct access, simplifying maintenance operations, which are often referred to as encapsulation.
The use of inheritance, runtime polymorphism, and encapsulation is, in fact, the most common sign of object-oriented programming. Therefore, in addition to other programming networks, C + + directly supports object-oriented programming.
Object layout
How the object is laid out inside. A member of a class defines the layout of an object: data members are stored one after another in memory. When inheritance is used, the data members of the derived class are simply placed after the members of the base class.
Definition of derivation and virtual functions of a class
We specify a class as a derived class by giving a base class after the class name.
struct Circle::shape {/*....*/};
By default, members of a struct (struct) are public. Public members in a base class also become public members of the struct. Another way to define equivalence is as follows:
Class Circle:public Shape {public:/*......*/};
A virtual function must be declared as virtual in the declaration of the class, but if you put the function definition outside the class, the keyword virtual does not have to be there. For example:
struct shape{//... virtual void draw_lines () const; virtual void Movd (); //...}; virtual void Shape::d raw_lines () const {/**.../}//errorvoid Shape::move () {/*...*/}//ok
When you want to overwrite a virtual function, you must use exactly the same name and type as in the base class. For example:
struct Circle:shape {void draw_lines (int) const;void drawlines () const;void draw_lines ();//...}
Access
C + + provides a simple model for class member access. The members of a class can be:
(a) Private: If a member is private, its name can only be used by members of the class to which it belongs.
(ii) protected (Protected): If a member is protected, its name can only be used by members of the class to which it belongs and its derived class.
(c) Public: If a member is public, its name can be used by all functions.
Pure virtual function
An abstract class is a class that can be used only as a base class. We use abstract classes to represent those abstractions, the concepts that correspond to generalizations of the commonalities of related entities. In a program, an abstract class typically defines an interface for a set of related classes (class hierarchies).
"C + + programming principles and practices" reading notes (iii)