Flash / Flex / ActionScript/Graphics/graphics
Содержание
- 1 Adding a Simple One-Color Fill
- 2 Change line style
- 3 Change scaleX
- 4 Creating Fills
- 5 Draw four shapes
- 6 Drawing a Curve
- 7 Drawing a Straight Line
- 8 Drawing Circles
- 9 Drawing Lines in a Graphics Object
- 10 Drawing Rounded Rectangles
- 11 Draw lines
- 12 Draw three shapes
- 13 Introducing the Graphics Object
- 14 Moving the Pen without Drawing
- 15 Set Shape position
- 16 To move the drawing pen without drawing any line at all, use moveTo( ).
- 17 To remove all vector drawings in an object, we use the Graphics class"s instance method clear( ).
- 18 Use the graphics
Adding a Simple One-Color Fill
<source lang="java">
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); } }
}
</source>
Change line style
<source lang="java">
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); } }
}
</source>
Change scaleX
<source lang="java">
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); } }
}
</source>
Creating Fills
<source lang="java">
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); } }
}
</source>
Draw four shapes
<source lang="java">
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); } }
}
</source>
Drawing a Curve
<source lang="java">
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); } }
}
</source>
Drawing a Straight Line
<source lang="java">
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); } }
}
</source>
Drawing Circles
<source lang="java">
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(); } }
}
</source>
Drawing Lines in a Graphics Object
<source lang="java">
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); } }
}
</source>
Drawing Rounded Rectangles
<source lang="java">
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(); } }
}
</source>
Draw lines
<source lang="java">
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); } }
}
</source>
Draw three shapes
<source lang="java">
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); } }
}
</source>
Introducing the Graphics Object
<source lang="java">
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); } }
}
</source>
Moving the Pen without Drawing
<source lang="java">
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); } }
}
</source>
Set Shape position
<source lang="java">
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); } }
}
</source>
To move the drawing pen without drawing any line at all, use moveTo( ).
<source lang="java">
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); } }
}
</source>
To remove all vector drawings in an object, we use the Graphics class"s instance method clear( ).
<source lang="java">
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( ); } }
}
</source>
Use the graphics
<source lang="java">
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); } }
}
</source>