Example usage for java.awt BasicStroke getLineWidth

List of usage examples for java.awt BasicStroke getLineWidth

Introduction

In this page you can find the example usage for java.awt BasicStroke getLineWidth.

Prototype

public float getLineWidth() 

Source Link

Document

Returns the line width.

Usage

From source file:Main.java

public static void main(String[] args) {
    BasicStroke stroke = new BasicStroke(10, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0.1F);
    System.out.println(stroke.getLineWidth());

}

From source file:StrokeUtility.java

/**
 * Tries to extract the stroke-width from the given stroke object.
 * //  w  ww.  j a  v  a 2  s.c o m
 * @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;
}

From source file:Main.java

/**
 * Serialises a <code>Stroke</code> object.  This code handles the
 * <code>BasicStroke</code> class which is the only <code>Stroke</code>
 * implementation provided by the JDK (and isn't directly
 * <code>Serializable</code>).
 *
 * @param stroke  the stroke object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *///from w w  w . j  av  a 2  s . c  om
public static void writeStroke(final Stroke stroke, final ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (stroke != null) {
        stream.writeBoolean(false);
        if (stroke instanceof BasicStroke) {
            final BasicStroke s = (BasicStroke) stroke;
            stream.writeObject(BasicStroke.class);
            stream.writeFloat(s.getLineWidth());
            stream.writeInt(s.getEndCap());
            stream.writeInt(s.getLineJoin());
            stream.writeFloat(s.getMiterLimit());
            stream.writeObject(s.getDashArray());
            stream.writeFloat(s.getDashPhase());
        } else {
            stream.writeObject(stroke.getClass());
            stream.writeObject(stroke);
        }
    } else {
        stream.writeBoolean(true);
    }
}

From source file:StrokeUtility.java

/**
 * 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.
 * //from   www.  j ava2 s . c  o m
 * @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;
}

From source file:de.bund.bfr.jung.JungUtils.java

public static <V, E> Pair<Transformer<E, Stroke>, Transformer<Context<Graph<V, E>, E>, Shape>> newEdgeStrokeArrowTransformers(
        int thickness, Integer maxThickness, Map<E, Double> thicknessValues) {
    double denom = getDenominator(thicknessValues);
    int max = maxThickness != null ? maxThickness : thickness + 10;

    Transformer<E, Stroke> strokeTransformer = edge -> {
        Double factor = thicknessValues != null ? thicknessValues.get(edge) : null;
        double width = factor != null ? thickness + (max - thickness) * factor / denom : thickness;

        return new BasicStroke((float) width);
    };/*  ww w .j  a v  a 2 s . c om*/
    Transformer<Context<Graph<V, E>, E>, Shape> arrowTransformer = edge -> {
        BasicStroke stroke = (BasicStroke) strokeTransformer.transform(edge.element);

        return ArrowFactory.getNotchedArrow(stroke.getLineWidth() + 8, 2 * stroke.getLineWidth() + 10, 4);
    };

    return new Pair<>(strokeTransformer, arrowTransformer);
}

From source file:Main.java

public static void writeStroke(Stroke aStroke, ObjectOutputStream out) throws IOException {
    if (aStroke instanceof Serializable) {
        out.writeBoolean(true);//from www .  j  a va 2  s  .  c  om
        out.writeBoolean(true);
        out.writeObject(aStroke);
    } else if (aStroke instanceof BasicStroke) {
        out.writeBoolean(true);
        out.writeBoolean(false);
        BasicStroke s = (BasicStroke) aStroke;

        float[] dash = s.getDashArray();

        if (dash == null) {
            out.write(0);
        } else {
            out.write(dash.length);
            for (int i = 0; i < dash.length; i++) {
                out.writeFloat(dash[i]);
            }
        }

        out.writeFloat(s.getLineWidth());
        out.writeInt(s.getEndCap());
        out.writeInt(s.getLineJoin());
        out.writeFloat(s.getMiterLimit());
        out.writeFloat(s.getDashPhase());
    } else {
        out.writeBoolean(false);
    }
}

From source file:com.jaspersoft.studio.components.chart.model.theme.util.PadUtil.java

/**
 * Method used to extract a property from a block frame provider using an id of 
 * the property. If the blockframe is undefined a default value is given
 * /*  w  w  w .ja v  a  2s  .  co  m*/
 * @param id identifier of the property
 * @param provider object from where the property is extracted
 * @return the result, can be null
 */
