1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
/*
* File: gwindow.h
* ---------------
* This file defines the <code>GWindow</code> class which supports
* drawing graphical objects on the screen.
*/
#ifndef _gwindow_h
#define _gwindow_h
#include <string>
#include "gtypes.h"
#include "vector.h"
class GCompound;
class GInteractor;
class GLabel;
class GObject;
/*
* Friend type: GWindowData
* ------------------------
* This block contains all data pertaining to the window. Shallow copying
* of the <code>GWindow</code> object ensures that all copies refer to the
* same onscreen window.
*/
struct GWindowData {
double windowWidth;
double windowHeight;
std::string windowTitle;
std::string color;
bool visible;
GCompound *top;
};
/*
* Class: GWindow
* --------------
* This class represents a graphics window that supports simple graphics.
* Each <code>GWindow</code> 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.
*
* <p>The <code>GWindow</code> class includes several methods that draw
* lines, rectangles, and ovals on the background layer without making
* use of the facilities of the <code>gobjects.h</code> interface. For
* example, the following program draws a diamond, rectangle, and oval
* at the center of the window.
*
*<pre>
* 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;
* }
*</pre>
*
* A <code>GWindow</code> object may be freely copied, after which all
* copies refer to the same window.
*/
class GWindow {
public:
/*
* Constructor: GWindow
* Usage: GWindow gw;
* GWindow gw(width, height);
* ---------------------------------
* Creates a window, either of the specified size or a default size.
*/
GWindow();
GWindow(double width, double height);
/*
* Destructor: ~GWindow
* --------------------
* Reclaims the internal storage for the window. Note that the window
* is not closed by this operation, but persists until it is closed by
* the client or the user exits the program.
*/
virtual ~GWindow();
/*
* Method: close
* Usage: gw.close();
* ------------------
* Deletes the window from the screen.
*/
void close();
/*
* Method: requestFocus
* Usage: gw.requestFocus();
* -------------------------
* 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. Clicking in the window automatically requests the focus.
*/
void requestFocus();
/*
* Method: clear
* Usage: gw.clear();
* ------------------
* Clears the contents of the window.
*/
void clear();
/*
* Method: setVisible
* Usage: gw.setVisible(flag);
* ---------------------------
* Determines whether the window is visible on the screen.
*/
void setVisible(bool flag);
/*
* Method: isVisible
* Usage: if (gw.isVisible()) ...
* ------------------------------
* Tests whether the window is visible.
*/
bool isVisible();
/*
* Method: drawLine
* Usage: gw.drawLine(p0, p1);
* gw.drawLine(x0, y0, x1, y1);
* -----------------------------------
* Draws a line connecting the specified points.
*/
void drawLine(const GPoint & p0, const GPoint & p1);
void drawLine(double x0, double y0, double x1, double y1);
/*
* Method: drawPolarLine
* Usage: GPoint p1 = gw.drawPolarLine(p0, r, theta);
* GPoint p1 = gw.drawPolarLine(x0, y0, r, theta);
* ------------------------------------------------------
* Draws a line of length <code>r</code> in the direction <code>theta</code>
* from the initial point. The angle <code>theta</code> is measured in
* degrees counterclockwise from the +<i>x</i> axis. The method returns
* the end point of the line.
*/
GPoint drawPolarLine(const GPoint & p0, double r, double theta);
GPoint drawPolarLine(double x0, double y0, double r, double theta);
/*
* Method: drawOval
* Usage: gw.drawOval(bounds);
* gw.drawOval(x, y, width, height);
* ----------------------------------------
* Draws the frame of a oval with the specified bounds.
*/
void drawOval(const GRectangle & bounds);
void drawOval(double x, double y, double width, double height);
/*
* Method: fillOval
* Usage: gw.fillOval(bounds);
* gw.fillOval(x, y, width, height);
* ----------------------------------------
* Fills the frame of a oval with the specified bounds.
*/
void fillOval(const GRectangle & bounds);
void fillOval(double x, double y, double width, double height);
/*
* Method: drawRect
* Usage: gw.drawRect(bounds);
* gw.drawRect(x, y, width, height);
* ----------------------------------------
* Draws the frame of a rectangle with the specified bounds.
*/
void drawRect(const GRectangle & bounds);
void drawRect(double x, double y, double width, double height);
/*
* Method: fillRect
* Usage: gw.fillRect(bounds);
* gw.fillRect(x, y, width, height);
* ----------------------------------------
* Fills the frame of a rectangle with the specified bounds.
*/
void fillRect(const GRectangle & bounds);
void fillRect(double x, double y, double width, double height);
/*
* Method: setColor
* Usage: gw.setColor(color);
* --------------------------
* Sets the color used for drawing. The <code>color</code> parameter is
* usually one of the predefined color names:
*
* <code>BLACK</code>,
* <code>BLUE</code>,
* <code>CYAN</code>,
* <code>DARK_GRAY</code>,
* <code>GRAY</code>,
* <code>GREEN</code>,
* <code>LIGHT_GRAY</code>,
* <code>MAGENTA</code>,
* <code>ORANGE</code>,
* <code>PINK</code>,
* <code>RED</code>,
* <code>WHITE</code>, and
* <code>YELLOW</code>.
*
* The case of the individual letters in the color name is ignored, as
* are spaces and underscores, so that the color <code>DARK_GRAY</code>
* can be written as <code>"Dark Gray"</code>.
*
* <p>The color can also be specified as a string in the form
* <code>"#rrggbb"</code> where <code>rr</code>, <code>gg</code>, and
* <code>bb</code> are pairs of hexadecimal digits indicating the
* red, green, and blue components of the color.
*/
void setColor(std::string color);
void setColor(int color);
/*
* Method: getColor
* Usage: string color = gw.getColor();
* ------------------------------------
* Returns the current color as a string in the form <code>"#rrggbb"</code>.
* In this string, the values <code>rr</code>, <code>gg</code>,
* and <code>bb</code> are two-digit hexadecimal values representing
* the red, green, and blue components of the color, respectively.
*/
std::string getColor();
/*
* Method: getWidth
* Usage: double width = gw.getWidth();
* ------------------------------------
* Returns the width of the graphics window in pixels.
*/
double getWidth();
/*
* Method: getHeight
* Usage: double height = gw.getHeight();
* --------------------------------------
* Returns the height of the graphics window in pixels.
*/
double getHeight();
/*
* Method: repaint
* Usage: gw.repaint();
* --------------------
* Schedule a repaint on this window.
*/
void repaint();
/*
* Method: setWindowTitle
* Usage: gw.setWindowTitle(title);
* --------------------------------
* Sets the title of the graphics window.
*/
void setWindowTitle(std::string title);
/*
* Method: getWindowTitle
* Usage: string title = gw.getWindowTitle();
* ------------------------------------------
* Returns the title of the graphics window.
*/
std::string getWindowTitle();
/*
* Method: draw
* Usage: gw.draw(gobj);
* gw.draw(gobj, x, y);
* ---------------------------
* Draws the <code>GObject</code> on the background layer. For convenience,
* the <code>gobj</code> parameter may be passed either as a constant
* reference or as a pointer. If the <code>x</code> and <code>y</code>
* parameters are included, the object is moved to that location before
* drawing.
*/
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);
/*
* Method: add
* Usage: gw.add(gobj);
* gw.add(gobj, x, y);
* --------------------------
* Adds the <code>GObject</code> to the foreground layer of the window.
* The second form of the call sets the location of the object to
* (<code>x</code>, <code>y</code>) first.
*
* <p>In terms of memory management, adding a <code>GObject</code> pointer to
* a <code>GWindow</code> transfers control of that object from the client to
* the window manager. Deleting a <code>GWindow</code> automatically deletes
* any <nobr><code>GObject</code><font size=-1>s</font></nobr> it contains.
*/
void add(GObject *gobj);
void add(GObject *gobj, double x, double y);
/*
* Method: remove
* Usage: gw.remove(gobj);
* -----------------------
* Removes the object from the window.
*/
void remove(GObject *gobj);
/*
* Method: addToRegion
* Usage: gw.addToRegion(interactor, region);
* ------------------------------------------
* Adds the interactor (which can also be a <code>GLabel</code>) to
* the control strip specified by the <code>region</code> parameter.
* The <code>region</code> parameter must be one of the strings
* <code>"NORTH"</code>, <code>"EAST"</code>, <code>"SOUTH"</code>,
* or <code>"WEST"</code>.
*/
void addToRegion(GInteractor *gobj, std::string region);
void addToRegion(GLabel *gobj, std::string region);
/*
* Method: removeFromRegion
* Usage: gw.removeFromRegion(interactor, region);
* -----------------------------------------------
* Adds the interactor (which can also be a <code>GLabel</code>) to
* the control strip specified by the <code>region</code> parameter.
* The <code>region</code> parameter must be one of the strings
* <code>"NORTH"</code>, <code>"EAST"</code>, <code>"SOUTH"</code>,
* or <code>"WEST"</code>.
*/
void removeFromRegion(GInteractor *gobj, std::string region);
void removeFromRegion(GLabel *gobj, std::string region);
/*
* Method: getGObjectAt
* Usage: GObject *gobj = getGObjectAt(x, y);
* ------------------------------------------
* Returns a pointer to the topmost <code>GObject</code> containing the
* point (<code>x</code>, <code>y</code>), or <code>NULL</code> if no such
* object exists.
*/
GObject *getGObjectAt(double x, double y);
/*
* Method: setRegionAlignment
* Usage: gw.setRegionAlignment(region, align);
* --------------------------------------------
* Sets the alignment of the specified side region as specified by the
* string <code>align</code>. The <code>region</code> parameter must be
* one of the strings <code>"NORTH"</code>, <code>"EAST"</code>,
* <code>"SOUTH"</code>, or <code>"WEST"</code> and the <code>align</code>
* parameter must be <code>"LEFT"</code>, <code>"RIGHT"</code>, or
* <code>"CENTER"</code>. By default, side panels use
* <code>CENTER</code> alignment.
*/
void setRegionAlignment(std::string region, std::string align);
/*
* Operator: ==
* Usage: if (w1 == w2) ...
* ------------------------
* Checks whether the two objects refer to the same window.
*/
bool operator==(GWindow w2);
/*
* Operator: !=
* Usage: if (w1 != w2) ...
* ------------------------
* Checks whether the two objects refer to different windows.
*/
bool operator!=(GWindow w2);
/* Private section */
/**********************************************************************/
/* Note: Everything below this point in the file is logically part */
/* of the implementation and should not be of interest to clients. */
/**********************************************************************/
explicit GWindow(bool visible);
GWindow(double width, double height, bool visible);
GWindow(GWindowData *gwd);
private:
/* Instance variables */
GWindowData *gwd;
/* Private methods */
void initGWindow(double width, double height, bool visible);
friend class Platform;
friend class GKeyEvent;
friend class GMouseEvent;
friend class GWindowEvent;
};
/*
* Function: repaint
* Usage: repaint();
* -----------------
* Issues a request to update all graphics windows. This function
* is called automatically when the program pauses, waits for an
* event, waits for user input on the console, or terminates. As
* a result, most clients never need to call repaint explicitly.
*/
void repaint();
/*
* Function: pause
* Usage: pause(milliseconds);
* ---------------------------
* Pauses for the indicated number of milliseconds. This function is
* useful for animation where the motion would otherwise be too fast.
*/
void pause(double milliseconds);
/*
* Function: getScreenWidth
* Usage: width = getScreenWidth();
* --------------------------------
* Returns the width of the entire display screen.
*/
double getScreenWidth();
/*
* Function: getScreenHeight
* Usage: height = getScreenHeight();
* ----------------------------------
* Returns the height of the entire display screen.
*/
double getScreenHeight();
/*
* Function: convertColorToRGB
* Usage: int rgb = convertColorToRGB(colorName);
* ----------------------------------------------
* Converts a color name into an integer that encodes the
* red, green, and blue components of the color.
*/
int convertColorToRGB(std::string colorName);
/*
* Function: convertRGBToColor
* Usage: int colorName = convertRGBToColor(rgb);
* ----------------------------------------------
* Converts an <code>rgb</code> value into a color name in the
* form <code>"#rrggbb"</code>. Each of the <code>rr</code>,
* <code>gg</code>, and <code>bb</code> values are two-digit
* hexadecimal numbers indicating the intensity of that component.
*/
std::string convertRGBToColor(int rgb);
/*
* Function: waitForClick
* Usage: waitForClick();
* ----------------------
* Waits for a mouse click to occur anywhere in any window.
*/
void waitForClick();
/*
* Function: exitGraphics
* Usage: exitGraphics();
* ----------------------
* Closes all graphics windows and exits from the application without
* waiting for any additional user interaction.
*/
void exitGraphics();
#include "console.h"
#include "private/main.h"
#endif
|