如何使用鼠标绘制和调整多边形大小

时间:2014-03-03 07:43:42

标签: flex

在我的网络应用程序中,我需要使用鼠标在图像中绘制形状。但是我被绘制并调整了多边形..我有一个明显的多边形..如何调整大小?

我可以使用上面的代码绘制多边形。 但我需要调整它的大小。任何想法都要调整淹没的多边形?

请参阅以下代码。

private function onDrawTriangle() : void {
    var ui:UIComponent = new UIComponent();
    drawPanel = new Sprite();
    drawPanel.graphics.clear();
    drawPanel.graphics.lineStyle(2, 0xFF0000);
    drawPanel.graphics.beginFill(0xDEFACE);
    drawPanel.graphics.drawRect(0,0,300,300);
    drawPanel.graphics.endFill();
    ui.addChild(drawPanel);
    addChild(ui);
    ui.x = 20;
    ui.y = 20;

    _lineArr = new Array();
    line = new Shape();
    drawPanel.addEventListener(MouseEvent.CLICK,onAddPoint);
}

private function onAddPoint(evt:MouseEvent) : void {
    if(numSpot <= 10) {
        var point:Sprite = new Sprite();
        point.graphics.clear();
        point.graphics.lineStyle(0,0x0000FF);
        point.graphics.beginFill(0x0000FF);
        point.graphics.drawCircle(0,0,5);
        point.graphics.endFill();

        point.x = evt.localX;
        point.y = evt.localY;
        _lineArr.push({x:point.x,y:point.y});
        drawPanel.addChild(point);
        if(numSpot > 0) drawLine();

        numSpot++;
    }
}

private function drawLine() : void {
    line.graphics.clear();
    line.graphics.lineStyle(2,0x000000);
    if(_lineArr.length > 2) line.graphics.beginFill(0xFF0000,0.5);
    line.graphics.moveTo(_lineArr[0].x,_lineArr[0].y);
    for(var i:int=1;i<_lineArr.length;i++) {
        line.graphics.lineTo(_lineArr[i].x,_lineArr[i].y);
    }
    line.graphics.lineTo(_lineArr[0].x,_lineArr[0].y);
    if(_lineArr.length > 2) line.graphics.endFill();
    drawPanel.addChildAt(line,0);
}

1 个答案:

答案 0 :(得分:0)

而是将点推到数组,你可以推动点(精灵变量)。

查看上面代码的修改代码,其中我添加了一些事件来重新调整多边形的大小

以下是示例代码:

private function onDrawTriangle() : void {
                var ui:SpriteVisualElement = new SpriteVisualElement();
                //ui.x = 20;
                //ui.y = 20;
                addElement(ui);
                drawPanel = new Sprite();
                drawPanel.graphics.clear();
                drawPanel.graphics.lineStyle(2, 0xFF0000);
                drawPanel.graphics.beginFill(0xDEFACE);
                drawPanel.graphics.drawRect(0,0,300,300);
                drawPanel.graphics.endFill();
                ui.addChild(drawPanel);

                _lineArr = new Array();
                line = new Shape();
                drawPanel.addEventListener(MouseEvent.CLICK,onAddPoint);
            }

            private function onAddPoint(evt:MouseEvent) : void {
                if(numSpot <= 10) {
                    var point:Sprite = new Sprite();
                    point.graphics.clear();
                    point.graphics.lineStyle(0,0x0000FF);
                    point.graphics.beginFill(0x0000FF);
                    point.graphics.drawCircle(0,0,5);
                    point.graphics.endFill();

                    point.x = mouseX;
                    point.y = mouseY;

                    point.addEventListener(MouseEvent.MOUSE_DOWN , onDown);
                    _lineArr.push(point);
                    drawPanel.addChild(point);
                    if(numSpot > 0) drawLine();

                    numSpot++;
                }
            }

            private function drawLine() : void {
                line.graphics.clear();
                line.graphics.lineStyle(2,0x000000);
                if(_lineArr.length > 2)     line.graphics.beginFill(0xFF0000,0.5);
                line.graphics.moveTo(_lineArr[0].x,_lineArr[0].y);
                for(var i:int=1;i<_lineArr.length;i++) {
                    line.graphics.lineTo(_lineArr[i].x,_lineArr[i].y);
                }
                line.graphics.lineTo(_lineArr[0].x,_lineArr[0].y);
                if(_lineArr.length > 2) line.graphics.endFill();
                drawPanel.addChildAt(line,0);
            }

            protected function onDown(event:MouseEvent):void
            {
                var point:Sprite = event.currentTarget as Sprite;
                point.addEventListener(Event.ENTER_FRAME , onResize);
                point.addEventListener(MouseEvent.MOUSE_UP , onUp);

                drawPanel.removeEventListener(MouseEvent.CLICK,onAddPoint);

            }

            protected function onResize(event:Event):void
            {
                var point:Sprite = event.currentTarget as Sprite;
                point.x = mouseX;
                point.y = mouseY;
                drawLine();
            }

            protected function onUp(event:MouseEvent):void
            {
                var point:Sprite = event.currentTarget as Sprite;
                point.removeEventListener(Event.ENTER_FRAME , onResize);
                point.removeEventListener(MouseEvent.MOUSE_UP , onUp);

                drawPanel.addEventListener(MouseEvent.CLICK,onAddPoint);
            }