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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
|
/*
* File: gobjects.h
* ----------------
* This file exports a hierarchy of graphical shapes based on
* the model developed for the ACM Java Graphics.
* <include src="pictures/ClassHierarchies/GObjectHierarchy-h.html">
*/
#ifndef _gobjects_h
#define _gobjects_h
#include "gtypes.h"
#include "gwindow.h"
#include "vector.h"
class GCompound;
/*
* Class: GObject
* --------------
* This class is the common superclass of all graphical objects that can
* be displayed on a graphical window. The class <code>GObject</code>
* itself is an <b><i>abstract class</i></b>, which means that you are not
* allowed to construct a <code>GObject</code> directly but must instead
* construct one of the concrete subclasses.
* <include src="pictures/ClassHierarchies/GObjectHierarchy.html">
*
* <p>Most methods used for graphics take a pointer to a <code>GObject</code>
* rather than the <code>GObject</code> itself. Applications that use
* <code>GObject</code> pointers therefore use the arrow operator
* (<code>-></code>) to apply methods to the object pointer.
* For examples illustrating the use of the <code>GObject</code> class, see
* the descriptions of the individual subclasses.
*/
class GObject {
public:
/*
* Destructor: ~GObject
* --------------------
* Frees the storage for the object.
*/
virtual ~GObject();
/*
* Method: getX
* Usage: double x = gobj->getX();
* -------------------------------
* Returns the <i>x</i>-coordinate of the object.
*/
double getX() const;
/*
* Method: getY
* Usage: double y = gobj->getY();
* -------------------------------
* Returns the <i>y</i>-coordinate of the object.
*/
double getY() const;
/*
* Method: getLocation
* Usage: GPoint pt = gobj->getLocation();
* ---------------------------------------
* Returns the location of this object as a <code>GPoint</code>.
*/
GPoint getLocation() const;
/*
* Method: setLocation
* Usage: gobj->setLocation(pt);
* gobj->setLocation(x, y);
* -------------------------------
* Sets the location of this object to the specified point.
*/
void setLocation(const GPoint & pt);
void setLocation(double x, double y);
/*
* Method: move
* Usage: gobj->move(dx, dy);
* --------------------------
* Moves the object on the screen using the displacements
* <code>dx</code> and <code>dy</code>.
*/
void move(double dx, double dy);
/*
* Method: getWidth
* Usage: double width = gobj->getWidth();
* ---------------------------------------
* Returns the width of this object, which is defined to be the width of
* the bounding box.
*/
double getWidth() const;
/*
* Method: getHeight
* Usage: double height = gobj->getHeight();
* -----------------------------------------
* Returns the height of this object, which is defined to be the height
* of the bounding box.
*/
double getHeight() const;
/*
* Method: getSize
* Usage: GDimension size = gobj->getSize();
* -----------------------------------------
* Returns the size of the object as a <code>GDimension</code>.
*/
GDimension getSize() const;
/*
* Method: getBounds
* Usage: GRectangle rect = gobj->getBounds();
* -------------------------------------------
* Returns the bounding box of this object, which is defined to be the
* smallest rectangle that covers everything drawn by the figure. The
* coordinates of this rectangle do not necessarily match the location
* returned by <code>getLocation</code>. Given a <code>GLabel</code>
* object, for example, <code>getLocation</code> returns the coordinates
* of the point on the baseline at which the string begins; the
* <code>getBounds</code> method, by contrast, returns a rectangle that
* covers the entire window area occupied by the string.
*/
virtual GRectangle getBounds() const = 0;
/*
* Method: setLineWidth
* Usage: gobj->setLineWidth(lineWidth);
* -------------------------------------
* Sets the width of the line used to draw this object.
*/
void setLineWidth(double lineWidth);
/*
* Method: getLineWidth
* Usage: double lineWidth = gobj->getLineWidth();
* -----------------------------------------------
* Returns the width of the line used to draw this object.
*/
double getLineWidth() const;
/*
* Method: setColor
* Usage: gobj->setColor(color);
* -----------------------------
* Sets the color used to display this object. The <code>color</code>
* string 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, respectively.
*/
void setColor(std::string color);
void setColor(int rgb);
/*
* Method: getColor
* Usage: string color = gobj->getColor();
* ---------------------------------------
* Returns the color used to display this object. This color is
* always returned as a string in the form <code>"#rrggbb"</code>,
* where <code>rr</code>, <code>gg</code>, and <code>bb</code> are
* the red, green, and blue components of the color, expressed as
* two-digit hexadecimal values.
*/
std::string getColor() const;
/*
* Method: scale
* Usage: gobj->scale(sf);
* gobj->scale(sx, sy);
* ---------------------------
* Scales the object by the specified scale factors. Most clients will use
* the first form, which scales the object by <code>sf</code> in both
* dimensions, so that invoking <code>gobj->scale(2)</code> doubles the
* size of the object. The second form applies independent scale factors
* to the <i>x</i> and <i>y</i> dimensions.
*/
void scale(double sf);
void scale(double sx, double sy);
/*
* Method: rotate
* Usage: gobj->rotate(theta);
* ---------------------------
* Transforms the object by rotating it <code>theta</code> degrees
* counterclockwise around its origin.
*/
void rotate(double theta);
/*
* Method: setVisible
* Usage: gobj->setVisible(flag);
* ------------------------------
* Sets whether this object is visible.
*/
void setVisible(bool flag);
/*
* Method: isVisible
* Usage: if (gobj->isVisible()) ...
* ---------------------------------
* Returns <code>true</code> if this object is visible.
*/
bool isVisible() const;
/*
* Method: sendForward
* Usage: gobj->sendForward();
* ---------------------------
* Moves this object one step toward the front in the <i>z</i> dimension.
* If it was already at the front of the stack, nothing happens.
*/
void sendForward();
/*
* Method: sendToFront
* Usage: gobj->sendToFront();
* ---------------------------
* Moves this object to the front of the display in the <i>z</i> dimension.
* By moving it to the front, this object will appear to be on top of the
* other graphical objects on the display and may hide any objects that
* are further back.
*/
void sendToFront();
/*
* Method: sendBackward
* Usage: gobj->sendBackward();
* ----------------------------
* Moves this object one step toward the back in the <i>z</i> dimension.
* If it was already at the back of the stack, nothing happens.
*/
void sendBackward();
/*
* Method: sendToBack
* Usage: gobj->sendToBack();
* --------------------------
* Moves this object to the back of the display in the <i>z</i> dimension.
* By moving it to the back, this object will appear to be behind the other
* graphical objects on the display and may be obscured by other objects
* in front.
*/
void sendToBack();
/*
* Method: contains
* Usage: if (gobj->contains(pt)) ...
* if (gobj->contains(x, y)) ...
* ------------------------------------
* Returns <code>true</code> if the specified point is inside the object.
*/
bool contains(GPoint pt) const;
virtual bool contains(double x, double y) const;
/*
* Method: getType
* Usage: string type = gobj->getType();
* -------------------------------------
* Returns the concrete type of the object as a string, as in
* <code>"GOval"</code> or <code>"GRect"</code>.
*/
virtual std::string getType() const = 0;
/*
* Method: toString
* Usage: gobj->toString();
* ------------------------
* Returns a printable representation of the object.
*/
virtual std::string toString() const = 0;
/*
* Method: getParent
* Usage: GCompound *parent = gobj->getParent();
* ---------------------------------------------
* Returns a pointer to the <code>GCompound</code> that contains this
* object. Every <code>GWindow</code> is initialized to contain a single
* <code>GCompound</code> that is aligned with the window. Adding
* objects to the window adds them to that <code>GCompound</code>,
* which means that every object you add to the window has a parent.
* Calling <code>getParent</code> on the top-level <code>GCompound</code>
* returns <code>NULL</code>.
*/
GCompound *getParent() const;
/* Private section */
private:
const GObject & operator=(const GObject &) { return *this; }
GObject(const GObject &) { }
/* Instance variables */
protected:
double x; /* The x coordinate of the origin */
double y; /* The y coordinate of the origin */
double lineWidth; /* The width of the line in pixels */
std::string color; /* The color of the object */
bool visible; /* Indicates if object is visible */
bool transformed; /* Indicates if object is transformed */
GCompound *parent; /* Pointer to the parent */
protected:
GObject();
friend class GArc;
friend class GButton;
friend class GCheckBox;
friend class GChooser;
friend class GCompound;
friend class GImage;
friend class GInteractor;
friend class GLabel;
friend class GLine;
friend class GOval;
friend class GPolygon;
friend class GRect;
friend class GRoundRect;
friend class GSlider;
friend class GTextField;
friend class G3DRect;
};
/*
* Class: GRect
* ------------
* This class represents a graphical object whose appearance consists of
* a rectangular box. For example, the following code adds a filled, red
* <nobr>200<font class=sansserif size=-1>x</font>100</nobr> rectangle
* at the upper left corner of the graphics window:
*
*<pre>
* int main() {
* GWindow gw;
* cout << "This program draws a red rectangle at (0, 0)." << endl;
* GRect *rect = new GRect(0, 0, 200, 100);
* rect->setFilled(true);
* rect->setColor("RED");
* gw.add(rect);
* return 0;
* }
*</pre>
*/
class GRect : public GObject {
public:
/*
* Constructor: GRect
* Usage: GRect *rect = new GRect(width, height);
* GRect *rect = new GRect(x, y, width, height);
* ----------------------------------------------------
* Constructs a rectangle with the specified width and height. The first
* form is positioned at the origin; the second at the coordinates
* given by <code>x</code> and <code>y</code>.
*/
GRect(double width, double height);
GRect(double x, double y, double width, double height);
/*
* Destructor: ~GRect
* ------------------
* Frees any resources maintained by this object.
*/
virtual ~GRect();
/*
* Method: setSize
* Usage: rect->setSize(size);
* rect->setSize(width, height);
* ------------------------------------
* Changes the size of this rectangle to the specified width and height.
*/
void setSize(const GDimension & size);
void setSize(double width, double height);
/*
* Method: setBounds
* Usage: rect->setBounds(rect);
* rect->setBounds(x, y, width, height);
* --------------------------------------------
* Changes the bounds of this rectangle to the specified values.
*/
void setBounds(const GRectangle & size);
void setBounds(double x, double y, double width, double height);
/*
* Method: setFilled
* Usage: rect->setFilled(flag);
* -----------------------------
* Sets the fill status for the rectangle, where <code>false</code> is
* outlined and <code>true</code> is filled.
*/
void setFilled(bool flag);
/*
* Method: isFilled
* Usage: if (rect->isFilled()) ...
* --------------------------------
* Returns <code>true</code> if the rectangle is filled.
*/
bool isFilled() const;
/*
* Method: setFillColor
* Usage: rect->setFillColor(color);
* ---------------------------------
* Sets the color used to display the filled region of this rectangle.
*/
void setFillColor(std::string color);
void setFillColor(int rgb);
/*
* Method: getFillColor
* Usage: string color = rect->getFillColor();
* -------------------------------------------
* Returns the color used to display the filled region of this rectangle. If
* none has been set, <code>getFillColor</code> returns the empty string.
*/
std::string getFillColor() const;
/* Prototypes for the virtual methods */
virtual GRectangle getBounds() const;
virtual std::string getType() const;
virtual std::string toString() const;
protected:
/* Instance variables */
double width; /* The width of the rectangle */
double height; /* The height of the rectangle */
bool fillFlag; /* Indicates whether the object is filled */
std::string fillColor; /* Color used to fill the object */
/* Protected methods */
GRect();
virtual void createGRect(double width, double height);
};
/*
* Class: GRoundRect
* -----------------
* This class represents a graphical object whose appearance consists
* of a rectangular box with rounded corners.
*/
class GRoundRect : public GRect {
public:
/*
* Constructor: GRoundRect
* Usage: GRoundRect *rect = new GRoundRect(width, height);
* GRoundRect *rect = new GRoundRect(width, height, corner);
* GRoundRect *rect = new GRoundRect(x, y, width, height);
* GRoundRect *rect = new GRoundRect(x, y, width, height, corner);
* ----------------------------------------------------------------------
* Constructs a new rectangle with the specified width and height. If
* the <code>x</code> and <code>y</code> parameters are specified, they
* are used to specify the origin. The <code>corner</code> parameter
* specifies the diameter of the arc forming the corner.
*/
GRoundRect(double width, double height);
GRoundRect(double width, double height, double corner);
GRoundRect(double x, double y, double width, double height);
GRoundRect(double x, double y, double width, double height, double corner);
/*
* Destructor: ~GRoundRect
* -----------------------
* Frees any resources maintained by this object.
*/
virtual ~GRoundRect();
/* Prototypes for the virtual methods */
virtual std::string getType() const;
virtual std::string toString() const;
protected:
double corner;
/* Protected methods */
void createGRoundRect(double width, double height, double corner);
};
/*
* Class: G3DRect
* --------------
* This graphical object subclass represents a rectangular box that can
* be raised or lowered.
*/
class G3DRect : public GRect {
public:
/*
* Constructor: G3DRect
* Usage: G3DRect *rect = new G3DRect(width, height);
* G3DRect *rect = new G3DRect(width, height, raised);
* G3DRect *rect = new G3DRect(x, y, width, height);
* G3DRect *rect = new G3DRect(x, y, width, height, raised);
* ----------------------------------------------------------------
* Constructs a new 3D rectangle with the specified width and height. If
* the <code>x</code> and <code>y</code> parameters are specified, they
* are used to specify the origin. The <code>raised</code> parameter
* determines whether the rectangle should be drawn with highlights that
* suggest that it is raised about the background.
*/
G3DRect(double width, double height);
G3DRect(double width, double height, bool raised);
G3DRect(double x, double y, double width, double height);
G3DRect(double x, double y, double width, double height, bool raised);
/*
* Destructor: ~G3DRect
* --------------------
* Frees any resources maintained by this object.
*/
virtual ~G3DRect();
/*
* Method: setRaised
* Usage: rect->setRaised(raised);
* -------------------------------
* Indicates whether this object appears raised.
*/
void setRaised(bool raised);
/*
* Method: isRaised
* Usage: if (rect->isRaised()) ...
* --------------------------------
* Returns <code>true</code> if this object appears raised.
*/
bool isRaised() const;
/* Prototypes for the virtual methods */
virtual std::string getType() const;
virtual std::string toString() const;
protected:
bool raised;
/* Protected methods */
void createG3DRect(double width, double height, bool raised);
};
/*
* Class: GOval
* ------------
* This graphical object subclass represents an oval inscribed in
* a rectangular box. For example, the following code displays a
* filled green oval inscribed in the graphics window:
*
*<pre>
* int main() {
* GWindow gw;
* cout << "This program draws a green oval filling the window." << endl;
* GOval *oval = new GOval(gw.getWidth(), gw.getHeight());
* oval->setFilled(true);
* oval->setColor("GREEN");
* gw.add(oval);
* return 0;
* }
*</pre>
*/
class GOval : public GObject {
public:
/*
* Constructor: GOval
* Usage: GOval *oval = new GOval(width, height);
* GOval *oval = new GOval(x, y, width, height);
* ----------------------------------------------------
* Constructs a new oval inscribed in the specified rectangle. The
* first form is positioned at the origin; the second at the coordinates
* given by <code>x</code> and <code>y</code>.
*/
GOval(double width, double height);
GOval(double x, double y, double width, double height);
/*
* Destructor: ~GOval
* ------------------
* Frees any resources maintained by this object.
*/
virtual ~GOval();
/*
* Method: setSize
* Usage: oval->setSize(size);
* oval->setSize(width, height);
* ------------------------------------
* Changes the size of the bounding rectangle to the specified width
* and height.
*/
void setSize(const GDimension & size);
void setSize(double width, double height);
/*
* Method: setBounds
* Usage: oval->setBounds(rect);
* oval->setBounds(x, y, width, height);
* --------------------------------------------
* Changes the bounds of the oval to the specified values.
*/
void setBounds(const GRectangle & size);
void setBounds(double x, double y, double width, double height);
/*
* Method: setFilled
* Usage: oval->setFilled(flag);
* -----------------------------
* Sets the fill status for the oval, where <code>false</code> is
* outlined and <code>true</code> is filled.
*/
void setFilled(bool flag);
/*
* Method: isFilled
* Usage: if (oval->isFilled()) ...
* --------------------------------
* Returns <code>true</code> if the oval is filled.
*/
bool isFilled() const;
/*
* Method: setFillColor
* Usage: oval->setFillColor(color);
* ---------------------------------
* Sets the color used to display the filled region of this oval.
*/
void setFillColor(std::string color);
void setFillColor(int rgb);
/*
* Method: getFillColor
* Usage: string color = oval->getFillColor();
* -------------------------------------------
* Returns the color used to display the filled region of this oval. If
* none has been set, <code>getFillColor</code> returns the empty string.
*/
std::string getFillColor() const;
/* Prototypes for the virtual methods */
virtual GRectangle getBounds() const;
virtual bool contains(double x, double y) const;
virtual std::string getType() const;
virtual std::string toString() const;
protected:
/* Instance variables */
double width; /* The width of the bounding rectangle */
double height; /* The height of the bounding rectangle */
bool fillFlag; /* Indicates whether the object is filled */
std::string fillColor; /* Color used to fill the object */
/* Protected methods */
void createGOval(double width, double height);
};
/*
* Class: GArc
* -----------
* This graphical object subclass represents an elliptical arc. The
* arc is specified by the following parameters:
*
* <p><ul>
* <li>The coordinates of the bounding rectangle (<code>x</code>,
* <code>y</code>, <code>width</code>, <code>height</code>)
* <li>The angle at which the arc starts (<code>start</code>)
* <li>The number of degrees that the arc covers (<code>sweep</code>)
* </ul>
*
* <p>All angles in a <code>GArc</code> description are measured in
* degrees moving counterclockwise from the +<i>x</i> axis. Negative
* values for either <code>start</code> or <code>sweep</code> indicate
* motion in a clockwise direction.
* <include src="pictures/GObjectDiagrams/GArcGeometry.html">
*/
class GArc : public GObject {
public:
/*
* Constructor: GArc
* Usage: GArc *arc = new GArc(width, height, start, sweep);
* GArc *arc = new GArc(x, y, width, height, start, sweep);
* ---------------------------------------------------------------
* Creates a new <code>GArc</code> object consisting of an elliptical arc.
* The first form creates a <code>GArc</code> whose origin is the point
* (0, 0); the second form positions the <code>GArc</code> at the
* point (<code>x</code>, <code>y</code>).
* <include src="pictures/GObjectDiagrams/GArcExamples.html">
*/
GArc(double width, double height, double start, double sweep);
GArc(double x, double y, double width, double height,
double start, double sweep);
/*
* Method: setStartAngle
* Usage: arc->setStartAngle(start);
* ---------------------------------
* Sets the starting angle for this <code>GArc</code> object.
*/
void setStartAngle(double start);
/*
* Method: getStartAngle
* Usage: double angle = arc->getStartAngle();
* -------------------------------------------
* Returns the starting angle for this <code>GArc</code> object.
*/
double getStartAngle() const;
/*
* Method: setSweepAngle
* Usage: arc->setSweepAngle(start);
* ---------------------------------
* Sets the sweep angle for this <code>GArc</code> object.
*/
void setSweepAngle(double start);
/*
* Method: getSweepAngle
* Usage: double angle = arc->getSweepAngle();
* -------------------------------------------
* Returns the sweep angle for this <code>GArc</code> object.
*/
double getSweepAngle() const;
/*
* Method: getStartPoint
* Usage: GPoint pt = arc->getStartPoint();
* ----------------------------------------
* Returns the point at which the arc starts.
*/
GPoint getStartPoint() const;
/*
* Method: getEndPoint
* Usage: GPoint pt = arc->getEndPoint();
* --------------------------------------
* Returns the point at which the arc ends.
*/
GPoint getEndPoint() const;
/*
* Method: setFrameRectangle
* Usage: arc->setFrameRectangle(rect);
* arc->setFrameRectangle(x, y, width, height);
* ---------------------------------------------------
* Changes the boundaries of the rectangle used to frame the arc.
*/
void setFrameRectangle(const GRectangle & rect);
void setFrameRectangle(double x, double y, double width, double height);
/*
* Method: getFrameRectangle
* Usage: GRectangle rect = arc->getFrameRectangle();
* --------------------------------------------------
* Returns the boundaries of the rectangle used to frame the arc.
*/
GRectangle getFrameRectangle() const;
/*
* Method: setFilled
* Usage: arc->setFilled(flag);
* ----------------------------
* Sets the fill status for the arc, where <code>false</code> is
* outlined and <code>true</code> is filled. If a <code>GArc</code> is
* unfilled, the figure consists only of the arc itself. If a
* <code>GArc</code> is filled, the figure consists of the
* pie-shaped wedge formed by connecting the endpoints of the arc to
* the center. As an example, the following program draws a 270-degree
* arc starting at 45 degrees, filled in yellow, much like the character
* in the PacMan video game:
*
*<pre>
* int main() {
* GWindow gw;
* cout << "This program draws the PacMan character." << endl;
* double cx = gw.getWidth() / 2;
* double cy = gw.getHeight() / 2;
* double r = 25;
* GArc *pacman = new GArc(cx - r, cy - r, 2 * r, 2 * r, 45, 270);
* pacman->setFilled(true);
* pacman->setFillColor("YELLOW");
* gw.add(pacman);
* return 0;
* }
*</pre>
* <include src="pictures/GObjectDiagrams/PacMan.html">
*/
void setFilled(bool flag);
/*
* Method: isFilled
* Usage: if (arc->isFilled()) ...
* -------------------------------
* Returns <code>true</code> if the arc is filled.
*/
bool isFilled() const;
/*
* Method: setFillColor
* Usage: arc->setFillColor(color);
* --------------------------------
* Sets the color used to display the filled region of this arc.
* Colors are specified as strings as described in the notes for the
* <a href="GObject-class.html#Method:setColor"><code>setColor</code></a>
* method.
*/
void setFillColor(std::string color);
void setFillColor(int rgb);
/*
* Method: getFillColor
* Usage: string color = arc->getFillColor();
* ------------------------------------------
* Returns the color used to display the filled region of this arc. If
* none has been set, <code>getFillColor</code> returns the empty string.
*/
std::string getFillColor() const;
/* Prototypes for the virtual methods */
virtual GRectangle getBounds() const;
virtual bool contains(double x, double y) const;
virtual std::string getType() const;
virtual std::string toString() const;
private:
GPoint getArcPoint(double theta) const;
bool containsAngle(double theta) const;
void createGArc(double width, double height, double start, double sweep);
/* Instance variables */
double start; /* Starting angle of the arc */
double sweep; /* How many degrees the arc runs */
double frameWidth; /* The width of the bounding box */
double frameHeight; /* The height of the bounding box */
std::string fillColor; /* The color of the interior */
bool fillFlag; /* Indicates if the arc is filled */
};
/*
* Class: GCompound
* ----------------
* This graphical object subclass consists of a collection
* of other graphical objects. Once assembled, the internal objects
* can be manipulated as a unit. The <code>GCompound</code> keeps
* track of its own position, and all items within it are drawn
* relative to that location.
*/
class GCompound : public GObject {
public:
/*
* Constructor: GCompound
* Usage: GCompound *comp = new GCompound();
* -----------------------------------------
* Creates a <code>GCompound</code> object with no internal components.
*/
GCompound();
/*
* Method: add
* Usage: comp->add(gobj);
* comp->add(gobj, x, y);
* -----------------------------
* Adds a new graphical object to the <code>GCompound</code>. The second
* form moves the object to the point (<code>x</code>, <code>y</code>) first.
*/
void add(GObject *gobj);
void add(GObject *gobj, double x, double y);
/*
* Method: remove
* Usage: comp->remove(gobj);
* --------------------------
* Removes the specified object from the <code>GCompound</code>.
*/
void remove(GObject *gobj);
/*
* Method: removeAll
* Usage: comp->removeAll();
* -------------------------
* Removes all graphical objects from the <code>GCompound</code>.
*/
void removeAll();
/*
* Method: getElementCount
* Usage: int n = comp->getElementCount();
* ---------------------------------------
* Returns the number of graphical objects stored in the
* <code>GCompound</code>.
*/
int getElementCount();
/*
* Method: getElement
* Usage: GObject *gobj = comp->getElement(index);
* -----------------------------------------------
* Returns a pointer to the graphical object at the specified index,
* numbering from back to front in the the <i>z</i> dimension.
*/
GObject *getElement(int index);
/* Prototypes for the virtual methods */
virtual GRectangle getBounds() const;
virtual bool contains(double x, double y) const;
virtual std::string getType() const;
virtual std::string toString() const;
private:
void sendForward(GObject *gobj);
void sendToFront(GObject *gobj);
void sendBackward(GObject *gobj);
void sendToBack(GObject *gobj);
int findGObject(GObject *gobj);
void removeAt(int index);
/* Instance variables */
Vector<GObject *> contents;
/* Friend declarations */
friend class GObject;
};
/*
* Class: GImage
* -------------
* This graphical object subclass represents an image from a file.
* For example, the following code displays a <code>GImage</code>
* containing the Stanford tree at the center of the window, assuming
* that the image file <code>StanfordTree.png</code> exists, either in
* the current directory or an <code>images</code> subdirectory:
*
*<pre>
* int main() {
* GWindow gw;
* cout << "This program draws the Stanford tree." << endl;
* GImage *tree = new GImage("StanfordTree.png");
* double x = (gw.getWidth() - tree->getWidth()) / 2;
* double y = (gw.getHeight() - tree->getHeight()) / 2;
* gw.add(tree, x, y);
* return 0;
* }
*</pre>
*/
class GImage : public GObject {
public:
/*
* Constructor: GImage
* Usage: GImage *image = new GImage(filename);
* GImage *image = new GImage(filename, x, y);
* --------------------------------------------------
* Constructs a new image by loading the image from the specified
* file, which is either in the current directory or a subdirectory named
* <code>images</code>. By default, the upper left corner of the image
* appears at the origin; the second form automatically sets the location
* to the point (<code>x</code>, <code>y</code>).
*/
GImage(std::string filename);
GImage(std::string filename, double x, double y);
/* Prototypes for the virtual methods */
virtual GRectangle getBounds() const;
virtual std::string getType() const;
virtual std::string toString() const;
private:
std::string filename;
double width;
double height;
void createGImage(std::string filename);
};
/*
* Class: GLabel
* -------------
* This graphical object subclass represents a text string. For
* example, the following code adds a <code>GLabel</code> containing
* the string <code>"hello, world"</code> to the center of the window:
*
*<pre>
* int main() {
* GWindow gw;
* cout << "This program draws the 'hello, world' message." << endl;
* GLabel *label = new GLabel("hello, world");
* label->setFont("SansSerif-18");
* double x = (gw.getWidth() - label->getWidth()) / 2;
* double y = (gw.getHeight() + label->getFontAscent()) / 2;
* gw.add(label, x, y);
* return 0;
* }
*</pre>
*
* Controlling the appearance and positioning of a <code>GLabel</code>
* depends on understanding the following terms:
*
* <p><ul>
* <li>The <b><i>baseline</i></b> is the horizontal line on which the
* characters rest.
* <li>The <b><i>origin</i></b> is the point on the baseline at which
* the label begins.
* <li>The <b><i>height</i></b> is the distance that separate two
* successive lines.
* <li>The <b><i>ascent</i></b> is the maximum distance a character
* in this font extends above the baseline.
* <li>The <b><i>descent</i></b> is the maximum distance a character
* in this font extends below the baseline.
* </ul>
* <include src="pictures/GObjectDiagrams/GLabelGeometry.html">
*/
class GLabel : public GObject {
public:
/*
* Constructor: GLabel
* Usage: GLabel *label = new GLabel(str);
* GLabel *label = new GLabel(str, x, y);
* ---------------------------------------------
* Creates a <code>GLabel</code> object containing the specified string.
* By default, the baseline of the first character appears at the origin;
* the second form automatically resets the location of the
* <code>GLabel</code> to the point (<code>x</code>, <code>y</code>).
*/
GLabel(std::string str);
GLabel(std::string str, double x, double y);
/*
* Method: setFont
* Usage: label->setFont(font);
* ----------------------------
* Changes the font used to display the <code>GLabel</code> as specified by
* the string <code>font</code>, which has the following format:
*
*<pre>
* family-style-size
*</pre>
*
* where both <code>style</code> and <code>size</code> are optional.
* If any of these elements are missing or specified as an asterisk,
* the existing value is retained.
*/
void setFont(std::string font);
/*
* Method: getFont
* Usage: string font = label->getFont();
* --------------------------------------
* Returns the current font for the <code>GLabel</code>.
*/
std::string getFont() const;
/*
* Method: setLabel
* Usage: label->setLabel(str);
* ----------------------------
* Changes the string stored within the <code>GLabel</code> object, so that
* a new text string appears on the display.
*/
void setLabel(std::string str);
/*
* Method: getLabel
* Usage: string str = label->getLabel();
* --------------------------------------
* Returns the string displayed by this object.
*/
std::string getLabel() const;
/*
* Method: getFontAscent
* Usage: double ascent = label->getFontAscent();
* ----------------------------------------------
* Returns the maximum distance strings in this font extend above
* the baseline.
*/
double getFontAscent() const;
/*
* Method: getFontDescent
* Usage: double descent = label->getFontDescent();
* ------------------------------------------------
* Returns the maximum distance strings in this font descend below
* the baseline.
*/
double getFontDescent() const;
/* Prototypes for the virtual methods */
virtual GRectangle getBounds() const;
virtual std::string getType() const;
virtual std::string toString() const;
private:
/* Instance variables */
std::string str; /* The string displayed by the label */
std::string font; /* The font string of the label */
double width; /* Width of the bounding box */
double height; /* Height of the bounding box */
double ascent; /* Font ascent */
double descent; /* Font descent */
void createGLabel(const std::string & str);
};
/*
* Class: GLine
* ------------
* This graphical object subclass represents a line segment. For
* example, the following code adds lines that mark the diagonals
* of the graphics window:
*
*<pre>
* int main() {
* GWindow gw;
* cout << "This program draws the diagonals on the window." << endl;
* gw.add(new GLine(0, 0, gw.getWidth(), gw.getHeight()));
* gw.add(new GLine(0, gw.getHeight(), gw.getWidth(), 0));
* return 0;
* }
*</pre>
*/
class GLine : public GObject {
public:
/*
* Constructor: GLine
* Usage: GLine *gline = new GLine(x0, y0, x1, y1);
* ------------------------------------------------
* Constructs a line segment from its endpoints. The point
* (<code>x0</code>, <code>y0</code>) defines the start of the
* line and the point (<code>x1</code>, <code>y1</code>) defines
* the end.
*/
GLine(double x0, double y0, double x1, double y1);
/*
* Method: setStartPoint
* Usage: line->setStartPoint(x, y);
* ---------------------------------
* Sets the initial point in the line to (<code>x</code>, <code>y</code>),
* leaving the end point unchanged. This method is therefore different from
* <code>setLocation</code>, which moves both components of the line segment.
*/
void setStartPoint(double x, double y);
/*
* Method: getStartPoint
* Usage: GPoint pt = line->getStartPoint();
* -----------------------------------------
* Returns the point at which the line starts.
*/
GPoint getStartPoint() const;
/*
* Method: setEndPoint
* Usage: line->setEndPoint(x, y);
* -------------------------------
* Sets the end point in the line to (<code>x</code>, <code>y</code>),
* leaving the start point unchanged. This method is therefore different from
* <code>setLocation</code>, which moves both components of the line segment.
*/
void setEndPoint(double x, double y);
/*
* Method: getEndPoint
* Usage: GPoint pt = line->getEndPoint();
* ---------------------------------------
* Returns the point at which the line ends.
*/
GPoint getEndPoint() const;
/* Prototypes for the virtual methods */
virtual GRectangle getBounds() const;
virtual bool contains(double x, double y) const;
virtual std::string getType() const;
virtual std::string toString() const;
protected:
/* Instance variables */
double dx; /* The x displacement of the line */
double dy; /* The y displacement of the line */
};
/*
* Class: GPolygon
* ---------------
* This graphical object subclass represents a polygon bounded by
* line segments. The <code>GPolygon</code> constructor creates an
* empty polygon. To complete the figure, you need to add vertices
* to the polygon using the methods
* <a href="#Method:addVertex"><code>addVertex</code></a>,
* <a href="#Method:addEdge"><code>addEdge</code></a>,
* and <a href="#Method:addPolarEdge"><code>addPolarEdge</code></a>.
* As an example, the following code adds a filled red octagon to
* the center of the window:
*
*<pre>
* int main() {
* GWindow gw;
* cout << "This program draws a red octagon." << endl;
* double edge = 75;
* GPolygon *stopSign = new GPolygon();
* stopSign->addVertex(-edge / 2, edge / 2 + edge / sqrt(2.0));
* for (int i = 0; i < 8; i++) {
* stopSign->addPolarEdge(edge, 45 * i);
* }
* stopSign->setFilled(true);
* stopSign->setColor("RED");
* gw.add(stopSign, gw.getWidth() / 2, gw.getHeight() / 2);
* return 0;
* }
*</pre>
* <include src="pictures/GObjectDiagrams/StopSign.html">
*/
class GPolygon : public GObject {
public:
/*
* Constructor: GPolygon
* Usage: GPolygon *poly = new GPolygon();
* ---------------------------------------
* Constructs a new empty polygon at the origin.
*/
GPolygon();
/*
* Method: addVertex
* Usage: poly->addVertex(x, y);
* -----------------------------
* Adds a vertex at (<code>x</code>, <code>y</code>) relative to the polygon
* origin.
*/
void addVertex(double x, double y);
/*
* Method: addEdge
* Usage: poly->addEdge(dx, dy);
* -----------------------------
* Adds an edge to the polygon whose components are given by the displacements
* <code>dx</code> and <code>dy</code> from the last vertex.
*/
void addEdge(double dx, double dy);
/*
* Method: addPolarEdge
* Usage: poly->addPolarEdge(r, theta);
* ------------------------------------
* Adds an edge to the polygon specified in polar coordinates. The length
* of the edge is given by <code>r</code>, and the edge extends in
* direction <code>theta</code>, measured in degrees counterclockwise
* from the +x axis.
*/
void addPolarEdge(double r, double theta);
/*
* Method: getVertices
* Usage: Vector<GPoint> vec = poly->getVertices();
* ------------------------------------------------
* Returns a vector of the points in the polygon.
*/
Vector<GPoint> getVertices() const;
/*
* Method: setFilled
* Usage: poly->setFilled(flag);
* -----------------------------
* Sets the fill status for the polygon, where <code>false</code> is
* outlined and <code>true</code> is filled.
*/
void setFilled(bool flag);
/*
* Method: isFilled
* Usage: if (poly->isFilled()) ...
* --------------------------------
* Returns <code>true</code> if the polygon is filled.
*/
bool isFilled() const;
/*
* Method: setFillColor
* Usage: poly->setFillColor(color);
* ---------------------------------
* Sets the color used to display the filled region of this polygon.
*/
void setFillColor(std::string color);
void setFillColor(int rgb);
/*
* Method: getFillColor
* Usage: string color = poly->getFillColor();
* -------------------------------------------
* Returns the color used to display the filled region of this polygon. If
* none has been set, <code>getFillColor</code> returns the empty string.
*/
std::string getFillColor() const;
/* Prototypes for the virtual methods */
virtual GRectangle getBounds() const;
virtual bool contains(double x, double y) const;
virtual std::string getType() const;
virtual std::string toString() const;
private:
/* Instance variables */
Vector<GPoint> vertices; /* The vertices of the polygon */
double cx; /* The most recent x coordinate */
double cy; /* The most recent y coordinate */
std::string fillColor; /* The color of the interior */
bool fillFlag; /* Indicates if object is filled */
};
#endif
|