SimpleCxxLib
packageclass GWindow
GWindow
consists of two layers. The background layer
provides a surface for drawing static pictures that involve no animation.
Graphical objects drawn in the background layer are persistent and do
not require the client to update the contents of the window. The
foreground layer contains graphical objects that are redrawn as necessary.
The GWindow
class includes several methods that draw
lines, rectangles, and ovals on the background layer without making
use of the facilities of the gobjects.h
interface. For
example, the following program draws a diamond, rectangle, and oval
at the center of the window.
int main() { GWindow gw; cout << "This program draws a diamond, rectangle, and oval." << endl; double width = gw.getWidth(); double height = gw.getHeight(); gw.drawLine(0, height / 2, width / 2, 0); gw.drawLine(width / 2, 0, width, height / 2); gw.drawLine(width, height / 2, width / 2, height); gw.drawLine(width / 2, height, 0, height / 2); gw.setColor("BLUE"); gw.fillRect(width / 4, height / 4, width / 2, height / 2); gw.setColor("GRAY"); gw.fillOval(width / 4, height / 4, width / 2, height / 2); return 0; }A
GWindow
object may be freely copied, after which all
copies refer to the same window.
Constructor | |
GWindow(width, height) | Creates a window, either of the specified size or a default size. |
Methods | |
add(gobj, x, y) | Adds the GObject to the foreground layer of the window. |
Adds the interactor (which can also be a GLabel ) to the control strip specified by the region parameter. | |
Clears the contents of the window. | |
Deletes the window from the screen. | |
draw(gobj, x, y) | Draws the GObject on the background layer. |
drawLine(x0, y0, x1, y1) | Draws a line connecting the specified points. |
drawOval(x, y, width, height) | Draws the frame of a oval with the specified bounds. |
drawPolarLine(x0, y0, r, theta) | Draws a line of length r in the direction theta from the initial point. |
drawRect(x, y, width, height) | Draws the frame of a rectangle with the specified bounds. |
fillOval(x, y, width, height) | Fills the frame of a oval with the specified bounds. |
fillRect(x, y, width, height) | Fills the frame of a rectangle with the specified bounds. |
Returns the current color as a string in the form "#rrggbb" . | |
Returns a pointer to the topmost GObject containing the point (x , y ), or NULL if no such object exists. | |
Returns the height of the graphics window in pixels. | |
Returns the width of the graphics window in pixels. | |
Returns the title of the graphics window. | |
Tests whether the window is visible. | |
Removes the object from the window. | |
Adds the interactor (which can also be a GLabel ) to the control strip specified by the region parameter. | |
Schedule a repaint on this window. | |
Asks the system to assign the keyboard focus to the window, which brings it to the top and ensures that key events are delivered to the window. | |
Sets the color used for drawing. | |
Sets the alignment of the specified side region as specified by the string align . | |
Determines whether the window is visible on the screen. | |
Sets the title of the graphics window. | |
Operators | |
Checks whether the two objects refer to different windows. | |
Checks whether the two objects refer to the same window. | |
Functions | |
Converts a color name into an integer that encodes the red, green, and blue components of the color. | |
Converts an rgb value into a color name in the form "#rrggbb" . | |
Closes all graphics windows and exits from the application without waiting for any additional user interaction. | |
Returns the height of the entire display screen. | |
Returns the width of the entire display screen. | |
Pauses for the indicated number of milliseconds. | |
Issues a request to update all graphics windows. | |
Waits for a mouse click to occur anywhere in any window. |
GWindow(); GWindow(double width, double height);
Usage:
GWindow gw; GWindow gw(width, height);
void close();
Usage:
gw.close();
void requestFocus();
Usage:
gw.requestFocus();
void clear();
Usage:
gw.clear();
void setVisible(bool flag);
Usage:
gw.setVisible(flag);
bool isVisible();
Usage:
if (gw.isVisible()) ...
void drawLine(const GPoint & p0, const GPoint & p1); void drawLine(double x0, double y0, double x1, double y1);
Usage:
gw.drawLine(p0, p1); gw.drawLine(x0, y0, x1, y1);
GPoint drawPolarLine(const GPoint & p0, double r, double theta); GPoint drawPolarLine(double x0, double y0, double r, double theta);
r
in the direction theta
from the initial point. The angle theta
is measured in
degrees counterclockwise from the +x axis. The method returns
the end point of the line.
Usage:
GPoint p1 = gw.drawPolarLine(p0, r, theta); GPoint p1 = gw.drawPolarLine(x0, y0, r, theta);
void drawOval(const GRectangle & bounds); void drawOval(double x, double y, double width, double height);
Usage:
gw.drawOval(bounds); gw.drawOval(x, y, width, height);
void fillOval(const GRectangle & bounds); void fillOval(double x, double y, double width, double height);
Usage:
gw.fillOval(bounds); gw.fillOval(x, y, width, height);
void drawRect(const GRectangle & bounds); void drawRect(double x, double y, double width, double height);
Usage:
gw.drawRect(bounds); gw.drawRect(x, y, width, height);
void fillRect(const GRectangle & bounds); void fillRect(double x, double y, double width, double height);
Usage:
gw.fillRect(bounds); gw.fillRect(x, y, width, height);
void setColor(string color); void setColor(int color);
color
parameter is
usually one of the predefined color names:
BLACK
,
BLUE
,
CYAN
,
DARK_GRAY
,
GRAY
,
GREEN
,
LIGHT_GRAY
,
MAGENTA
,
ORANGE
,
PINK
,
RED
,
WHITE
, and
YELLOW
.
The case of the individual letters in the color name is ignored, as
are spaces and underscores, so that the color DARK_GRAY
can be written as "Dark Gray"
.
The color can also be specified as a string in the form
"#rrggbb"
where rr
, gg
, and
bb
are pairs of hexadecimal digits indicating the
red, green, and blue components of the color.
Usage:
gw.setColor(color);
string getColor();
"#rrggbb"
.
In this string, the values rr
, gg
,
and bb
are two-digit hexadecimal values representing
the red, green, and blue components of the color, respectively.
Usage:
string color = gw.getColor();
double getWidth();
Usage:
double width = gw.getWidth();
double getHeight();
Usage:
double height = gw.getHeight();
void repaint();
Usage:
gw.repaint();
void setWindowTitle(string title);
Usage:
gw.setWindowTitle(title);
string getWindowTitle();
Usage:
string title = gw.getWindowTitle();
void draw(const GObject & gobj); void draw(GObject *gobj); void draw(const GObject *gobj); void draw(GObject & gobj, double x, double y); void draw(GObject *gobj, double x, double y);
GObject
on the background layer. For convenience,
the gobj
parameter may be passed either as a constant
reference or as a pointer. If the x
and y
parameters are included, the object is moved to that location before
drawing.
Usage:
gw.draw(gobj); gw.draw(gobj, x, y);
void add(GObject *gobj); void add(GObject *gobj, double x, double y);
GObject
to the foreground layer of the window.
The second form of the call sets the location of the object to
(x
, y
) first.
In terms of memory management, adding a GObject
pointer to
a GWindow
transfers control of that object from the client to
the window manager. Deleting a GWindow
automatically deletes
any GObject
s
Usage:
gw.add(gobj); gw.add(gobj, x, y);
void remove(GObject *gobj);
Usage:
gw.remove(gobj);
void addToRegion(GInteractor *gobj, string region); void addToRegion(GLabel *gobj, string region);
GLabel
) to
the control strip specified by the region
parameter.
The region
parameter must be one of the strings
"NORTH"
, "EAST"
, "SOUTH"
,
or "WEST"
.
Usage:
gw.addToRegion(interactor, region);
void removeFromRegion(GInteractor *gobj, string region); void removeFromRegion(GLabel *gobj, string region);
GLabel
) to
the control strip specified by the region
parameter.
The region
parameter must be one of the strings
"NORTH"
, "EAST"
, "SOUTH"
,
or "WEST"
.
Usage:
gw.removeFromRegion(interactor, region);
GObject *getGObjectAt(double x, double y);
GObject
containing the
point (x
, y
), or NULL
if no such
object exists.
Usage:
GObject *gobj = getGObjectAt(x, y);
void setRegionAlignment(string region, string align);
align
. The region
parameter must be
one of the strings "NORTH"
, "EAST"
,
"SOUTH"
, or "WEST"
and the align
parameter must be "LEFT"
, "RIGHT"
, or
"CENTER"
. By default, side panels use
CENTER
alignment.
Usage:
gw.setRegionAlignment(region, align);
bool operator==(GWindow w2);
Usage:
if (w1 == w2) ...
bool operator!=(GWindow w2);
Usage:
if (w1 != w2) ...
void repaint();
Usage:
repaint();
void pause(double milliseconds);
Usage:
pause(milliseconds);
double getScreenWidth();
Usage:
width = getScreenWidth();
double getScreenHeight();
Usage:
height = getScreenHeight();
int convertColorToRGB(string colorName);
Usage:
int rgb = convertColorToRGB(colorName);
string convertRGBToColor(int rgb);
rgb
value into a color name in the
form "#rrggbb"
. Each of the rr
,
gg
, and bb
values are two-digit
hexadecimal numbers indicating the intensity of that component.
Usage:
int colorName = convertRGBToColor(rgb);
void waitForClick();
Usage:
waitForClick();
void exitGraphics();
Usage:
exitGraphics();