Les méthodes de dessin de la classe Graphics

Créer des dessins vectoriels dans Flash en ActionScript 3 (AS3)

Nous allons étudier les différentes méthodes de la classe Graphics afin de créer en AS3, des rectangles, des carrés, des ellipses. On affectera à ces dessins, un remplissage ou un contour.

N'hésitez par à relire l'article sur les classes Shape et Graphics.

Pour créer un rectangle, nous utilisons la méthode drawRect().
Pour créer un rectangle aux coins arrondis, nous utilisons la méthode drawRoundRect().
Pour créer un cercle, nous utilisons la méthode drawCircle().
Pour créer une ellipse, nous utilisons la méthode drawEllipse().

Dessins avec un remplissage

Le remplissage se fait avec ma méthode beginFill().

Actionscript:
  1. // rectangle bleu
  2. var rectangle:Shape = new Shape ();
  3. rectangle.graphics.beginFill(0x0000FF);
  4. rectangle.graphics.drawRect(5, 5, 100, 30);
  5. this.addChild(rectangle);
  6. // rectangle bleu aux coins arrondis
  7. var rectangleRond:Shape = new Shape ();
  8. rectangleRond.graphics.beginFill(0x0000FF);
  9. rectangleRond.graphics.drawRoundRect(5, 40, 100, 30, 30);
  10. this.addChild(rectangleRond);
  11. // cercle bleu
  12. var cercle:Shape = new Shape ();
  13. cercle.graphics.beginFill(0x0000FF);
  14. cercle.graphics.drawCircle(55, 100, 25);
  15. this.addChild(cercle);
  16. // ellipse bleue
  17. var ellipse:Shape = new Shape ();
  18. ellipse.graphics.beginFill(0x0000FF);
  19. ellipse.graphics.drawEllipse(5, 130, 100, 30);
  20. this.addChild(ellipse);
  21. // carre bleu
  22. var carre:Shape = new Shape ();
  23. carre.graphics.beginFill(0x0000FF);
  24. carre.graphics.drawRect(30, 165, 50, 50);
  25. this.addChild(carre);

Dessins avec un remplissage et un contour

Le remplissage se fait avec ma méthode beginFill() et le contour avec la méthode lineStyle().

Actionscript:
  1. // rectangle jaune avec un contour rouge
  2. var rectangleContour:Shape = new Shape ();
  3. rectangleContour.graphics.beginFill(0xFFFF00);
  4. rectangleContour.graphics.lineStyle(2,0XFF0000);
  5. rectangleContour.graphics.drawRect(150, 5, 100, 30);
  6. this.addChild(rectangleContour);
  7. // rectangle jaune aux coins arrondis avec un contour rouge
  8. var rectangleRondContour:Shape = new Shape ();
  9. rectangleRondContour.graphics.beginFill(0xFFFF00);
  10. rectangleRondContour.graphics.lineStyle(2,0XFF0000);
  11. rectangleRondContour.graphics.drawRoundRect(150, 40, 100, 30, 30);
  12. this.addChild(rectangleRondContour);
  13. // cercle jaune avec un contour rouge
  14. var cercleContour:Shape = new Shape ();
  15. cercleContour.graphics.beginFill(0xFFFF00);
  16. cercleContour.graphics.lineStyle(2,0XFF0000);
  17. cercleContour.graphics.drawCircle(205, 100, 25);
  18. this.addChild(cercleContour);
  19. // ellipse jaune avec un contour rouge
  20. var ellipseContour:Shape = new Shape ();
  21. ellipseContour.graphics.beginFill(0xFFFF00);
  22. ellipseContour.graphics.lineStyle(2,0XFF0000);
  23. ellipseContour.graphics.drawEllipse(150, 130, 100, 30);
  24. this.addChild(ellipseContour);
  25. // carre jaune avec un contour rouge
  26. var carreContour:Shape = new Shape ();
  27. carreContour.graphics.beginFill(0xFFFF00);
  28. carreContour.graphics.lineStyle(2,0XFF0000);
  29. carreContour.graphics.drawRect(175, 165, 50, 50);
  30. this.addChild(carreContour);

Dessins avec un contour
Actionscript:
  1. // rectangle avec un contour vert
  2. var rectangleContourVert:Shape = new Shape ();
  3. rectangleContourVert.graphics.lineStyle(2,0x00FF00);
  4. rectangleContourVert.graphics.drawRect(300, 5, 100, 30);
  5. this.addChild(rectangleContourVert);
  6. // rectangle aux coins arrondis avec un contour vert
  7. var rectangleRondContourVert:Shape = new Shape ();
  8. rectangleRondContourVert.graphics.lineStyle(2,0x00FF00);
  9. rectangleRondContourVert.graphics.drawRoundRect(300, 40, 100, 30, 30);
  10. this.addChild(rectangleRondContourVert);
  11. // cercle avec un contour vert
  12. var cercleContourVert:Shape = new Shape ();
  13. cercleContourVert.graphics.lineStyle(2,0x00FF00);
  14. cercleContourVert.graphics.drawCircle(355, 100, 25);
  15. this.addChild(cercleContourVert);
  16. // ellipse avec un contour vert
  17. var ellipseContourVert:Shape = new Shape ();
  18. ellipseContourVert.graphics.lineStyle(2,0x00FF00);
  19. ellipseContourVert.graphics.drawEllipse(300, 130, 100, 30);
  20. this.addChild(ellipseContourVert);
  21. // carre avec un contour vert
  22. var carreContourVert:Shape = new Shape ();
  23. carreContourVert.graphics.lineStyle(2,0x00FF00);
  24. carreContourVert.graphics.drawRect(325, 165, 50, 50);
  25. this.addChild(carreContourVert);

;-)