How can I draw dotted line in flex

10,634

Solution 1

Well there is no Dashed or Dotted line in flex.

However, you can create your own line or border: http://cookbooks.adobe.com/post_Creating_a_dashed_line_custom_border_with_dash_and-13286.html

Try and enjoy!

Solution 2

You can't, well, not just by using Flex library classes anyway. You can do it yourself though, of course. Here's a class that implements it (modified from code found here, thanks Ken Fox):

/*Copyright (c) 2006 Adobe Systems Incorporated

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
package com.some.package {
  import mx.graphics.IStroke;
  import flash.display.Graphics;

  public class GraphicsUtils {
    public static function _drawDashedLine( target:Graphics, strokes:Array, pattern:Array,
                                            drawingState:DashStruct,
                                            x0:Number, y0:Number, x1:Number, y1:Number):void {
      var dX:Number = x1 - x0;
      var dY:Number = y1 - y0;
      var len:Number = Math.sqrt( dX * dX + dY * dY );
      dX /= len;
      dY /= len;
      var tMax:Number = len;
      var t:Number = -drawingState.offset;
      var patternIndex:int = drawingState.patternIndex;
      var strokeIndex:int = drawingState.strokeIndex;
      var styleInited:Boolean = drawingState.styleInited;

      while( t < tMax ) {
        t += pattern[patternIndex];
        if( t < 0 ) {
          var x:int = 5;
        }

        if( t >= tMax ) {
          drawingState.offset = pattern[patternIndex] - ( t - tMax );
          drawingState.patternIndex = patternIndex;
          drawingState.strokeIndex = strokeIndex;
          drawingState.styleInited = true;
          t = tMax;
        }

        if( styleInited == false ) {
          (strokes[strokeIndex] as IStroke).apply( target );
        }
        else {
          styleInited = false;
        }

        target.lineTo( x0 + t * dX, y0 + t * dY );

        patternIndex = ( patternIndex + 1 ) % pattern.length;
        strokeIndex = ( strokeIndex + 1 ) % strokes.length;
      }
    }

    public static function drawDashedLine( target:Graphics, strokes:Array, pattern:Array,
                                           x0:Number, y0:Number, x1:Number, y1:Number ):void {
      target.moveTo( x0, y0 );
      var struct:DashStruct = new DashStruct();
      _drawDashedLine( target, strokes, pattern, struct, x0, y0, x1, y1 );
    }

    public static function drawDashedPolyLine( target:Graphics, strokes:Array, pattern:Array,
                                               points:Array ):void {
      if( points.length == 0 ) { return; }
      var prev:Object = points[0];
      var struct:DashStruct = new DashStruct();

      target.moveTo( prev.x, prev.y );

      for( var i:int = 1; i < points.length; i++ ) {
        var current:Object = points[i];
        _drawDashedLine( target, strokes, pattern, struct,
                         prev.x, prev.y, current.x, current.y );
        prev = current;
      }
    }
  }
}

class DashStruct {
  public function init():void {
    patternIndex = 0;
    strokeIndex = 0;
    offset = 0;
  }
  public var patternIndex:int = 0;
  public var strokeIndex:int = 0;
  public var offset:Number = 0;
  public var styleInited:Boolean = false;
}

And you can use it like:

var points:Array = [];
points.push( { x: 0, y: 0 } );
points.push( { x: this.width, y: 0 } );
points.push( { x: this.width, y: this.height } );
points.push( { x: 0, y: this.height } );
points.push( { x: 0, y: 0 } );

var strokes:Array = [];
var transparentStroke:Stroke = new Stroke( 0x0, 0, 0 );
strokes.push( new Stroke( 0x999999, 2, 1, false, 'normal', CapsStyle.NONE ) );
strokes.push( transparentStroke );
strokes.push( new Stroke( 0x222222, 2, 1, false, 'normal', CapsStyle.NONE ) );
strokes.push( transparentStroke );

GraphicsUtils.drawDashedPolyLine( this.graphics, strokes, [ 16, 5, 16, 5 ], points );

Solution 3

I ran across this, today, which I like a lot. Provides for dashed/dotted lines and a few other things (curves, primarily).

http://flexdevtips.blogspot.com/2010/01/drawing-dashed-lines-and-cubic-curves.html

Share:
10,634
Yang Bo
Author by

Yang Bo

Updated on June 04, 2022

Comments

  • Yang Bo
    Yang Bo about 2 years

    After some search and reading the Graphics class document, I can't find a way to specify the line style of a line. I mean whether the line is a solid one or a dotted one. Could anybody help me?

    Thanks!