Java Code Examples for java.awt.BasicStroke#getDashArray()

The following examples show how to use java.awt.BasicStroke#getDashArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LineFormat.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
float[] getScaledDashArray() {
	BasicStroke stroke = getStyle().getStroke();

	if (stroke == null) {
		return null;
	}
	float[] dashArray = stroke.getDashArray();
	float[] scaledDashArray;
	if (dashArray != null) {
		float scalingFactor = getWidth();
		if (scalingFactor <= 0) {
			scalingFactor = 1;
		}
		if (scalingFactor != 1) {
			scaledDashArray = DataStructureUtils.cloneAndMultiplyArray(dashArray, scalingFactor);
		} else {
			scaledDashArray = dashArray;
		}
	} else {
		scaledDashArray = dashArray;
	}
	return scaledDashArray;
}
 
Example 2
Source File: BasicStrokeWriteHandler.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Performs the writing of a single object.
 *
 * @param tagName  the tag name.
 * @param object  the object ({@link BasicStroke} expected).
 * @param writer  the writer.
 * @param mPlexAttribute  ??
 * @param mPlexValue  ??
 * 
 * @throws IOException if there is an I/O problem.
 * @throws XMLWriterException if there is a problem with the writer.
 */
public void write(final String tagName, final Object object, final XMLWriter writer,
                  final String mPlexAttribute, final String mPlexValue)
    throws IOException, XMLWriterException {
    final BasicStroke stroke = (BasicStroke) object;
    final float[] dashArray = stroke.getDashArray();
    final float dashPhase = stroke.getDashPhase();
    final int endCap = stroke.getEndCap();
    final int lineJoin = stroke.getLineJoin();
    final float lineWidth = stroke.getLineWidth();
    final float miterLimit = stroke.getMiterLimit();
    final AttributeList attribs = new AttributeList();
    if (mPlexAttribute != null) {
        attribs.setAttribute(mPlexAttribute, mPlexValue);
    }
    attribs.setAttribute("type", "basic");
    attribs.setAttribute("endCap", String.valueOf(endCap));
    attribs.setAttribute("lineJoin", String.valueOf(lineJoin));
    attribs.setAttribute("lineWidth", String.valueOf(lineWidth));
    attribs.setAttribute("miterLimit", String.valueOf(miterLimit));
    if (dashArray != null) {
        attribs.setAttribute("dashArray", toString(dashArray));
        attribs.setAttribute("dashPhase", String.valueOf(dashPhase));
    }
    writer.writeTag(tagName, attribs, true);
}
 
Example 3
Source File: BasicStrokeValueConverter.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String toAttributeValue( final Object o ) throws BeanException {
  if ( o instanceof BasicStroke == false ) {
    throw new BeanException();
  }
  final BasicStroke s = (BasicStroke) o;
  final float lineWidth = s.getLineWidth();
  final int lineJoin = s.getLineJoin();
  final float dashPhase = s.getDashPhase();
  final int endCap = s.getEndCap();
  final float mitterLimit = s.getMiterLimit();
  final float[] dashArray = s.getDashArray();

  final StringBuilder b = new StringBuilder();
  if ( dashArray != null ) {
    for ( int i = 0; i < dashArray.length; i++ ) {
      if ( i != 0 ) {
        b.append( "," );
      }
      b.append( dashArray[i] );
    }
  }

  return String.format( Locale.US, "BasicStroke:%f:%d:%f:%d:%f:%s", lineWidth, lineJoin, dashPhase, endCap,
      mitterLimit, b.toString() );
}
 
Example 4
Source File: PixelToParallelogramConverter.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public void drawRect(SunGraphics2D sg2d,
                     int x, int y, int w, int h)
{
    if (w >= 0 && h >= 0) {
        if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM) {
            BasicStroke bs = ((BasicStroke) sg2d.stroke);
            if (w > 0 && h > 0) {
                if (bs.getLineJoin() == BasicStroke.JOIN_MITER &&
                    bs.getDashArray() == null)
                {
                    double lw = bs.getLineWidth();
                    drawRectangle(sg2d, x, y, w, h, lw);
                    return;
                }
            } else {
                // Note: This calls the integer version which
                // will verify that the local drawLine optimizations
                // work and call super.drawLine(), if not.
                drawLine(sg2d, x, y, x+w, y+h);
                return;
            }
        }
        super.drawRect(sg2d, x, y, w, h);
    }
}
 
