Flash / Flex / ActionScript/Graphics/graphics

Материал из Web эксперт
Перейти к: навигация, поиск

Adding a Simple One-Color Fill

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var shape:Shape = new Shape ();
        shape.graphics.lineStyle(0, 0xFF0000);
        shape.graphics.beginFill(0xFFFF00,100);
        
        shape.graphics.lineTo(100, 0);
        shape.graphics.lineTo(100, 100);
        shape.graphics.lineTo(0, 100);
        shape.graphics.lineTo(0, 0);
        shape.graphics.endFill();
        addChild(shape);
    }
  }
}



Change line style

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var shape:Shape = new Shape();
        shape.graphics.lineStyle(0, 0xFF0000);
        shape.graphics.lineTo(100, 0);
        shape.graphics.lineStyle(0,0x00FF00);
        shape.graphics.lineTo(100, 100);
        addChild(shape);
    }
  }
}



Change scaleX

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var rectangleA:Shape = new Shape();
        rectangleA.graphics.lineStyle(1, 0, 100);
        rectangleA.graphics.lineTo(100, 0);
        rectangleA.graphics.lineTo(100, 50);
        rectangleA.graphics.lineTo(0, 50);
        rectangleA.graphics.lineTo(0, 0);
        rectangleA.scaleX = 4;
        addChild(rectangleA);
    }
  }
}



Creating Fills

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
      var spr:Sprite = new Sprite();
      spr.graphics.beginFill(0xff00ff, 0.5);//our fill will be yellow
      spr.graphics.drawCircle(0, 0, 50);//fill a circle with our fill
      spr.graphics.endFill();//we"re done with this graphic
      addChild(spr);
    }
  }
}



Draw four shapes

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var linesA:Shape = new Shape();
        var linesB:Shape = new Shape();
        var linesC:Shape = new Shape();
        var linesD:Shape = new Shape();
        
        addChild(linesA);
        addChild(linesB);
        addChild(linesC);
        addChild(linesD);
        
        linesA.x = 100;
        linesA.y = 100;
        linesA.graphics.lineStyle(20, 0, 100, false, "none", "none", "round");
        linesA.graphics.lineTo(100, 0);
        linesA.graphics.lineTo(0, 50);
        
        linesB.x = 250;
        linesB.y = 100;
        linesB.graphics.lineStyle(20, 0, 100, false, "none", "none", "none");
        linesB.graphics.lineTo(100, 0);
        linesB.graphics.lineTo(0, 50);
        
        linesC.x = 100;
        linesC.y = 200;
        linesC.graphics.lineStyle(20, 0, 100, false, "none", "none", "miter");
        linesC.graphics.lineTo(100, 0);
        linesC.graphics.lineTo(0, 50);
        
        linesD.x = 250;
        linesD.y = 200;
        linesD.graphics.lineStyle(20, 0, 100, false, "none", "none", "miter", 10);
        linesD.graphics.lineTo(100, 0);
        linesD.graphics.lineTo(0, 50);
    }
  }
}



Drawing a Curve

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var shape:Shape = new Shape ();
        shape.graphics.lineStyle(0, 0xFF0000, 100);
        shape.graphics.curveTo(50, 100, 100, 0);
        addChild(shape);
    }
  }
}



Drawing a Straight Line

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var shape:Shape = new Shape ();
        shape.graphics.lineStyle(0, 0xFF0000);
        shape.graphics.lineTo(100, 0);
        shape.graphics.lineTo(100, 100);
        shape.graphics.lineStyle(0, 0xFF0000);
        shape.graphics.moveTo(100, 100);
        shape.graphics.lineTo(150, 100);
        shape.graphics.moveTo(200, 100);
        shape.graphics.lineTo(250, 100);
        shape.graphics.moveTo(125, 200);
        shape.graphics.lineTo(225, 200);
        addChild(shape);
    }
  }
}



Drawing Circles

 
package{
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    public class Main extends Sprite
    {
        private var Maib:Shape;
        public function ShapeTest()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            var shape:Shape = new Shape();
            addChild(shape);
            shape.x = 100;
            shape.y = 100;
            shape.graphics.beginFill(0x000000, 1);
            shape.graphics.drawCircle(0, 0, 100);
            shape.graphics.endFill();
        }
    }
}



