패스 그리기

Flash Player 10 이상, Adobe AIR 1.5 이상

선 및 곡선 그리기에 대한 단원(선 및 곡선 그리기 참조)에서는 단일 선(Graphics.lineTo()) 또는 곡선(Graphics.curveTo())을 그린 다음 해당 선을 다른 지점(Graphics.moveTo())으로 이동하여 모양을 형성하기 위한 명령을 소개했습니다. Graphics.drawPath() 및 Graphics.drawTriangles()와 같은 일부 ActionScript 드로잉 API 향상 기능에서는 기존 드로잉 명령이 매개 변수로 사용됩니다. 따라서 Flash 런타임에 일련의 Graphics.lineTo(), Graphics.curveTo() 또는 Graphics.moveTo() 명령을 제공하여 단일 명령문에서 실행할 수 있습니다.

드로잉 API의 두 가지 향상된 기능을 통해 Graphics.drawPath() 및 Graphics.drawTriangles()를 기존 명령으로 통합할 수 있습니다.

  • GraphicsPathCommand 열거형 클래스: GraphicsPathCommand 클래스는 여러 드로잉 명령을 상수 값과 연결합니다. 이러한 일련의 값은 Graphics.drawPath() 메서드의 매개 변수로 사용합니다. 그러면 단일 명령을 사용하여 모양 전체 또는 일부 모양을 렌더링할 수 있습니다. 이러한 메서드에 전달되는 값을 동적으로 변경하여 기존 모양을 변경할 수도 있습니다.

  • 벡터 배열: 벡터 배열은 특정 데이터 유형의 값을 여러 개 포함합니다. 따라서 일련의 GraphicsPathCommand 상수를 하나의 Vector 객체에 저장하고 일련의 좌표는 다른 Vector 객체에 저장합니다. Graphics.drawPath() 또는 Graphics.drawTriangles()는 이러한 값을 함께 지정하여 드로잉 패스 또는 모양을 생성합니다.

이제 모양의 각 선분에 대해 개별적으로 명령을 실행할 필요가 없습니다. 예를 들어 Graphics.drawPath() 메서드는 Graphics.moveTo(), Graphics.lineTo() 및 Graphics.curveTo()를 단일 메서드로 통합합니다. 각 메서드는 개별적으로 호출되는 대신 GraphicsPathCommand 클래스에 정의된 숫자 식별자로 추상화됩니다. moveTo() 작업은 1로 나타내고 lineTo() 작업은 2로 나타냅니다. 이러한 값의 배열을 Vector.<int> 객체에 저장하여 commands 매개 변수에 사용할 수 있습니다. 그런 다음 data 매개 변수에 사용할 수 있도록 Vector.<Number> 객체에 좌표가 들어 있는 다른 배열을 만듭니다. 각 GraphicsPathCommand 값은 data 매개 변수에 저장된 좌표 값에 해당합니다. 연속되는 두 개의 숫자는 대상 좌표 공간에서의 위치를 정의합니다.

참고: 벡터의 값은 Point 객체가 아니고, 벡터는 일련의 숫자이며 두 개의 숫자로 된 각 그룹이 x/y 좌표 쌍을 나타냅니다.

Graphics.drawPath() 메서드는 각 명령을 해당 점 값(두 개 또는 네 개의 숫자로 된 컬렉션)과 대응시켜 Graphics 객체에 패스를 생성합니다.

package{ 
import flash.display.*; 
 
public class DrawPathExample extends Sprite { 
    public function DrawPathExample(){ 
 
    var square_commands:Vector.<int> = new Vector.<int>(5,true); 
    square_commands[0] = 1;//moveTo 
    square_commands[1] = 2;//lineTo 
    square_commands[2] = 2; 
    square_commands[3] = 2; 
    square_commands[4] = 2; 
 
    var square_coord:Vector.<Number> = new Vector.<Number>(10,true); 
    square_coord[0] = 20; //x 
    square_coord[1] = 10; //y 
    square_coord[2] = 50; 
    square_coord[3] = 10; 
    square_coord[4] = 50; 
    square_coord[5] = 40; 
    square_coord[6] = 20; 
    square_coord[7] = 40; 
    square_coord[8] = 20; 
    square_coord[9] = 10; 
 
    graphics.beginFill(0x442266);//set the color 
    graphics.drawPath(square_commands, square_coord); 
    } 
} 
}

위 예제에서는 각 명령과 좌표 쌍이 배열 내의 해당 위치를 나타내도록 개별적으로 할당되지만 단일 명령문에 할당될 수도 있습니다. 다음 예제는 단일 push() 문에 각 배열에 대한 값을 할당하여 동일한 별을 그립니다.

package{ 
import flash.display.*; 
 
public class DrawPathExample extends Sprite { 
    public function DrawPathExample(){ 
 
    var square_commands:Vector.<int> = new Vector.<int>(); 
    square_commands.push(1, 2, 2, 2, 2); 
    var square_coord:Vector.<Number> = new Vector.<Number>(); 
    square_coord.push(20,10, 50,10, 50,40, 20,40, 20,10); 
 
    graphics.beginFill(0x442266); 
    graphics.drawPath(square_commands, square_coord); 
    } 
} 
}