Example 5
Source File: PixelToParallelogramConverter.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void drawRect(SunGraphics2D sg2d,
                     int x, int y, int w, int h)
{
    if (w >= 0 && h >= 0) {
        if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM) {
            BasicStroke bs = ((BasicStroke) sg2d.stroke);
            if (w > 0 && h > 0) {
                if (bs.getLineJoin() == BasicStroke.JOIN_MITER &&
                    bs.getDashArray() == null)
                {
                    double lw = bs.getLineWidth();
                    drawRectangle(sg2d, x, y, w, h, lw);
                    return;
                }
            } else {
                // Note: This calls the integer version which
                // will verify that the local drawLine optimizations
                // work and call super.drawLine(), if not.
                drawLine(sg2d, x, y, x+w, y+h);
                return;
            }
        }
        super.drawRect(sg2d, x, y, w, h);
    }
}
 
Example 6
Source File: PdfGraphics2D.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private Stroke transformStroke(Stroke stroke) {
	if (!(stroke instanceof BasicStroke)) {
		return stroke;
	}
	BasicStroke st = (BasicStroke) stroke;
	float scale = (float) Math.sqrt(Math.abs(transform.getDeterminant()));
	float dash[] = st.getDashArray();
	if (dash != null) {
		for (int k = 0; k < dash.length; ++k) {
			dash[k] *= scale;
		}
	}
	return new BasicStroke(st.getLineWidth() * scale, st.getEndCap(), st.getLineJoin(), st.getMiterLimit(), dash, st.getDashPhase() * scale);
}
 
Example 7
Source File: SLGraphics.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void applyStroke(SimpleShape<?,?> shape) {
    if (_stroke instanceof BasicStroke){
        BasicStroke bs = (BasicStroke)_stroke;
        shape.setStrokeStyle((double)bs.getLineWidth());
        float[] dash = bs.getDashArray();
        if (dash != null) {
            //TODO: implement more dashing styles
            shape.setStrokeStyle(StrokeStyle.LineDash.DASH);
        }
    }
}
 
Example 8
Source File: DataSaver.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
public static void saveStroke(final OutputStream stream,
                              final Stroke stroke, final MemoryData memoryData)
        throws IOException {
    if (stroke instanceof BasicStroke) {
        BasicStroke basickStroke = (BasicStroke) stroke;
        saveBoolean(stream, true);

        saveDouble(stream, basickStroke.getLineWidth());
        saveInteger(stream, basickStroke.getEndCap());
        saveInteger(stream, basickStroke.getLineJoin());
        saveDouble(stream, basickStroke.getDashPhase());
        saveDouble(stream, basickStroke.getMiterLimit());
        final float ar[] = basickStroke.getDashArray();
        if (ar == null)
            saveInteger(stream, -1);
        else {
            saveInteger(stream, ar.length);
            for (final float element : ar)
                saveDouble(stream, element);
        }

        memoryData.stroukes.add(stroke);
    } else if (stroke instanceof WayStroke) {
        saveBoolean(stream, false);
        saveInteger(stream, -10 - ((WayStroke) stroke).getType());
    } else if (stroke instanceof ArrowedStroke) {
        saveBoolean(stream, false);
        saveInteger(stream, -20 - ((ArrowedStroke) stroke).getType());
    }
}
 
Example 9
Source File: PixelToParallelogramConverter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void draw(SunGraphics2D sg2d, Shape s) {
    if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM) {
        BasicStroke bs = ((BasicStroke) sg2d.stroke);
        if (s instanceof Rectangle2D) {
            if (bs.getLineJoin() == BasicStroke.JOIN_MITER &&
                bs.getDashArray() == null)
            {
                Rectangle2D r2d = (Rectangle2D) s;
                double w = r2d.getWidth();
                double h = r2d.getHeight();
                double x = r2d.getX();
                double y = r2d.getY();
                if (w >= 0 && h >= 0) {
                    double lw = bs.getLineWidth();
                    drawRectangle(sg2d, x, y, w, h, lw);
                }
                return;
            }
        } else if (s instanceof Line2D) {
            Line2D l2d = (Line2D) s;
            if (drawGeneralLine(sg2d,
                                l2d.getX1(), l2d.getY1(),
                                l2d.getX2(), l2d.getY2()))
            {
                return;
            }
        }
    }

    outpipe.draw(sg2d, s);
}
 
