Graphics Library Reference

advertisement
CSCI-201 HANDOUT 1 – Spring 2008
Programming with Simple Graphics
Students of previous CSCI-201 courses have been introduced to the notion of C++ classes and objects by
writing some simple graphics programs. Since graphics are known to be fun, intuitive and educational, you
will be given a similar introduction. All you will need to know about the graphic programming for our lab
exercises is given below. The original source for this information is C.Horstmann, Computing Concepts with
C++ Essentials, 3rd Edition, John Wiley, 2003.
Setting up a graphics program. The file into which you write your graphics program should follow the
following template:
#include "ccc_win.h"
// other includes if necessary
int ccc_win_main()
{
// ... your code ...
return 0;
}
The Graphics Window. The graphics window is an object similar to cout and cin. It is called cwin and
you can use the << operator to output graphics objects to the window. By default, the coordinate system is
[−10 . . . 10] on the x-axis and [−10 . . . 10] on the y-axis, so the origin (0, 0) is in the center of the screen. You
can change the coordinates by using the coord function. Following the convention in computer graphics, the
coordinate system is specified with the coordinates of the top left corner and the coordinates of the bottom
right corner. Therefore to change the coordinates to a 10 × 10 window with the origin on the bottom left
corner, we would use the following
cwin.coord(0,10,10,0); // change the coordinate system
You can also clear the window of all objects using the clear function. For example :
cwin.clear(); // clears the window
The Point class. You can declare a variable to be of type Point, by using Horstmann’s Point class. When
you declare (“create”) a Point object, you need to specify an x- and a y-coordinate. For example,
Point pt1(1.5, 7.5); // a point at coordinates (1.5,7.5);
A Point object can tell you about its x- and y-coordinates with the get x() and get y() member functions.
A Point can move with the move( , ) member function. For example,
pt1.move(0.5, 2.5);
// move 0.5 units in x, and 0.5 units in y;
double newx = pt1.get_x();
double newy = pt1.get_y();
// x-coordinate
// y-coordinate
cout << "New location: (" << newx << "," << newy << ")" << endl;
1
The Line class. You can declare a variable to be of type Line, by using Horstmann’s Line class. When
you declare (“create”) a Line object, you need to specify the line’s start and end points. For Example,
Line ln(Point(1,1), Point(5,5)); // a line from (1,1) to (5,5);
A Line object can tell you about its two end points with the get start() and get end() member functions.
A Line can also move in x- and y-directions with the move( , ) member function. For example,
Point start = ln.get_start();
Point end = ln.get_end();
// start is at (1,1)
// end is at (5,5)
ln.move(-1,-1);
// ln is moved to start in 0,0;
The Circle class. You can declare a variable to be of type Circle, by using Horstmann’s Circle class.
When you declare (“create”) a Circle object, you need to specify the line’s start and end points. For
Example,
Circle circ(Point(1,1), 3.5); // a circle with center (1,1) and radius 3.5
A Circle object can tell you about its center point and radius with the get center() and get radius()
member functions. A Circle moves with move( , ). For example.
Point ct = circ.get_center(); // the center
double rad = circ.get_radius();// the radius
circ.move(-1,-1);
// circle now with center (0,0);
The Message class. You can declare a variable to be of type Message, by using Horstmann’s Message
class. When you declare (“create”) a Message object, you need to indicate the Point where the message
will be located and the message itself which can be a string or a number. For Example,
Message message1(Point(1,1), 11); // outputs "11" at location (1,1)
Message message2(Point(1,1), "Message Here"); // "Message Here" at location (1,1)
A Message object can tell you about its point and message with the get start() and get text() member
functions. A Message moves with move( , ). For example.
Point mpoint = message1.get_start(); // the point where the message starts
string mstring = message2.get_text();// the text of the message
message1.move(-1,-1);
// moves the message to (0,0)
Drawing graphics objects. Horstmann’s graphics will open up a separate graphics window. After you
create your graphics objects (your Points, Lines, and Circles, you make them visible in the graphics window
by drawing them with cwin <<. For example,
Circle circ1(Point(1,1), 3.5);
Circle circ2(Point(-1,-1), 5.0);
cwin << circ1 << circ2;
// draw circles
cwin << Line(circ1.get_center(),circ2.get_center());// draw line
2
This code fragment creates two circles, draws them, and then also draws a Line between the center points
of both.
Input from the graphics window. Horstmann’s graphics allows you to input from the graphics window.
You can input a string, integer, double and a mouse click which returns an object from the Point class.
These functions allow you to prompt the user by passing a string argument as a function paramter. For
example,
string name = cwin.get_string("Please enter your name:") //input a string
int integernumber = cwin.get_int("Please enter an integer:") //input an integer
double realnumber = cwin.get_double("Please enter a number:") //input a (real) number
Point click = cwin.get_mouse("Please click on the window") //input a mouse click
A complete graphics program. Insert the most recent lines of code into the body of a ccc win main().
// File: mycircs.cpp
#include "ccc_win.h"
// other includes if necessary
int ccc_win_main()
{
Circle circ1(Point(1,1), 3.5);
Circle circ2(Point(-1,-1), 5.0);
cwin << circ1 << circ2;
cwin << Line(circ1.get_center(),circ2.get_center());
return 0;
}
Here is another program :
// File: mycircs.cpp
#include "ccc_win.h"
// other includes if necessary
int ccc_win_main()
{
cwin.coord(0,10,10,0);
Message message(Point(5,8),"Lets draw a line");
cwin << message1;
Point p1=cwin.get_mouse("Click on one end of the line");
Point p2=cwin.get_mouse("Click on the other end of the line");
cwin << Line(p1,p2);
return 0;
}
Compile a graphics program. Compile and run your Horstmann graphics program with Q mycircs.cpp
(or, /share/bin/Q mycircs.cpp).
3
Download