Drawing Lines in a Graphics Object

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
      var spr:Sprite = new Sprite();
      spr.graphics.lineStyle(1, 0x0000ff);
      spr.graphics.moveTo(30, 30);
      spr.graphics.lineTo(100, 30);
    
      spr.graphics.lineTo(100, 100);
      spr.graphics.lineTo(30, 100);
      spr.graphics.lineTo(30, 30);
      spr.graphics.endFill();
    
      addChild(spr);
    }
  }
}



Drawing Rounded Rectangles

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var shape:Shape = new Shape();
        addChild(shape);
        shape.graphics.beginFill(0x000000, 1);
        shape.graphics.drawRoundRect(0, 0, 100, 100, 10, 20);
        shape.graphics.endFill();
    }
  }
}



Draw lines

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var rectangleA:Sprite = new Sprite();
        rectangleA.graphics.lineStyle(1, 0x000000, 100, false);
        rectangleA.graphics.lineTo(100, 0.4);
        rectangleA.graphics.lineTo(100.4, 50);
        rectangleA.graphics.lineTo(0, 50.4);
        rectangleA.graphics.lineTo(0.4, 0)
        addChild(rectangleA);
    }
  }
}



Draw three shapes

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var lineA:Shape = new Shape();
        var lineB:Shape = new Shape();
        var lineC:Shape = new Shape();
        
        addChild(lineA);
        addChild(lineB);
        addChild(lineC);
        
        lineA.x = 100;
        lineA.y = 100;
        lineA.graphics.lineStyle(20, 0, 1, false, "none", "round");
        lineA.graphics.lineTo(100, 0);
        
        lineB.x = 100;
        lineB.y = 150;
        lineB.graphics.lineStyle(20, 0, 1, false, "none", "square");
        lineB.graphics.lineTo(100, 0);
        
        lineC.x = 100;
        lineC.y = 200;
        lineC.graphics.lineStyle(20, 0, 1, false, "none", "none");
        lineC.graphics.lineTo(100, 0);
    }
  }
}



Introducing the Graphics Object

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
      createSprite();
    }
    public function createSprite(){
      var spr:Sprite = new Sprite();
      spr.graphics.beginFill(0x00ff00, 1.0);
      spr.graphics.drawRect(0, 0, 50, 50);
      spr.graphics.endFill();
      addChild(spr);
    }
  }
}



Moving the Pen without Drawing

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var shape:Shape = new Shape();
        shape.graphics.lineStyle(0, 0xFF0000);
        shape.graphics.moveTo(100, 100);
        addChild(shape);
    }
  }
}



Set Shape position

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var rectangleB:Shape = new Shape();
        rectangleB.graphics.lineStyle(1, 0, 100, false, "none");
        rectangleB.graphics.lineTo(100, 0);
        rectangleB.graphics.lineTo(100, 50);
        rectangleB.graphics.lineTo(0, 50);
        rectangleB.graphics.lineTo(0, 0);
        rectangleB.y = 1;
        rectangleB.scaleX = 400;
        addChild(rectangleB);
    }
  }
}



To move the drawing pen without drawing any line at all, use moveTo( ).

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var canvas:Shape = new Shape(  );   // Create the Shape to draw in
        canvas.graphics.lineStyle(1);     // Set the stroke to 1 point, black
        canvas.graphics.moveTo(100, 100); // Move the pen without drawing a line
        canvas.graphics.lineTo(200, 200); // Draw the line (this also moves the pen)
        addChild(canvas);
    }
  }
}



To remove all vector drawings in an object, we use the Graphics class"s instance method clear( ).

 
package{
  import flash.display.*;
  
  public class Main extends Sprite{
    public function Main(){
        var canvas:Shape = new Shape(  );
        canvas.graphics.lineStyle(3, 0x0000FF);  // Apply blue stroke
        canvas.graphics.lineTo(25, 35);
        addChild(canvas);
        
        canvas.graphics.clear(  );
    }
  }
}



Use the graphics

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var rectangleB:Sprite = new Sprite();
        rectangleB.graphics.lineStyle(1, 0x000000, 100, true);
        rectangleB.graphics.lineTo(100, 0.4);
        rectangleB.graphics.lineTo(100.4, 50);
        rectangleB.graphics.lineTo(0, 50.4);
        rectangleB.graphics.lineTo(0.4, 0);
        rectangleB.x = 200;
        addChild(rectangleB);
    }
  }
}