Example 10
Source File: PixelToParallelogramConverter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void draw(SunGraphics2D sg2d, Shape s) {
    if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM) {
        BasicStroke bs = ((BasicStroke) sg2d.stroke);
        if (s instanceof Rectangle2D) {
            if (bs.getLineJoin() == BasicStroke.JOIN_MITER &&
                bs.getDashArray() == null)
            {
                Rectangle2D r2d = (Rectangle2D) s;
                double w = r2d.getWidth();
                double h = r2d.getHeight();
                double x = r2d.getX();
                double y = r2d.getY();
                if (w >= 0 && h >= 0) {
                    double lw = bs.getLineWidth();
                    drawRectangle(sg2d, x, y, w, h, lw);
                }
                return;
            }
        } else if (s instanceof Line2D) {
            Line2D l2d = (Line2D) s;
            if (drawGeneralLine(sg2d,
                                l2d.getX1(), l2d.getY1(),
                                l2d.getX2(), l2d.getY2()))
            {
                return;
            }
        }
    }

    outpipe.draw(sg2d, s);
}
 
Example 11
Source File: PixelToParallelogramConverter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void draw(SunGraphics2D sg2d, Shape s) {
    if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM) {
        BasicStroke bs = ((BasicStroke) sg2d.stroke);
        if (s instanceof Rectangle2D) {
            if (bs.getLineJoin() == BasicStroke.JOIN_MITER &&
                bs.getDashArray() == null)
            {
                Rectangle2D r2d = (Rectangle2D) s;
                double w = r2d.getWidth();
                double h = r2d.getHeight();
                double x = r2d.getX();
                double y = r2d.getY();
                if (w >= 0 && h >= 0) {
                    double lw = bs.getLineWidth();
                    drawRectangle(sg2d, x, y, w, h, lw);
                }
                return;
            }
        } else if (s instanceof Line2D) {
            Line2D l2d = (Line2D) s;
            if (drawGeneralLine(sg2d,
                                l2d.getX1(), l2d.getY1(),
                                l2d.getX2(), l2d.getY2()))
            {
                return;
            }
        }
    }

    outpipe.draw(sg2d, s);
}
 
Example 12
Source File: EdgeEditor.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
static Stroke setEndCap(Stroke s, int cap) {
     if(s instanceof BasicStroke) {
BasicStroke b = (BasicStroke)s;
return new BasicStroke(b.getLineWidth(),
		       cap,
		       b.getLineJoin(),
		       b.getMiterLimit(),
		       b.getDashArray(),
		       b.getDashPhase());
     }
     else return s;
   }
 
Example 13
Source File: DuctusRenderingEngine.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void strokeTo(Shape src,
                     AffineTransform transform,
                     BasicStroke bs,
                     boolean thin,
                     boolean normalize,
                     boolean antialias,
                     PathConsumer2D sr)
{
    PathStroker stroker = new PathStroker(sr);
    PathConsumer consumer = stroker;

    float matrix[] = null;
    if (!thin) {
        stroker.setPenDiameter(bs.getLineWidth());
        if (transform != null) {
            matrix = getTransformMatrix(transform);
        }
        stroker.setPenT4(matrix);
        stroker.setPenFitting(PenUnits, MinPenUnits);
    }
    stroker.setCaps(RasterizerCaps[bs.getEndCap()]);
    stroker.setCorners(RasterizerCorners[bs.getLineJoin()],
                       bs.getMiterLimit());
    float[] dashes = bs.getDashArray();
    if (dashes != null) {
        PathDasher dasher = new PathDasher(stroker);
        dasher.setDash(dashes, bs.getDashPhase());
        if (transform != null && matrix == null) {
            matrix = getTransformMatrix(transform);
        }
        dasher.setDashT4(matrix);
        consumer = dasher;
    }

    try {
        PathIterator pi = src.getPathIterator(transform);

        feedConsumer(pi, consumer, normalize, 0.25f);
    } catch (PathException e) {
        throw new InternalError("Unable to Stroke shape ("+
                                e.getMessage()+")", e);
    } finally {
        while (consumer != null && consumer != sr) {
            PathConsumer next = consumer.getConsumer();
            consumer.dispose();
            consumer = next;
        }
    }
}
 
