Creates a new Stroke-Object for the given type and with. : Shape « 2D Graphics GUI « Java






Creates a new Stroke-Object for the given type and with.

    
/**
 * 
 * JFreeReport : a free Java reporting library
 * 
 *
 * Project Info:  http://reporting.pentaho.org/
 *
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * ------------
 * StrokeUtility.java
 * ------------
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 */
import java.awt.BasicStroke;
import java.awt.Stroke;
import java.util.Arrays;

/**
 * This class provides helper methods to work with Strokes and line-styles.
 * 
 * @author Thomas Morgner
 */
public class StrokeUtility {
  /** A constant defining a stroke-type. */
  public static final int STROKE_SOLID = 0;

  /** A constant defining a stroke-type. */
  public static final int STROKE_DASHED = 1;

  /** A constant defining a stroke-type. */
  public static final int STROKE_DOTTED = 2;

  /** A constant defining a stroke-type. */
  public static final int STROKE_DOT_DASH = 3;

  /** A constant defining a stroke-type. */
  public static final int STROKE_DOT_DOT_DASH = 4;

  /** A constant defining a stroke-type. */
  public static final int STROKE_NONE = 5;

  /**
   * Default Constructor. Private to prevent Object-creation.
   */
  private StrokeUtility() {
  }

  /**
   * Creates a new Stroke-Object for the given type and with.
   * 
   * @param type
   *          the stroke-type. (Must be one of the constants defined in this
   *          class.)
   * @param width
   *          the stroke's width.
   * @return the stroke, never null.
   */
  public static Stroke createStroke(final int type, final float width) {

    final boolean useWidthForStrokes = true;

    final float effectiveWidth;
    if (useWidthForStrokes) {
      effectiveWidth = width;
    } else {
      effectiveWidth = 1;
    }

    switch (type) {
    case STROKE_DASHED:
      return new BasicStroke(width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f,
          new float[] { 6 * effectiveWidth, 6 * effectiveWidth }, 0.0f);
    case STROKE_DOTTED:
      return new BasicStroke(width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 5.0f,
          new float[] { 0.0f, 2 * effectiveWidth }, 0.0f);
    case STROKE_DOT_DASH:
      return new BasicStroke(width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f,
          new float[] { 0, 2 * effectiveWidth, 6 * effectiveWidth, 2 * effectiveWidth }, 0.0f);
    case STROKE_DOT_DOT_DASH:
      return new BasicStroke(width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f,
          new float[] { 0, 2 * effectiveWidth, 0, 2 * effectiveWidth, 6 * effectiveWidth,
              2 * effectiveWidth }, 0.0f);
    default:
      return new BasicStroke(width);
    }
  }

  /**
   * Tries to extract the stroke-width from the given stroke object.
   * 
   * @param s
   *          the stroke.
   * @return the stroke's width.
   */
  public static float getStrokeWidth(final Stroke s) {
    if (s instanceof BasicStroke) {
      final BasicStroke bs = (BasicStroke) s;
      return bs.getLineWidth();
    }
    return 1;
  }