public static Object getBlockFrameValue(Object id, BlockFrameProvider provider) {
    if (provider == null) {
        return frameDefaultValues.get(id);
    } else {
        String preID = LegendSettings.PROPERTY_frame;
        if (id.equals(preID + PadUtil.FRAME_FILL)) {
            return !(provider instanceof LineBorderProvider);
        } else if (id.equals(preID + PadUtil.FRAME_STROKE)) {
            if (provider instanceof LineBorderProvider) {
                BasicStroke stroke = (BasicStroke) ((LineBorderProvider) provider).getLineStroke();
                return stroke != null ? stroke.getLineWidth() : null;
            } else
                return null;
        } else if (id.equals(preID + FRAME_COLOR)) {
            return getPaint(provider);
        } else {
            return getPropertyValue(id, provider.getBlockFrame().getInsets(), preID);
        }
    }
}

From source file:Java2DUtils.java

/**
 * This function provides a way to cancel the effects of the zoom on a particular Stroke.
 * All the stroke values (as line width, dashes, etc...) are divided by the zoom factor.
 * This allow to have essentially a fixed Stroke independent by the zoom.
 * The returned Stroke is a copy./*ww w.j a v a2s.  c o  m*/
 * Remember to restore the right stroke in the graphics when done.
 * 
 * It works only with instances of BasicStroke
 * 
 * zoom is the zoom factor.
 */
public static Stroke getInvertedZoomedStroke(Stroke stroke, double zoom) {
    if (stroke == null || !(stroke instanceof BasicStroke))
        return stroke;

    BasicStroke bs = (BasicStroke) stroke;
    float[] dashArray = bs.getDashArray();

    float[] newDashArray = null;
    if (dashArray != null) {
        newDashArray = new float[dashArray.length];
        for (int i = 0; i < newDashArray.length; ++i) {
            newDashArray[i] = (float) (dashArray[i] / zoom);
        }
    }

    BasicStroke newStroke = new BasicStroke((float) (bs.getLineWidth() / zoom), bs.getEndCap(),
            bs.getLineJoin(), bs.getMiterLimit(),
            //(float)(bs.getMiterLimit() / zoom),
            newDashArray, (float) (bs.getDashPhase() / zoom));
    return newStroke;
}

From source file:net.sf.jasperreports.customizers.marker.AbstractMarkerCustomizer.java

protected BasicStroke getStroke(Float strokeWidth) {
    BasicStroke basicStroke = new BasicStroke(strokeWidth);

    StrokeStyleEnum strokeStyle = StrokeStyleEnum.getByName(getProperty(PROPERTY_STROKE_STYLE));
    if (strokeStyle != null) {
        switch (strokeStyle) {
        case SOLID: {
            //do nothing; already created stroke is good
            break;
        }/*from  www  .j  av a  2s  . c o  m*/
        case DOTTED: {
            basicStroke = new BasicStroke(basicStroke.getLineWidth(), basicStroke.getEndCap(),
                    basicStroke.getLineJoin(), basicStroke.getMiterLimit(), new float[] { 1.0f, 1.0f },
                    basicStroke.getDashPhase());
            break;
        }
        case DASHED: {
            basicStroke = new BasicStroke(basicStroke.getLineWidth(), basicStroke.getEndCap(),
                    basicStroke.getLineJoin(), basicStroke.getMiterLimit(), new float[] { 10.0f, 10.0f },
                    basicStroke.getDashPhase());
            break;
        }
        }
    }

    return basicStroke;
}

From source file:edu.ucla.stat.SOCR.motionchart.MotionBubbleRenderer.java

/**
 * Returns the stroke used to outline data items.  The default
 * implementation passes control to the//from ww w  .  ja va 2  s  .c o  m
 * {@link #lookupSeriesOutlineStroke(int)} method. You can override this
 * method if you require different behaviour.
 *
 * @param row    the row (or series) index (zero-based).
 * @param column the column (or category) index (zero-based).
 * @return The stroke (never <code>null</code>).
 */
@Override
public Stroke getItemOutlineStroke(int row, int column) {
    Number z = dataset.getZ(row, column);

    BasicStroke stroke = DEFAULT_OUTLINE_STROKE;

    if (shouldHighlight(row, column)) {
        stroke = new BasicStroke(2.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL);
    }

    if (z == null) {
        stroke = new BasicStroke(stroke.getLineWidth(), stroke.getEndCap(), stroke.getLineJoin(),
                stroke.getMiterLimit(), new float[] { 5.0f, 5.0f }, 0.0f);
    }

    return stroke;
}