Example 14
Source File: WPathGraphics.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draw a line using a pen created using the specified color
 * and current stroke properties.
 */
@Override
protected void deviceDrawLine(int xBegin, int yBegin, int xEnd, int yEnd,
                              Color color) {
    Stroke stroke = getStroke();

    if (stroke instanceof BasicStroke) {
        BasicStroke lineStroke = (BasicStroke) stroke;

        if (lineStroke.getDashArray() != null) {
            draw(new Line2D.Float(xBegin, yBegin, xEnd, yEnd));
            return;
        }

        float lineWidth = lineStroke.getLineWidth();
        Point2D.Float penSize = new Point2D.Float(lineWidth, lineWidth);

        AffineTransform deviceTransform = getTransform();
        deviceTransform.deltaTransform(penSize, penSize);

        float deviceLineWidth = Math.min(Math.abs(penSize.x),
                                         Math.abs(penSize.y));

        Point2D.Float begin_pos = new Point2D.Float(xBegin, yBegin);
        deviceTransform.transform(begin_pos, begin_pos);

        Point2D.Float end_pos = new Point2D.Float(xEnd, yEnd);
        deviceTransform.transform(end_pos, end_pos);

        int endCap = lineStroke.getEndCap();
        int lineJoin = lineStroke.getLineJoin();

        /* check if it's a one-pixel line */
        if ((end_pos.getX() == begin_pos.getX())
            && (end_pos.getY() == begin_pos.getY())) {

            /* endCap other than Round will not print!
             * due to Windows GDI limitation, force it to CAP_ROUND
             */
            endCap = BasicStroke.CAP_ROUND;
        }


        WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();

        /* call native function that creates pen with style */
        if (wPrinterJob.selectStylePen(endCap, lineJoin,
                                       deviceLineWidth, color)) {
            wPrinterJob.moveTo((float)begin_pos.getX(),
                               (float)begin_pos.getY());
            wPrinterJob.lineTo((float)end_pos.getX(),
                               (float)end_pos.getY());
        }
        /* selectStylePen is not supported, must be Win 9X */
        else {

            /* let's see if we can use a a default pen
             *  if it's round end (Windows' default style)
             *  or it's vertical/horizontal
             *  or stroke is too thin.
             */
            double lowerRes = Math.min(wPrinterJob.getXRes(),
                                       wPrinterJob.getYRes());

            if ((endCap == BasicStroke.CAP_ROUND) ||
             (((xBegin == xEnd) || (yBegin == yEnd)) &&
             (deviceLineWidth/lowerRes < MAX_THINLINE_INCHES))) {

                wPrinterJob.selectPen(deviceLineWidth, color);
                wPrinterJob.moveTo((float)begin_pos.getX(),
                                   (float)begin_pos.getY());
                wPrinterJob.lineTo((float)end_pos.getX(),
                                   (float)end_pos.getY());
            }
            else {
                draw(new Line2D.Float(xBegin, yBegin, xEnd, yEnd));
            }
        }
    }
}
 
Example 15
Source File: WPathGraphics.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draw a line using a pen created using the specified color
 * and current stroke properties.
 */
