首页 > 代码库 > StrokesAndFills
StrokesAndFills
1 //StrokesAndFills.cpp 2 3 /* 4 Copyright 2000-2004 The VCF Project. 5 Please see License.txt in the top level directory 6 where you installed the VCF. 7 */ 8 9 10 #include "vcf/ApplicationKit/ApplicationKit.h" 11 #include "vcf/ApplicationKit/ControlsKit.h" 12 13 14 15 using namespace VCF; 16 17 18 /** 19 This example will demonstrate the use of the 20 Fill and Stroke classes and the new AGG anti-aliased 21 vector library support. 22 */ 23 24 class StrokesAndFillsWindow : public Window { 25 public: 26 StrokesAndFillsWindow() { 27 setCaption( "StrokesAndFills" ); 28 29 /** 30 Create a checkbox button, and add an event handler 31 when the button is clicked 32 */ 33 CheckBoxControl* antiAliasToggle = new CheckBoxControl(); 34 antiAliasToggle->setCaption( "Anti-aliased" ); 35 antiAliasToggle->setBounds( 20, 20, 100, antiAliasToggle->getPreferredHeight() ); 36 add( antiAliasToggle ); 37 38 antiAliasToggle->ButtonClicked += new ClassProcedure1<ButtonEvent*,StrokesAndFillsWindow>( this, &StrokesAndFillsWindow::onButtonClicked, "StrokesAndFillsWindow::onButtonClicked" ); 39 } 40 41 virtual ~StrokesAndFillsWindow(){}; 42 43 44 /** 45 This event handler will toggle whether or not hte render 46 buffer is enabled. If the render buffer is enabled 47 (isUsingRenderBuffer() returns true) then anti-aliased 48 drawing is possible for the control, otherwise no 49 anti-aliased drawing will take place. 50 */ 51 void onButtonClicked( ButtonEvent* e ) { 52 CheckBoxControl* antiAliasToggle = (CheckBoxControl*)e->getSource(); 53 54 antiAliasToggle->setChecked( !isUsingRenderBuffer() ); 55 56 /** 57 calling this method sets the use of an render buffer 58 to support the anti-aliasing features of AGG. Each control 59 can control whether or not it support this, and by 60 default controls start up *not* using the render 61 buffer. 62 */ 63 setUsingRenderBuffer( antiAliasToggle->isChecked() ); 64 65 66 if ( isUsingRenderBuffer() ) { 67 //GraphicsContext* ctx = getContext(); 68 //ctx->setRenderArea( getClientBounds() ); 69 } 70 71 repaint(); 72 } 73 74 75 virtual void paint( GraphicsContext* ctx ) { 76 Window::paint( ctx ); 77 78 /** 79 This is a path that supports lines, and curves. 80 You call the moveTo, lineTo, curveTo, etc methods 81 to add points to the path. 82 */ 83 BezierCurve shape; 84 85 /** 86 This is a stroke object. You can use it 87 to draw the shape of your path. You 88 set properties of the stroke object 89 such as the width, color, etc. 90 */ 91 BasicStroke stroke; 92 93 /** 94 This sets the current stroke to use. This 95 pointer is *not* owned by the GraphicsContext 96 so be sure to set it to NULL (or 97 the previous value) when you‘re done 98 */ 99 ctx->setCurrentStroke( &stroke );100 101 /**102 Add a dash pattern to the stroke.103 */104 stroke.addDash( 10.0, 5.0 );105 stroke.addDash( 3.0, 5.0 );106 107 /** 108 Start dashing pattern at 15.0 (10.0 + 5.0), so the first part109 of line displayed will correspond to the short dashed segment110 specified above (3.0). 111 */112 stroke.dashStart( 15.0 );113 114 /**115 Add two points to the path116 */117 shape.moveTo( 100, 100 );118 shape.lineTo( 300, 300 );119 shape.lineTo( 500, 300 );120 121 /**122 Draw the path. The GraphicsContext will use123 it‘s current Stroke and Fill pointers to stroke124 and fill the path. Since we have not specified a125 Fill to use, the path will only be stroked126 */127 ctx->draw( &shape );128 129 /**130 Remove dash from stroke, so any use of stroke131 below will NOT be dashed.132 */133 stroke.removeDashes();134 135 136 /**137 Adjust the GraphicsContext‘s matrix138 to offset by 0 pixels to the left, and 50 pixel down139 This will transform any paths that are drawn but will140 *not* directly modify the path‘s points141 */142 ctx->setTranslation( 0, 50 );143 144 /**145 set the stroke‘s color to green and the146 width to 3147 */148 stroke.setColor( &Color(0.0,1.0,0.0) );149 stroke.setWidth( 3.0 );150 151 ctx->draw( &shape );152 153 /*154 reset the translation155 */156 ctx->setTranslation( 0, 0 );157 158 /**159 create a curved shape .160 the curve() method takes 4 points, a161 starting point, the first control point, the second162 control point, and the end point.163 */164 BezierCurve shape2;165 166 /**167 This will overlap the previous shapes a bit168 so we can see the effects of setting the169 stroke‘s opacity170 */171 shape2.curve( 50, 300,172 100, 250,173 300, 250,174 375, 300 );175 176 177 Color color(1.0,0.0,0.75);178 color.setAlpha( 0.75 );179 stroke.setColor( &color );180 stroke.setWidth( 15.0 );181 /**182 Note we set the stroke to be 75% opaque183 so we‘ll be able to partially see what ever is184 "underneath" this shape. However this will185 only take effect if the anti-aliasing support186 is enabled by calling setUsingRenderBuffer() with187 a true value passed in.188 */189 190 ctx->draw( &shape2 );191 192 193 BezierCurve rect;194 195 rect.rectangle( Rect(75,350,175,450) );196 197 ctx->draw( &rect );198 199 BasicFill fill;200 color = *Color::getColor("yellow");201 color.setAlpha( 0.65 );202 203 fill.setColor( &color );204 205 ctx->setCurrentFill( &fill );206 207 /**208 We‘re going to directly modify the points of our209 path by using a series to matrices to transform210 the shape by first rotating 45 degrees and then211 offsetting by 50 pixels212 */213 Matrix2D translate;214 /**215 tranlation matrix to move the rect shape back to216 0,0 centered around it‘s center.217 Our rect‘s left/top coordinate was at 75,350218 and it‘s width/height was 100,100 so to center219 it around the origin (0,0) we need to220 translate it by -125,-400221 */222 translate.translate( -125, -400 );223 224 Matrix2D rotate;225 rotate.rotate( 45 );226 227 Matrix2D mat;228 229 mat.multiply( translate );230 mat.multiply( rotate );231 232 /**233 apply the rotation/translation matrix to the shape234 */235 rect.applyTransform( mat );236 237 translate.translate( 125, 400 );238 239 /**240 transalte the shape back to it‘s original position241 only now‘s been rotated 45 degrees242 */243 rect.applyTransform( translate );244 245 translate.translate( 0, 50 );246 //move it down 50 pixels247 rect.applyTransform( translate );248 249 Color ltBlue(0.0,0.0,1.0);250 //change the color by manipulating the HSV components251 ltBlue.setHSV( 0.23, 0.0, 0.0 );252 ltBlue.setAlpha( 0.45 );253 254 stroke.setColor( <Blue );255 stroke.setWidth( 4.0 );256 257 ctx->draw( &rect );258 259 260 ctx->setCurrentFill( NULL );261 ctx->setCurrentStroke( NULL );262 }263 };264 265 266 267 268 class StrokesAndFillsApplication : public Application {269 public:270 271 StrokesAndFillsApplication(int argc, char** argv) : Application(argc,argv){272 273 }274 275 virtual bool initRunningApplication(){276 bool result = Application::initRunningApplication();277 278 Window* mainWindow = new StrokesAndFillsWindow();279 setMainWindow(mainWindow);280 mainWindow->setBounds( 100.0, 100.0, 500.0, 500.0 );281 mainWindow->show();282 283 return result;284 }285 286 };287 288 289 int main(int argc, char *argv[])290 {291 Application* app = new StrokesAndFillsApplication( argc, argv );292 293 Application::main();294 295 return 0;296 }297 298 299 /**300 $Id: StrokesAndFills.cpp 3424 2008-04-02 18:23:15Z ddiego $301 */
StrokesAndFills
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。