Class PEToolKit


  • public class PEToolKit
    extends java.lang.Object
    A collection of static -utilities- methods targetting number formatting, Swing's widget creation, computation of geometrical properties,...
    Since:
    jpicedt 1.0
    Version:
    $Id: PEToolKit.java,v 1.15.2.1 2007/09/02 11:56:16 reynal Exp $

    Author:
    Sylvain Reynal
    • Constructor Summary

      Constructors 
      Constructor Description
      PEToolKit()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static PicVector computeTangentToPath​(java.awt.Shape s, PicPoint pt, double maxDist)
      Returns the tangent to the given path, computed at the path-point which is closest to the given point "pt" than the given maximum distance Return null otherwise (ie if pt is too far from the given shape).
      static java.awt.geom.QuadCurve2D[] convertCubicBezierToQuad​(java.awt.geom.CubicCurve2D cubic)
      Converts the given cubic curve to a quad curve
      static PicPoint[] convertCubicBezierToQuad​(java.awt.geom.Point2D p1, java.awt.geom.Point2D pCtrl1, java.awt.geom.Point2D pCtrl2, java.awt.geom.Point2D p2)
      Given four specification points of a cubic bezier spline, returns an array of five PicPoint's containing the specification points of two quad bezier splines having the same geometry as the given curve.
      static PicPoint[] convertQuadBezierToCubic​(java.awt.geom.Point2D p1, java.awt.geom.Point2D pCtrl, java.awt.geom.Point2D p2)
      Given the three specification points of a quad bezier spline, returns an array of PicPoint's containing the four specification points of a cubic spline having the same geometry.
      static java.awt.geom.CubicCurve2D convertQuadBezierToCubic​(java.awt.geom.QuadCurve2D quad)
      Converts the given quad curve to a cubic curve
      static javax.swing.JComboBox createComboBox​(java.lang.Object[] items)
      creates a JComboBox
      static java.awt.geom.Line2D[] createFlattenedPath​(java.awt.Shape s, double flatness)
      Return an array of Line2D's representing the (visible part of the) flattened path of the given shape.
      static javax.swing.ImageIcon createImageIcon​(java.lang.String icon)
      create an ImageIcon built from "/jpicedt/images/"+icon+".gif"
      static javax.swing.JLabel createJLabel​(java.lang.String icon)
      create a JLabel with an Icon built from "/jpicedt/images/"+icon+".gif"
      static java.awt.Shape[] createPath​(java.awt.Shape s)
      Return an array of Line2D, Quad2D and Cubic2D, representing the (visible part of the) path of the given shape.
      static java.lang.String doubleToString​(double x)
      Returns a string representation of the given double.
      static java.lang.String doubleToString​(java.lang.Double x)  
      static PicPoint getDirector​(double startPtX, double startPtY, double endPtX, double endPtY, PicPoint dest)
      Deprecated. 
      static PicPoint getDirector​(PicPoint startPt, PicPoint endPt)
      Deprecated. 
      static PicPoint getDirector​(PicPoint startPt, PicPoint endPt, PicPoint dest)  
      static PicPoint getOrthogonal​(PicPoint startPt, PicPoint endPt)
      Return a unitary vector orthogonal to the vector pointing from startPt to endPt and built by rotating this vector CounterClockWise.
      static java.lang.String intToString​(double x)  
      static double[] minMaxArray​(double[] array)
      Return an array of length 2 containing the minimum and the maximum of the given array, in that order.
      static void setMaximumFractionDigits​(int n)
      Set the maximum number of fraction digits to be used when formatting doubles using the doubleToString() method.
      static int testDistanceToPath​(java.awt.Shape s, PicPoint pt, double maxDist)
      Return a positive integer if the given point "pt" lies closer to the given shape path (= stroke) than the given maximum distance ; this integer indicates which segment/quad/cubic of the path, starting from 0 (ie according to getPathIterator numbering scheme), lies at the closest distance from "pt" ; -1 otherwise.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PEToolKit

        public PEToolKit()
    • Method Detail

      • setMaximumFractionDigits

        public static void setMaximumFractionDigits​(int n)
        Set the maximum number of fraction digits to be used when formatting doubles using the doubleToString() method.
        Parameters:
        n - set to a negative number to retrieve the default behaviour
      • doubleToString

        public static java.lang.String doubleToString​(double x)
        Returns a string representation of the given double. Max nb of digits may be changed by invoking setMaximumFractionDigits beforehands.
      • doubleToString

        public static java.lang.String doubleToString​(java.lang.Double x)
        Returns:
        a string representation of the given double, ako %0.2f
      • intToString

        public static java.lang.String intToString​(double x)
        Returns:
        a string representation of the given int (with implicit cast from double to int)
      • minMaxArray

        public static double[] minMaxArray​(double[] array)
        Return an array of length 2 containing the minimum and the maximum of the given array, in that order.
        Parameters:
        array - an array of length greater than 0
      • getDirector

        public static PicPoint getDirector​(PicPoint startPt,
                                           PicPoint endPt)
        Deprecated.
        Returns:
        a unitary vector pointing from startPt to endPt
        Since:
        PicEdt 1.1 [SR:pending] moved to jpicedt.graphic.PicVector
      • getDirector

        public static PicPoint getDirector​(PicPoint startPt,
                                           PicPoint endPt,
                                           PicPoint dest)
        Returns:
        a unitary vector pointing from startPt to endPt ; if dest is non-null, it's filled with the result and returned for convenience ; otherwise, a new PicPoint gets allocated.
        Since:
        jpicedt 1.3.3 [SR:pending] moved to jpicedt.graphic.PicVector
      • getDirector

        public static PicPoint getDirector​(double startPtX,
                                           double startPtY,
                                           double endPtX,
                                           double endPtY,
                                           PicPoint dest)
        Deprecated.
        Return a unitary vector pointing from startPt to endPt ; if dest is non-null, it's filled with the result and returned for convenience ; otherwise, a new PicPoint gets allocated.
        Since:
        jpicedt 1.3.3 [SR:pending] moved to jpicedt.graphic.PicVector
      • getOrthogonal

        public static PicPoint getOrthogonal​(PicPoint startPt,
                                             PicPoint endPt)
        Return a unitary vector orthogonal to the vector pointing from startPt to endPt and built by rotating this vector CounterClockWise.
        Since:
        PicEdt 1.1 [SR:pending] moved to jpicedt.graphic.PicVector
      • computeTangentToPath

        public static PicVector computeTangentToPath​(java.awt.Shape s,
                                                     PicPoint pt,
                                                     double maxDist)
        Returns the tangent to the given path, computed at the path-point which is closest to the given point "pt" than the given maximum distance Return null otherwise (ie if pt is too far from the given shape).

        This implementation relies on FlatteningPathIterator, hence the smaller the "maxDist" parameter, the better the return result.

        Parameters:
        shape - basically this would rather be a quadratic or a cubic bezier segment, but Arc2D should work as well..
        maxDist - the maximum distance allowed between the given PicPoint and the stroke of the given Shape before null is returned ; may be Double.POSITIVE_INFINITY, in which case a tangent is always returned.
        Returns:
        a unit-norm vector
      • testDistanceToPath

        public static int testDistanceToPath​(java.awt.Shape s,
                                             PicPoint pt,
                                             double maxDist)
        Return a positive integer if the given point "pt" lies closer to the given shape path (= stroke) than the given maximum distance ; this integer indicates which segment/quad/cubic of the path, starting from 0 (ie according to getPathIterator numbering scheme), lies at the closest distance from "pt" ; -1 otherwise.

        This may be used by the UI to determine if a mouse-click occured on a stroke path, especially for complex shapes like Bezier curves, or arcs (although the returned integer only really makes sense for shape which are made of bezier curves or segments). This uses FlatteningPathIterator, with the given maxDist parameter also used as the flatness parameter, since it's the value which gives the best results.

      • createPath

        public static java.awt.Shape[] createPath​(java.awt.Shape s)
        Return an array of Line2D, Quad2D and Cubic2D, representing the (visible part of the) path of the given shape. This is based on Shape.getPathIterator() This may be used, for instance, by formaters willing to express a given shape only in terms of Bezier curves and segments, if these are the only shapes available in the given target language (e.g. this is the case with LaTeX's picture environment, where ellipses are not available).
      • createFlattenedPath

        public static java.awt.geom.Line2D[] createFlattenedPath​(java.awt.Shape s,
                                                                 double flatness)
        Return an array of Line2D's representing the (visible part of the) flattened path of the given shape. This is based on FlattenedPathIterator, and may be used by the UI to test the distance b/w a given shape and the mouse, or by a formatter to format a given shape in terms of segments only, if the target language only supports lines.
        Parameters:
        flatness - the flatness used to build the FlattenedPathIterator (max. dist. b/w shape and segments of the flattened path).
      • convertCubicBezierToQuad

        public static PicPoint[] convertCubicBezierToQuad​(java.awt.geom.Point2D p1,
                                                          java.awt.geom.Point2D pCtrl1,
                                                          java.awt.geom.Point2D pCtrl2,
                                                          java.awt.geom.Point2D p2)
        Given four specification points of a cubic bezier spline, returns an array of five PicPoint's containing the specification points of two quad bezier splines having the same geometry as the given curve.
        Parameters:
        p1 - first cubic spline end-point
        p2 - second cubic spline end-point
        pCtrl1 - first cubic spline control-point
        pCtrl2 - second cubic spline control-point
        Returns:
        an array containing {Q1P1, Q1CTRL,Q1P2=Q2P1, Q2CTRL, Q2P2}, where Q1 and Q2 refer to the two quad splines, and QiCTRL are quad's control-points.
        See Also:
        CubicCurve2D, QuadCurve2D
      • convertQuadBezierToCubic

        public static PicPoint[] convertQuadBezierToCubic​(java.awt.geom.Point2D p1,
                                                          java.awt.geom.Point2D pCtrl,
                                                          java.awt.geom.Point2D p2)
        Given the three specification points of a quad bezier spline, returns an array of PicPoint's containing the four specification points of a cubic spline having the same geometry.

        The following algorith is being used (xQ and xC refer to quad and cubic spline resp.) :

        • xC(0) = xQ(0)
        • xC(1) = (xQ(0) + 2 xQ(1))/3
        • xC(2) = (2xQ(1) + xQ(2))/3
        • xC(3) = xQ(2)
        where xQ(0) = pt1, xQ(1) = ptCtrl and xQ(2) = pt2
        Parameters:
        p1 - first quad end-point
        p2 - second quad end-point
        pCtrl - quad control-point
        Returns:
        {P1, PCTRL1, PCTRL2, P2}
      • convertQuadBezierToCubic

        public static java.awt.geom.CubicCurve2D convertQuadBezierToCubic​(java.awt.geom.QuadCurve2D quad)
        Converts the given quad curve to a cubic curve
      • convertCubicBezierToQuad

        public static java.awt.geom.QuadCurve2D[] convertCubicBezierToQuad​(java.awt.geom.CubicCurve2D cubic)
        Converts the given cubic curve to a quad curve
      • createImageIcon

        public static javax.swing.ImageIcon createImageIcon​(java.lang.String icon)
        create an ImageIcon built from "/jpicedt/images/"+icon+".gif"
      • createJLabel

        public static javax.swing.JLabel createJLabel​(java.lang.String icon)
        create a JLabel with an Icon built from "/jpicedt/images/"+icon+".gif"
      • createComboBox

        public static javax.swing.JComboBox createComboBox​(java.lang.Object[] items)
        creates a JComboBox