@Override
protected void deviceDrawLine(int xBegin, int yBegin, int xEnd, int yEnd,
                              Color color) {
    Stroke stroke = getStroke();

    if (stroke instanceof BasicStroke) {
        BasicStroke lineStroke = (BasicStroke) stroke;

        if (lineStroke.getDashArray() != null) {
            draw(new Line2D.Float(xBegin, yBegin, xEnd, yEnd));
            return;
        }

        float lineWidth = lineStroke.getLineWidth();
        Point2D.Float penSize = new Point2D.Float(lineWidth, lineWidth);

        AffineTransform deviceTransform = getTransform();
        deviceTransform.deltaTransform(penSize, penSize);

        float deviceLineWidth = Math.min(Math.abs(penSize.x),
                                         Math.abs(penSize.y));

        Point2D.Float begin_pos = new Point2D.Float(xBegin, yBegin);
        deviceTransform.transform(begin_pos, begin_pos);

        Point2D.Float end_pos = new Point2D.Float(xEnd, yEnd);
        deviceTransform.transform(end_pos, end_pos);

        int endCap = lineStroke.getEndCap();
        int lineJoin = lineStroke.getLineJoin();

        /* check if it's a one-pixel line */
        if ((end_pos.getX() == begin_pos.getX())
            && (end_pos.getY() == begin_pos.getY())) {

            /* endCap other than Round will not print!
             * due to Windows GDI limitation, force it to CAP_ROUND
             */
            endCap = BasicStroke.CAP_ROUND;
        }


        WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();

        /* call native function that creates pen with style */
        if (wPrinterJob.selectStylePen(endCap, lineJoin,
                                       deviceLineWidth, color)) {
            wPrinterJob.moveTo((float)begin_pos.getX(),
                               (float)begin_pos.getY());
            wPrinterJob.lineTo((float)end_pos.getX(),
                               (float)end_pos.getY());
        }
        /* selectStylePen is not supported, must be Win 9X */
        else {

            /* let's see if we can use a a default pen
             *  if it's round end (Windows' default style)
             *  or it's vertical/horizontal
             *  or stroke is too thin.
             */
            double lowerRes = Math.min(wPrinterJob.getXRes(),
                                       wPrinterJob.getYRes());

            if ((endCap == BasicStroke.CAP_ROUND) ||
             (((xBegin == xEnd) || (yBegin == yEnd)) &&
             (deviceLineWidth/lowerRes < MAX_THINLINE_INCHES))) {

                wPrinterJob.selectPen(deviceLineWidth, color);
                wPrinterJob.moveTo((float)begin_pos.getX(),
                                   (float)begin_pos.getY());
                wPrinterJob.lineTo((float)end_pos.getX(),
                                   (float)end_pos.getY());
            }
            else {
                draw(new Line2D.Float(xBegin, yBegin, xEnd, yEnd));
            }
        }
    }
}
 
Example 16
Source File: SunGraphics2D.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void validateBasicStroke(BasicStroke bs) {
    boolean aa = (antialiasHint == SunHints.INTVAL_ANTIALIAS_ON);
    if (transformState < TRANSFORM_TRANSLATESCALE) {
        if (aa) {
            if (bs.getLineWidth() <= MinPenSizeAA) {
                if (bs.getDashArray() == null) {
                    strokeState = STROKE_THIN;
                } else {
                    strokeState = STROKE_THINDASHED;
                }
            } else {
                strokeState = STROKE_WIDE;
            }
        } else {
            if (bs == defaultStroke) {
                strokeState = STROKE_THIN;
            } else if (bs.getLineWidth() <= 1.0f) {
                if (bs.getDashArray() == null) {
                    strokeState = STROKE_THIN;
                } else {
                    strokeState = STROKE_THINDASHED;
                }
            } else {
                strokeState = STROKE_WIDE;
            }
        }
    } else {
        double widthsquared;
        if ((transform.getType() & NON_UNIFORM_SCALE_MASK) == 0) {
            /* sqrt omitted, compare to squared limits below. */
            widthsquared = Math.abs(transform.getDeterminant());
        } else {
            /* First calculate the "maximum scale" of this transform. */
            double A = transform.getScaleX();       // m00
            double C = transform.getShearX();       // m01
            double B = transform.getShearY();       // m10
            double D = transform.getScaleY();       // m11

            /*
             * Given a 2 x 2 affine matrix [ A B ] such that
             *                             [ C D ]
             * v' = [x' y'] = [Ax + Cy, Bx + Dy], we want to
             * find the maximum magnitude (norm) of the vector v'
             * with the constraint (x^2 + y^2 = 1).
             * The equation to maximize is
             *     |v'| = sqrt((Ax+Cy)^2+(Bx+Dy)^2)
             * or  |v'| = sqrt((AA+BB)x^2 + 2(AC+BD)xy + (CC+DD)y^2).
             * Since sqrt is monotonic we can maximize |v'|^2
             * instead and plug in the substitution y = sqrt(1 - x^2).
             * Trigonometric equalities can then be used to get
             * rid of most of the sqrt terms.
             */
            double EA = A*A + B*B;          // x^2 coefficient
            double EB = 2*(A*C + B*D);      // xy coefficient
            double EC = C*C + D*D;          // y^2 coefficient

            /*
             * There is a lot of calculus omitted here.
             *
             * Conceptually, in the interests of understanding the
             * terms that the calculus produced we can consider
             * that EA and EC end up providing the lengths along
             * the major axes and the hypot term ends up being an
             * adjustment for the additional length along the off-axis
             * angle of rotated or sheared ellipses as well as an
             * adjustment for the fact that the equation below
             * averages the two major axis lengths.  (Notice that
             * the hypot term contains a part which resolves to the
             * difference of these two axis lengths in the absence
             * of rotation.)
             *
             * In the calculus, the ratio of the EB and (EA-EC) terms
             * ends up being the tangent of 2*theta where theta is
             * the angle that the long axis of the ellipse makes
             * with the horizontal axis.  Thus, this equation is
             * calculating the length of the hypotenuse of a triangle
             * along that axis.
             */
            double hypot = Math.sqrt(EB*EB + (EA-EC)*(EA-EC));

            /* sqrt omitted, compare to squared limits below. */
            widthsquared = ((EA + EC + hypot)/2.0);
        }
        if (bs != defaultStroke) {
            widthsquared *= bs.getLineWidth() * bs.getLineWidth();
        }
        if (widthsquared <=
            (aa ? MinPenSizeAASquared : MinPenSizeSquared))
        {
            if (bs.getDashArray() == null) {
                strokeState = STROKE_THIN;
            } else {
                strokeState = STROKE_THINDASHED;
            }
        } else {
            strokeState = STROKE_WIDE;
        }
    }
}
 