  /**
   * Tries to deduct the stroke-type from the given stroke object. This will
   * result in funny return values if the stroke was not created by the
   * {@link #createStroke(int, float)} method.
   * 
   * @param s
   *          the stroke.
   * @return the stroke's width.
   */
  public static int getStrokeType(final Stroke s) {
    if (s instanceof BasicStroke == false) {
      return STROKE_SOLID;
    }
    final BasicStroke bs = (BasicStroke) s;
    if (bs.getLineWidth() <= 0) {
      return STROKE_NONE;
    }

    final float[] dashes = bs.getDashArray();
    if (dashes == null) {
      return STROKE_SOLID;
    }
    if (dashes.length < 2) {
      return STROKE_SOLID;
    }
    if (dashes.length == 3 || dashes.length == 5) {
      return STROKE_SOLID;
    }

    if (dashes.length == 2) {
      // maybe dashed or dotted ...
      // if (dashes[0] < 2 && dashes[1] < 2) {
      // return STROKE_DOTTED;
      // }
      final float factor = dashes[0] / dashes[1];
      if (factor > 0.9 && factor < 1.1) {
        return STROKE_DASHED;
      } else if (factor < 0.1) {
        return STROKE_DOTTED;
      }

      // else ... not recognized ...
      return STROKE_SOLID;
    } else if (dashes.length == 4) {
      // maybe a dot-dashed stroke ...
      final float[] copyDashes = (float[]) dashes.clone();
      Arrays.sort(copyDashes);

      // the first value should be near zero ..
      if (Math.abs(copyDashes[0] / bs.getLineWidth()) > 0.5) {
        // not recognized ..
        return STROKE_SOLID;
      }

      // test that the first two values have the same size
      final float factor1 = (2 * bs.getLineWidth()) / copyDashes[1];
      final float factor2 = (2 * bs.getLineWidth()) / copyDashes[2];
      final float factorBig = (2 * bs.getLineWidth()) / copyDashes[3];

      if ((factor1 < 0.9 || factor1 > 1.1) || (factor2 < 0.9 || factor2 > 1.1)) {
        // not recognized ...
        return STROKE_SOLID;
      }

      if (factorBig < 0.4 || factorBig > 2.5) {
        return STROKE_DOT_DASH;
      }
      if (factorBig < 0.9 || factorBig > 1.1) {
        return STROKE_DOTTED;
      }
      return STROKE_DASHED;
    } else if (dashes.length == 6) {
      // maybe a dot-dashed stroke ...
      final float[] copyDashes = (float[]) dashes.clone();
      Arrays.sort(copyDashes);
      // test that the first three values have the same size

      // the first two values should be near zero ..
      if (Math.abs(copyDashes[0] / bs.getLineWidth()) > 0.5) {
        // not recognized ..
        return STROKE_SOLID;
      }
      if (Math.abs(copyDashes[1] / bs.getLineWidth()) > 0.5) {
        // not recognized ..
        return STROKE_SOLID;
      }

      final float factor2 = (2 * bs.getLineWidth()) / copyDashes[2];
      final float factor3 = (2 * bs.getLineWidth()) / copyDashes[3];
      final float factor4 = (2 * bs.getLineWidth()) / copyDashes[4];
      final float factorBig = (2 * bs.getLineWidth()) / copyDashes[5];

      if ((factor2 < 0.9 || factor2 > 1.1) || (factor3 < 0.9 || factor3 > 1.1)
          || (factor4 < 0.9 || factor4 > 1.1)) {
        return STROKE_SOLID;
      }

      if (factorBig < 0.4 || factorBig > 2.5) {
        return STROKE_DOT_DOT_DASH;
      }
      if ((factorBig < 0.9 || factorBig > 1.1)) {
        return STROKE_DOTTED;
      }
      return STROKE_DASHED;
    }
    // not recognized ...
    return STROKE_SOLID;
  }
}

   
    
    
    
  








Related examples in the same category

1.Creating Basic Shapes
2.fillRect (int, int, int, int) method draws a solid rectangle
3.Creating a Shape Using Lines and Curves
4.Combining Shapes
5.Draw rectangles, use the drawRect() method. To fill rectangles, use the fillRect() method
6.Draw lineDraw line
7.Draw a PolygonDraw a Polygon
8.Draw an oval outline
9.Draw a (Round)rectangleDraw a (Round)rectangle
10.Fill a polygonFill a polygon
11.Fill a solid oval
12.Fill a (Round)rectangleFill a (Round)rectangle
13.Change fontChange font
14.Draw rectangle 2Draw rectangle 2
15.Draw ArcDraw Arc
16.Draw EllipseDraw Ellipse
17.Fill a Rectangle 2Fill a Rectangle 2
18.Fill Arc 2Fill Arc 2
19.Draw textDraw text
20.Draw unicode string Draw unicode string
21.Shape combineShape combine
22.EffectsEffects
23.Mouse drag and drop to drawMouse drag and drop to draw
24.Arc demonstration: scale, move, rotate, sheerArc demonstration: scale, move, rotate, sheer
25.Hypnosis SpiralHypnosis Spiral
26.GlyphVector.getNumGlyphs()
27.Resize a shape
28.Rectangle with rounded corners drawn using Java 2D Graphics API
29.Compares two ellipses and returns true if they are equal or both null.
30.Compares two lines are returns true if they are equal or both null.
31.Creates a diagonal cross shape.
32.Creates a diamond shape.
33.Creates a region surrounding a line segment by 'widening' the line segment.
34.Creates a triangle shape that points downwards.
35.Creates a triangle shape that points upwards.
36.Generate Polygon
37.Polygon with float coordinates.
38.Polyline 2D
39.Serialises a Shape object.
40.Tests two polygons for equality. If both are null this method returns true.
41.Union two rectangles
42.Calculate Intersection Clip
43.Draws a shape with the specified rotation about (x, y).
44.Checks, whether the given rectangle1 fully contains rectangle 2 (even if rectangle 2 has a height or width of zero!).
45.Reads a Point2D object that has been serialised by the writePoint2D(Point2D, ObjectOutputStream)} method.
46.Returns a point based on (x, y) but constrained to be within the bounds of a given rectangle.
47.RectListManager is a class to manage a list of rectangular regions.
48.Fill Rectangle2D.Double and Ellipse2D.DoubleFill Rectangle2D.Double and Ellipse2D.Double
49.This program demonstrates the various 2D shapesThis program demonstrates the various 2D shapes