Sourceforge.net - The VCF's Project Host
   The VCF Website Home   |   Online Discussion Forums   |   Sourceforge.net Project Page   

6.7. GraphicsKit

6.7.1. How do I draw lines ? How do I draw thick lines ?
6.7.2. How do I draw ellipses? Rectangles?
6.7.3. Is there any support for anti-aliased graphics?
6.7.4. How do I use anti-aliased drawing?
6.7.5. Is there an equivalent for the Win32 API call DrawEdge() ?
6.7.6. How do I change the color of a pen or brush ? Are there even pens or brushes ?
6.7.7. What's the deal with GraphicsContext::strokePath() and GraphicsContext::fillPath() methods?
6.7.8. How do I create an Image? Can I create one from a file? A resource?
6.7.9. How do I draw an Image ?
6.7.10. How do I modify Images ?
6.7.11. What's the Stroke class for? What about the Fill class?
6.7.12. How do I draw text?
6.7.13. How do I measure the length of some text I'd like to draw? What about the height?
6.7.14. How do I change the font that is used to render some text?
6.7.15. How do I change the color of text?
6.7.1.

How do I draw lines ? How do I draw thick lines ?

Drawing lines is accomplished with the GraphicsContext::moveTo() and GraphicsContext::lineTo() methods, and to render the lines to the screen, you call GraphicsContext::strokePath(). A quick example:


GraphicsContext* gc = getMyGraphicsContext();//get the graphics context some how....
//draw a square
gc->moveTo( 0.0, 0.0 );
gc->lineTo( 100.0, 0.0 );
gc->lineTo( 100.0, 100.0 );
gc->lineTo( 0.0, 100.0 );
gc->lineTo( 0.0, 0.0 );

//render 
gc->strokePath();

Drawing with thick lines is also easy, just adjust the current stroke width of the GraphicsContext by calling GraphicsContext::setStrokeWidth(), for example:


GraphicsContext* gc = getMyGraphicsContext();//get the graphics context some how....
//draw a square
gc->moveTo( 0.0, 0.0 );
gc->lineTo( 100.0, 0.0 );
gc->lineTo( 100.0, 100.0 );
gc->lineTo( 0.0, 100.0 );
gc->lineTo( 0.0, 0.0 );

//render 
gc->strokePath();

gc->setStrokeWidth( 10.0 );
//draw a thicker square
gc->moveTo( 200.0, 200.0 );
gc->lineTo( 300.0, 200.0 );
gc->lineTo( 300.0, 300.0 );
gc->lineTo( 200.0, 300.0 );
gc->lineTo( 200.0, 200.0 );

//render 
gc->strokePath();

6.7.2.

How do I draw ellipses? Rectangles?

To draw an ellips, use the GraphicsContext::ellipse method. To draw a rectangle, use the GraphicsContext::rectangle() method.


GraphicsContext* gc = getMyGraphicsContext();//get the graphics context some how....
gc->ellipse( 20, 20, 100, 100 );
gc->rectangle( 400, 400, 500, 600 );
gc->strokePath();

6.7.3.

Is there any support for anti-aliased graphics?

Yes, the VCF uses the AGG C++ graphics library for performing advanced anti-aliased vector graphics.

6.7.4.

How do I use anti-aliased drawing?

If you're drawing in a Control, then the simplest way is to turn it on by calling Control::setUsingRenderBuffer(true), and then use the BasicStroke and BasicFill classes to draw with. You can see an example of this in the vcf/examples/StrokesAndFills directory.

6.7.5.

Is there an equivalent for the Win32 API call DrawEdge() ?

Yes, you can call the GraphicsContext::drawEdge() method.


GraphicsContext* gc = getMyGraphicsContext();//get the graphics context some how....
long edgeSides = GraphicsContext::etAllSides;
long edgeStyle = GraphicsContext::etSunken;
gc->drawEdge( &Rect(20,20,100,100), edgeSides, edgeStyle );

6.7.6.

How do I change the color of a pen or brush ? Are there even pens or brushes ?

There are no Pen or Brush objects in the VCF. Instead you set the basic attributes of a GraphicsContext object. For more advanced graphics features you would implement specific Stroke or Fill classes.

To change the color that the GraphicsContext uses to fill or stroke a path you just need to call GraphicsContext::setColor() and pass in a pointer to a color object. For example:


GraphicsContext* gc = getMyGraphicsContext();//get the graphics context some how....
gc->setColor( Color::getColor("red") );

gc->moveTo( 10, 10 );
gc->lineTo( 10, 100 );
gc->strokePath();
			
			

This will draw a straight vertical line, 90 pixels long, with the color red.

6.7.7.

What's the deal with GraphicsContext::strokePath() and GraphicsContext::fillPath() methods?

The GraphicsContext::strokePath() or GraphicsContext::fillPath() are used to either stroke, or fill the current path(s). A path is made using the GraphicsContext's moveTo(), lineTo(), curveTo(),

6.7.8.

How do I create an Image? Can I create one from a file? A resource?

You can create an image using the GraphicsToolkit::createImage() functions. You can create an image either by specifying an width and height, or by passing in the file name of a valid image. Currently the GraphicsToolkit supports at least JPEG and PNG images, plus whatever platform specific image types. For Win32 this includes BMP image types.

6.7.9.

How do I draw an Image ?

Call the GraphicsContext::drawImageXXX functions. You'll need to have a valid GraphicsContext, and a valid image. If you pass in a rectangle it's usually some sub section of the image itself, plus the cordinates where you want the image bits rendered to.

6.7.10.

How do I modify Images ?

You can modify an image at either the pixel level or by getting the image's GraphicsContext and make various graphics calls to that.

6.7.11.

What's the Stroke class for? What about the Fill class?

The Stroke and Fill classes are interfaces used to describe a stroke, or the rendering of a path, and a fill, or the filling in of a path. You don't create instance directly of a Stroke or Fill, rather you create instance of derived classes, like BasicFill or BasicStroke.

6.7.12.

How do I draw text?

Call the GraphicsContext::textAt() functions or GraphicsContext::textBoundedBy() methods.

6.7.13.

How do I measure the length of some text I'd like to draw? What about the height?

Call the GraphicsContext::getTextWidth() to measure the width of some text and the GraphicsContext::getTextHeight() to measure the height of some text.

6.7.14.

How do I change the font that is used to render some text?

Call GraphicsContext::setCurrentFont() and pass in a pointer to a font instance. The data in your font instance is copied into the current font state of the GraphicsContext, so the font instance does not need to be valid once the call returns. For example:

GraphicsContext* ctx = //get a context from somewhere...
Font f;
f.setName( "Times New Roman" );
f.setPointSize( 34 );
ctx->setCurrentFont( &f );
				

Note that the font variable was created on the stack, because it's not needed once the setCurrentFont() function returns.

6.7.15.

How do I change the color of text?

Set the color of the current font.

GraphicsContext* ctx = //get a context from somewhere...
ctx->getCurrentFont()->setColor( Color::getColor("red") );
ctx->textAt( 20, 200, "Hello" );
				

   Comments or Suggestions?    License Information