List of usage examples for java.awt BasicStroke getDashArray
public float[] getDashArray()
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(Arrays.toString(stroke.getDashArray())); }
From source file:Main.java
public static void writeStroke(Stroke aStroke, ObjectOutputStream out) throws IOException { if (aStroke instanceof Serializable) { out.writeBoolean(true);/*from w w w.ja v a 2 s. c o m*/ 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: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.//from w w w . j a v a 2 s. com * 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: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. * /*ww w. j ava 2s. c om*/ * @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: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. *//* www . j a va 2 s . co m*/ 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:edu.umass.cs.iesl.pdf2meta.cli.extract.pdfbox.pagedrawer.StrokePath.java
/** * S stroke the path.//from w w w .j a v a 2s. com * @param operator The operator that is being executed. * @param arguments List * * @throws java.io.IOException If an error occurs while processing the font. */ public void process(PDFOperator operator, List<COSBase> arguments) throws IOException { ///dwilson 3/19/07 refactor try { GraphicsAwarePDFStreamEngine drawer = (GraphicsAwarePDFStreamEngine) context; float lineWidth = (float) context.getGraphicsState().getLineWidth(); Matrix ctm = context.getGraphicsState().getCurrentTransformationMatrix(); if (ctm != null && ctm.getXScale() > 0) { lineWidth = lineWidth * ctm.getXScale(); } BasicStroke stroke = (BasicStroke) drawer.getStroke(); if (stroke == null) { drawer.setStroke(new BasicStroke(lineWidth)); } else { drawer.setStroke(new BasicStroke(lineWidth, stroke.getEndCap(), stroke.getLineJoin(), stroke.getMiterLimit(), stroke.getDashArray(), stroke.getDashPhase())); } drawer.strokePath(); } catch (Exception exception) { log.warn(exception, exception); } }
From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.PlotInstanceLegendCreator.java
private CustomLegendItem createValueSourceLegendItem(PlotConfiguration plotConfig, ValueSource valueSource) { Set<PlotDimension> dimensions = new HashSet<PlotDimension>(); for (PlotDimension dimension : PlotDimension.values()) { switch (dimension) { case DOMAIN: case VALUE: break; default:/* ww w . jav a 2 s. co m*/ if (valueSource.useSeriesFormatForDimension(plotConfig, dimension)) { dimensions.add(dimension); } } } if (dimensions.isEmpty()) { return null; } SeriesFormat format = valueSource.getSeriesFormat(); String description = ""; String toolTipText = ""; String urlText = ""; boolean shapeVisible = true; Shape shape; boolean shapeFilled = true; Paint fillPaint = UNDEFINED_COLOR_PAINT; boolean shapeOutlineVisible = true; Paint outlinePaint = PlotConfiguration.DEFAULT_OUTLINE_COLOR; Stroke outlineStroke = DEFAULT_OUTLINE_STROKE; boolean lineVisible = format.getLineStyle() != LineStyle.NONE && format.getSeriesType() == SeriesFormat.VisualizationType.LINES_AND_SHAPES; // configure fill paint and line paint Paint linePaint; String label = valueSource.toString(); if (label == null) { label = ""; } if (dimensions.contains(PlotDimension.COLOR)) { Color color = format.getItemColor(); fillPaint = format.getAreaFillPaint(color); linePaint = fillPaint; } else { if (format.getAreaFillStyle() == FillStyle.NONE) { fillPaint = new Color(0, 0, 0, 0); linePaint = fillPaint; } else if (format.getAreaFillStyle() == FillStyle.SOLID) { fillPaint = UNDEFINED_COLOR_PAINT; linePaint = UNDEFINED_LINE_COLOR; } else { fillPaint = format.getAreaFillPaint(UNDEFINED_COLOR); linePaint = fillPaint; } } VisualizationType seriesType = valueSource.getSeriesFormat().getSeriesType(); if (seriesType == VisualizationType.LINES_AND_SHAPES) { if (dimensions.contains(PlotDimension.SHAPE)) { shape = format.getItemShape().getShape(); } else if (dimensions.contains(PlotDimension.COLOR)) { shape = UNDEFINED_SHAPE; } else { shape = UNDEFINED_SHAPE_AND_COLOR; } if (dimensions.contains(PlotDimension.SIZE)) { AffineTransform transformation = new AffineTransform(); double scalingFactor = format.getItemSize(); transformation.scale(scalingFactor, scalingFactor); shape = transformation.createTransformedShape(shape); } } else if (seriesType == VisualizationType.BARS) { shape = BAR_SHAPE; } else if (seriesType == VisualizationType.AREA) { shape = AREA_SHAPE; } else { throw new RuntimeException("Unknown SeriesType. This should not happen."); } // configure line shape float lineLength = 0; if (lineVisible) { lineLength = format.getLineWidth(); if (lineLength < 1) { lineLength = 1; } if (lineLength > 1) { lineLength = 1 + (float) Math.log(lineLength) / 2; } // line at least 30 pixels long, and show at least 2 iterations of stroke lineLength = Math.max(lineLength * 30, format.getStrokeLength() * 2); // line at least 2x longer than shape width if (shape != null) { lineLength = Math.max(lineLength, (float) shape.getBounds().getWidth() * 2f); } } // now create line shape and stroke Shape line = new Line2D.Float(0, 0, lineLength, 0); BasicStroke lineStroke = format.getStroke(); if (lineStroke == null) { lineStroke = new BasicStroke(); } // unset line ending decoration to prevent drawing errors in legend { BasicStroke s = lineStroke; lineStroke = new BasicStroke(s.getLineWidth(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, s.getMiterLimit(), s.getDashArray(), s.getDashPhase()); } return new CustomLegendItem(label, description, toolTipText, urlText, shapeVisible, shape, shapeFilled, fillPaint, shapeOutlineVisible, outlinePaint, outlineStroke, lineVisible, line, lineStroke, linePaint); }
From source file:org.moeaframework.analysis.plot.Plot.java
/** * Modifies the line thickness or point size in the last dataset. The * size is applied to all series in the dataset. * /* www. j a va 2 s. co m*/ * @param size the size * @return a reference to this {@code Plot} instance */ public Plot withSize(float size) { if (chart.getPlot() instanceof XYPlot) { XYPlot plot = chart.getXYPlot(); XYItemRenderer renderer = plot.getRenderer(currentDataset); if (renderer instanceof XYDotRenderer) { ((XYDotRenderer) renderer).setDotWidth((int) (size * 2)); ((XYDotRenderer) renderer).setDotHeight((int) (size * 2)); } else if (renderer.getBaseStroke() instanceof BasicStroke) { BasicStroke oldStroke = (BasicStroke) renderer.getBaseStroke(); BasicStroke newStroke = new BasicStroke(size, oldStroke.getEndCap(), oldStroke.getLineJoin(), oldStroke.getMiterLimit(), oldStroke.getDashArray(), oldStroke.getDashPhase()); renderer.setBaseStroke(newStroke); } else { renderer.setBaseStroke(new BasicStroke(size, 1, 1)); } } return this; }
From source file:com.mgmtp.perfload.loadprofiles.ui.AppFrame.java
private void updateGraph() { XYSeriesCollection dataset = new XYSeriesCollection(); Collection<Stairs> loadProfileEnities = transform( filter(loadProfilesController.getTreeItems(), new IsStairsPredicate()), new LoadProfileEntityToStairsFunction()); GraphPointsCalculator calc = new GraphPointsCalculator(); Map<String, Set<Point>> pointsMap = calc.calculatePoints(loadProfileEnities); for (Entry<String, Set<Point>> entry : pointsMap.entrySet()) { final XYSeries series = new XYSeries(entry.getKey()); for (Point point : entry.getValue()) { series.add(point.getX(), point.getY()); }/*from w ww . jav a 2 s. c om*/ dataset.addSeries(series); } String name = txtName.getText(); chart = ChartFactory.createXYLineChart(name, "t (min)", "Executions (1/h)", dataset, PlotOrientation.VERTICAL, true, true, false); XYPlot plot = chart.getXYPlot(); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); plot.setRenderer(renderer); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis(); domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); double maxX = 0; for (OneTime oneTime : getOneTimes()) { String key = oneTime.operation.getName(); XYSeries series; try { // We need the series in order to retrieve paint and stroke series = dataset.getSeries(key); } catch (UnknownKeyException ex) { series = new XYSeries(key); dataset.addSeries(series); } int index = dataset.getSeriesIndex(key); BasicStroke stroke = (BasicStroke) renderer.lookupSeriesStroke(index); stroke = new BasicStroke(stroke.getLineWidth() + 1f, stroke.getEndCap(), stroke.getLineJoin(), stroke.getMiterLimit(), stroke.getDashArray(), stroke.getDashPhase()); Color paint = (Color) renderer.lookupSeriesPaint(index); paint = new Color(paint.getRed(), paint.getGreen(), paint.getBlue(), 160); double height = rangeAxis.getUpperBound() * .05; // five percent of range double width = domainAxis.getUpperBound() * .01; // one percent of range double center = oneTime.t0; double left = center - width / 2; double right = center + width / 2; // We only add annotations for one times, but nothing to the series plot.addAnnotation(new XYPolygonAnnotation(new double[] { left, 0d, center, height, right, 0d }, stroke, paint, paint)); maxX = max(maxX, right); } for (Marker marker : getMarkers()) { IntervalMarker im = new IntervalMarker(marker.left, marker.right); im.setLabel(marker.name); im.setLabelFont(new Font(getFont().getName(), getFont().getStyle(), getFont().getSize() + 1)); im.setLabelAnchor(RectangleAnchor.TOP); im.setLabelOffset(new RectangleInsets(8d, 0d, 0d, 0d)); im.setLabelPaint(Color.BLACK); im.setAlpha(.3f); im.setPaint(Color.WHITE); im.setOutlinePaint(Color.BLACK); im.setOutlineStroke(new BasicStroke(1.0f)); plot.addDomainMarker(im, Layer.BACKGROUND); maxX = max(maxX, marker.right); } if (domainAxis.getUpperBound() < maxX) { domainAxis.setUpperBound(maxX * 1.05); } chartPanel.setChart(chart); }
From source file:org.jfree.experimental.swt.SWTGraphics2D.java
/** * Sets the stroke for this graphics context. For now, this implementation * only recognises the {@link BasicStroke} class. * * @param stroke the stroke (<code>null</code> not permitted). * * @see #getStroke()/*from ww w.ja v a2s. c om*/ */ public void setStroke(Stroke stroke) { if (stroke == null) { throw new IllegalArgumentException("Null 'stroke' argument."); } if (stroke instanceof BasicStroke) { BasicStroke bs = (BasicStroke) stroke; this.gc.setLineWidth((int) bs.getLineWidth()); this.gc.setLineJoin(toSwtLineJoin(bs.getLineJoin())); this.gc.setLineCap(toSwtLineCap(bs.getEndCap())); // set the line style to solid by default this.gc.setLineStyle(SWT.LINE_SOLID); // apply dash style if any float[] dashes = bs.getDashArray(); if (dashes != null) { int[] swtDashes = new int[dashes.length]; for (int i = 0; i < swtDashes.length; i++) { swtDashes[i] = (int) dashes[i]; } this.gc.setLineDash(swtDashes); } } else { throw new RuntimeException("Can only handle 'Basic Stroke' at present."); } }