Example 17
Source File: WPathGraphics.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draw a line using a pen created using the specified color
 * and current stroke properties.
 */
@Override
protected void deviceDrawLine(int xBegin, int yBegin, int xEnd, int yEnd,
                              Color color) {
    Stroke stroke = getStroke();

    if (stroke instanceof BasicStroke) {
        BasicStroke lineStroke = (BasicStroke) stroke;

        if (lineStroke.getDashArray() != null) {
            draw(new Line2D.Float(xBegin, yBegin, xEnd, yEnd));
            return;
        }

        float lineWidth = lineStroke.getLineWidth();
        Point2D.Float penSize = new Point2D.Float(lineWidth, lineWidth);

        AffineTransform deviceTransform = getTransform();
        deviceTransform.deltaTransform(penSize, penSize);

        float deviceLineWidth = Math.min(Math.abs(penSize.x),
                                         Math.abs(penSize.y));

        Point2D.Float begin_pos = new Point2D.Float(xBegin, yBegin);
        deviceTransform.transform(begin_pos, begin_pos);

        Point2D.Float end_pos = new Point2D.Float(xEnd, yEnd);
        deviceTransform.transform(end_pos, end_pos);

        int endCap = lineStroke.getEndCap();
        int lineJoin = lineStroke.getLineJoin();

        /* check if it's a one-pixel line */
        if ((end_pos.getX() == begin_pos.getX())
            && (end_pos.getY() == begin_pos.getY())) {

            /* endCap other than Round will not print!
             * due to Windows GDI limitation, force it to CAP_ROUND
             */
            endCap = BasicStroke.CAP_ROUND;
        }


        WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();

        /* call native function that creates pen with style */
        if (wPrinterJob.selectStylePen(endCap, lineJoin,
                                       deviceLineWidth, color)) {
            wPrinterJob.moveTo((float)begin_pos.getX(),
                               (float)begin_pos.getY());
            wPrinterJob.lineTo((float)end_pos.getX(),
                               (float)end_pos.getY());
        }
        /* selectStylePen is not supported, must be Win 9X */
        else {

            /* let's see if we can use a a default pen
             *  if it's round end (Windows' default style)
             *  or it's vertical/horizontal
             *  or stroke is too thin.
             */
            double lowerRes = Math.min(wPrinterJob.getXRes(),
                                       wPrinterJob.getYRes());

            if ((endCap == BasicStroke.CAP_ROUND) ||
             (((xBegin == xEnd) || (yBegin == yEnd)) &&
             (deviceLineWidth/lowerRes < MAX_THINLINE_INCHES))) {

                wPrinterJob.selectPen(deviceLineWidth, color);
                wPrinterJob.moveTo((float)begin_pos.getX(),
                                   (float)begin_pos.getY());
                wPrinterJob.lineTo((float)end_pos.getX(),
                                   (float)end_pos.getY());
            }
            else {
                draw(new Line2D.Float(xBegin, yBegin, xEnd, yEnd));
            }
        }
    }
}
 
