Creating a picture graphic

The following code creates the picture shown in Figure 91. The picture has a red background fill, a 5.0 world-coordinate unit pen around the background to frame the picture, and two polygons. To create a picture, you instantiate the TPicture class, get the picture port, and draw 2-D or 3-D graphics into the picture port. In this example, the background is drawn into the picture first, followed by the white triangle, and then the black triangle.


Include files

You need to add the following include file to use TPicture.

    #include <Taligent/Picture.h>

Code

    1  TFillAndFrameBundle* pictureBundle = new TFillAndFrameBundle( TRGBColor( .75, .5, .5 ), 
    2                                                              TRGBColor( 0, 0, 0 ), 5.0 );
    3  TFillAndFrameBundle* whitePolyBundle = new TFillAndFrameBundle( TRGBColor( 1, 1, 1 ),
    4                                                              TRGBColor( 0, 0, 0 ) );
    5  TFillBundle* blackPolyBundle = new TFillBundle( TRGBColor( 0, 0, 0 ) );
    6  
    7  TPicture starPicture( );
    8  TGrafPort* picturePort = starPicture.GetGrafPort();
    9  picturePort.Draw( TGRect( 0, 0, 200, 250 ), pictureBundle );
    10  
    11  TGPolygon aPolygonGeometry;
    12  aPolygonGeometry.Append( TGPoint( 100, 60 ) );
    13  aPolygonGeometry.Append( TGPoint( 140, 115 ) );
    14  aPolygonGeometry.Append( TGPoint( 60, 115 ) );
    15  
    16  TPolygon whitePolygon( aPolygonGeometry, whitePolyBundle );
    17  whitePolygon.Draw( *picturePort );
    18  
    19  TPolygon blackPolygon( whitePolygon );
    20  blackPolygon.AdoptBundle( blackPolyBundle );
    21  
    22  blackPolygon.RotateBy( 180, blackPolygon.GetGeometricBounds().GetCenter() );
    23  blackPolygon.TranslateBy( TGPoint( 0, 27 ) );
    24  
    25  blackPolygon.Draw( *picturePort );
    26  
    27  starPicture.Draw( thePort );
Lines 1 through 5: Three attribute bundles are created: pictureBundle, whitePolyBundle, and blackPolyBundle.

Line 7: Create a picture instance from
TPicture. The TPicture constructor has an attribute bundle parameter with a default value of Nil. An attribute bundle at the TPicture level has no effect so there is no reason to create one and pass it in the constructor. All attributes are applied to the graphics drawn into the picture only. TPicture has a Nil attribute bundle because all MGraphic derived classes must have an attribute bundle in their constructor.

Lines 8 and 9: Get the picture port and draw a rectangle into it with an attribute bundle to make the picture background. Just like the 2-D image classes, you get a drawing port into which you draw the graphics.

Lines 11 through 17: Create the white polygon from a geometry and the whitePolyBundle.

Lines 19 and 20: Create the black polygon by copying the whitePolygon geometry and adopting blackPolyBundle (whitePolyBundle is automatically orphaned when you do this).

Lines 22 and 23: A rotation and transformation is applied to reposition the black polygon.

Line 25: Draw blackPolygon drawn into the picture port. The picture can be transformed as a whole only.

Line 27: To see the picture in the view, the starPicture is drawn to the view port.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker