Draw a rectangle : Rectangle « Graphics « Flash / Flex / ActionScript






Draw a rectangle

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){


        try {
          drawRectangle(this, 100, 200);
          drawRectangle(this, 300, 400);
        }
        catch(errObject:Error) {
          this.graphics.clear(  );
          trace("An error occurred: " + errObject.message);
        }
    }
    private function drawRectangle(sprite:Sprite, nWidth:Number, nHeight:Number):void {
      if(isNaN(nWidth) || isNaN(nHeight)) {
        throw new Error("Invalid dimensions specified.");
      }
      sprite.graphics.lineStyle(1, 0, 1);
      sprite.graphics.lineTo(nWidth, 0);
      sprite.graphics.lineTo(nWidth, nHeight);
      sprite.graphics.lineTo(0, nHeight);
      sprite.graphics.lineTo(0, 0);
    }
  }
}

        








Related examples in the same category

1.Draw Colored Rectangle
2.Fill a rectangle
3.Rotating Rectangles
4.rotateChildren( ), applies the generalized code from Example 20-4. It randomly rotates all the descendants of a specified container (not just the children).
5.A rectangle in a Sprite is nested within another Sprite. Both Sprite instances are rotated 45 degrees.
6.Square