Example 18
Source File: SunGraphics2D.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void validateBasicStroke(BasicStroke bs) {
    boolean aa = (antialiasHint == SunHints.INTVAL_ANTIALIAS_ON);
    if (transformState < TRANSFORM_TRANSLATESCALE) {
        if (aa) {
            if (bs.getLineWidth() <= MinPenSizeAA) {
                if (bs.getDashArray() == null) {
                    strokeState = STROKE_THIN;
                } else {
                    strokeState = STROKE_THINDASHED;
                }
            } else {
                strokeState = STROKE_WIDE;
            }
        } else {
            if (bs == defaultStroke) {
                strokeState = STROKE_THIN;
            } else if (bs.getLineWidth() <= 1.0f) {
                if (bs.getDashArray() == null) {
                    strokeState = STROKE_THIN;
                } else {
                    strokeState = STROKE_THINDASHED;
                }
            } else {
                strokeState = STROKE_WIDE;
            }
        }
    } else {
        double widthsquared;
        if ((transform.getType() & NON_UNIFORM_SCALE_MASK) == 0) {
            /* sqrt omitted, compare to squared limits below. */
            widthsquared = Math.abs(transform.getDeterminant());
        } else {
            /* First calculate the "maximum scale" of this transform. */
            double A = transform.getScaleX();       // m00
            double C = transform.getShearX();       // m01
            double B = transform.getShearY();       // m10
            double D = transform.getScaleY();       // m11

            /*
             * Given a 2 x 2 affine matrix [ A B ] such that
             *                             [ C D ]
             * v' = [x' y'] = [Ax + Cy, Bx + Dy], we want to
             * find the maximum magnitude (norm) of the vector v'
             * with the constraint (x^2 + y^2 = 1).
             * The equation to maximize is
             *     |v'| = sqrt((Ax+Cy)^2+(Bx+Dy)^2)
             * or  |v'| = sqrt((AA+BB)x^2 + 2(AC+BD)xy + (CC+DD)y^2).
             * Since sqrt is monotonic we can maximize |v'|^2
             * instead and plug in the substitution y = sqrt(1 - x^2).
             * Trigonometric equalities can then be used to get
             * rid of most of the sqrt terms.
             */
            double EA = A*A + B*B;          // x^2 coefficient
            double EB = 2*(A*C + B*D);      // xy coefficient
            double EC = C*C + D*D;          // y^2 coefficient

            /*
             * There is a lot of calculus omitted here.
             *
             * Conceptually, in the interests of understanding the
             * terms that the calculus produced we can consider
             * that EA and EC end up providing the lengths along
             * the major axes and the hypot term ends up being an
             * adjustment for the additional length along the off-axis
             * angle of rotated or sheared ellipses as well as an
             * adjustment for the fact that the equation below
             * averages the two major axis lengths.  (Notice that
             * the hypot term contains a part which resolves to the
             * difference of these two axis lengths in the absence
             * of rotation.)
             *
             * In the calculus, the ratio of the EB and (EA-EC) terms
             * ends up being the tangent of 2*theta where theta is
             * the angle that the long axis of the ellipse makes
             * with the horizontal axis.  Thus, this equation is
             * calculating the length of the hypotenuse of a triangle
             * along that axis.
             */
            double hypot = Math.sqrt(EB*EB + (EA-EC)*(EA-EC));

            /* sqrt omitted, compare to squared limits below. */
            widthsquared = ((EA + EC + hypot)/2.0);
        }
        if (bs != defaultStroke) {
            widthsquared *= bs.getLineWidth() * bs.getLineWidth();
        }
        if (widthsquared <=
            (aa ? MinPenSizeAASquared : MinPenSizeSquared))
        {
            if (bs.getDashArray() == null) {
                strokeState = STROKE_THIN;
            } else {
                strokeState = STROKE_THINDASHED;
            }
        } else {
            strokeState = STROKE_WIDE;
        }
    }
}
 
Example 19
Source File: WPathGraphics.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draw a line using a pen created using the specified color
 * and current stroke properties.
 */
protected void deviceDrawLine(int xBegin, int yBegin, int xEnd, int yEnd,
                              Color color) {
    Stroke stroke = getStroke();

    if (stroke instanceof BasicStroke) {
        BasicStroke lineStroke = (BasicStroke) stroke;

        if (lineStroke.getDashArray() != null) {
            draw(new Line2D.Float(xBegin, yBegin, xEnd, yEnd));
            return;
        }

        float lineWidth = lineStroke.getLineWidth();
        Point2D.Float penSize = new Point2D.Float(lineWidth, lineWidth);

        AffineTransform deviceTransform = getTransform();
        deviceTransform.deltaTransform(penSize, penSize);

        float deviceLineWidth = Math.min(Math.abs(penSize.x),
                                         Math.abs(penSize.y));

        Point2D.Float begin_pos = new Point2D.Float(xBegin, yBegin);
        deviceTransform.transform(begin_pos, begin_pos);

        Point2D.Float end_pos = new Point2D.Float(xEnd, yEnd);
        deviceTransform.transform(end_pos, end_pos);

        int endCap = lineStroke.getEndCap();
        int lineJoin = lineStroke.getLineJoin();

        /* check if it's a one-pixel line */
        if ((end_pos.getX() == begin_pos.getX())
            && (end_pos.getY() == begin_pos.getY())) {

            /* endCap other than Round will not print!
             * due to Windows GDI limitation, force it to CAP_ROUND
             */
            endCap = BasicStroke.CAP_ROUND;
        }


        WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();

        /* call native function that creates pen with style */
        if (wPrinterJob.selectStylePen(endCap, lineJoin,
                                       deviceLineWidth, color)) {
            wPrinterJob.moveTo((float)begin_pos.getX(),
                               (float)begin_pos.getY());
            wPrinterJob.lineTo((float)end_pos.getX(),
                               (float)end_pos.getY());
        }
        /* selectStylePen is not supported, must be Win 9X */
        else {

            /* let's see if we can use a a default pen
             *  if it's round end (Windows' default style)
             *  or it's vertical/horizontal
             *  or stroke is too thin.
             */
            double lowerRes = Math.min(wPrinterJob.getXRes(),
                                       wPrinterJob.getYRes());

            if ((endCap == BasicStroke.CAP_ROUND) ||
             (((xBegin == xEnd) || (yBegin == yEnd)) &&
             (deviceLineWidth/lowerRes < MAX_THINLINE_INCHES))) {

                wPrinterJob.selectPen(deviceLineWidth, color);
                wPrinterJob.moveTo((float)begin_pos.getX(),
                                   (float)begin_pos.getY());
                wPrinterJob.lineTo((float)end_pos.getX(),
                                   (float)end_pos.getY());
            }
            else {
                draw(new Line2D.Float(xBegin, yBegin, xEnd, yEnd));
            }
        }
    }
}
 
Example 20
Source File: DuctusRenderingEngine.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void strokeTo(Shape src,
                     AffineTransform transform,
                     BasicStroke bs,
                     boolean thin,
                     boolean normalize,
                     boolean antialias,
                     PathConsumer2D sr)
{
    PathStroker stroker = new PathStroker(sr);
    PathConsumer consumer = stroker;

    float matrix[] = null;
    if (!thin) {
        stroker.setPenDiameter(bs.getLineWidth());
        if (transform != null) {
            matrix = getTransformMatrix(transform);
        }
        stroker.setPenT4(matrix);
        stroker.setPenFitting(PenUnits, MinPenUnits);
    }
    stroker.setCaps(RasterizerCaps[bs.getEndCap()]);
    stroker.setCorners(RasterizerCorners[bs.getLineJoin()],
                       bs.getMiterLimit());
    float[] dashes = bs.getDashArray();
    if (dashes != null) {
        PathDasher dasher = new PathDasher(stroker);
        dasher.setDash(dashes, bs.getDashPhase());
        if (transform != null && matrix == null) {
            matrix = getTransformMatrix(transform);
        }
        dasher.setDashT4(matrix);
        consumer = dasher;
    }

    try {
        PathIterator pi = src.getPathIterator(transform);

        feedConsumer(pi, consumer, normalize, 0.25f);
    } catch (PathException e) {
        throw new InternalError("Unable to Stroke shape ("+
                                e.getMessage()+")", e);
    } finally {
        while (consumer != null && consumer != sr) {
            PathConsumer next = consumer.getConsumer();
            consumer.dispose();
            consumer = next;
        }
    }
}