Example usage for java.awt Graphics2D setStroke

List of usage examples for java.awt Graphics2D setStroke

Introduction

In this page you can find the example usage for java.awt Graphics2D setStroke.

Prototype

public abstract void setStroke(Stroke s);

Source Link

Document

Sets the Stroke for the Graphics2D context.

Usage

From source file:org.esa.snap.graphbuilder.rcp.dialogs.support.GraphNode.java

/**
 * Draws an arrow head at the correct angle
 *
 * @param g     The Java2D Graphics//from  w w w. ja  va 2 s  .c  o m
 * @param tailX position X on target node
 * @param tailY position Y on target node
 * @param headX position X on source node
 * @param headY position Y on source node
 */
private static void drawArrow(final Graphics2D g, final int tailX, final int tailY, final int headX,
        final int headY) {

    final double t1 = Math.abs(headY - tailY);
    final double t2 = Math.abs(headX - tailX);
    double theta = Math.atan(t1 / t2);
    if (headX >= tailX) {
        if (headY > tailY)
            theta = Math.PI + theta;
        else
            theta = -(Math.PI + theta);
    } else if (headX < tailX && headY > tailY)
        theta = 2 * Math.PI - theta;
    final double cosTheta = FastMath.cos(theta);
    final double sinTheta = FastMath.sin(theta);

    final Point p2 = new Point(-8, -3);
    final Point p3 = new Point(-8, +3);

    int x = (int) Math.round((cosTheta * p2.x) - (sinTheta * p2.y));
    p2.y = (int) Math.round((sinTheta * p2.x) + (cosTheta * p2.y));
    p2.x = x;
    x = (int) Math.round((cosTheta * p3.x) - (sinTheta * p3.y));
    p3.y = (int) Math.round((sinTheta * p3.x) + (cosTheta * p3.y));
    p3.x = x;

    p2.translate(tailX, tailY);
    p3.translate(tailX, tailY);

    Stroke oldStroke = g.getStroke();
    g.setStroke(new BasicStroke(2));
    g.drawLine(tailX, tailY, headX, headY);
    g.drawLine(tailX, tailY, p2.x, p2.y);
    g.drawLine(p3.x, p3.y, tailX, tailY);
    g.setStroke(oldStroke);
}

From source file:edu.ku.brc.ui.dnd.SimpleGlassPane.java

/**
 * @param g2/*  ww w . j a  v a  2s  .c om*/
 * @param x
 * @param y
 * @param w
 * @param h
 * @param arcW
 * @param arcH
 */
private void drawBGContainer(final Graphics2D g2, final boolean doFill, final int x, final int y, final int w,
        final int h, final int arcW, final int arcH) {
    Stroke cacheStroke = g2.getStroke();

    g2.setStroke(UIHelper.getStdLineStroke());

    if (UIHelper.isWindows()) {
        if (doFill) {
            //g2.fillRect(x, y, w, h); // Make ugly for Windows
            g2.fillRoundRect(x, y, w, h, 4, 4);
        } else {
            //g2.drawRect(x, y, w, h); // Make ugly for Windows
            g2.drawRoundRect(x, y, w, h, 4, 4);
        }
    } else {
        if (doFill) {
            g2.fillRoundRect(x, y, w, h, arcW, arcH);
        } else {
            g2.drawRoundRect(x, y, w, h, arcW, arcH);
        }
    }
    g2.setStroke(cacheStroke);
}

From source file:figs.treeVisualization.gui.PhyloDateAxis.java

/**
  * Draws an axis line at the current cursor position and edge.
  * /*w  ww .j a v  a2 s .  co  m*/
  * This always uses RectangleEdge.RIGHT, so the cursor is the x position.
  * 
  * @param g2  the graphics device.
  * @param cursor  the cursor position. 
  * @param dataArea  the data area.
  * @param edge  the edge.
  * 
  * Original method is in <code>org.jfree.chart.axis.ValueAxis</code>
  */
protected void drawAxisLine(Graphics2D g2, double cursor, Rectangle2D dataArea) {

    if (!isAxisLineVisible()) {
        // originally in drawTickMarksAndLabels
        return;
    }

    Line2D axisLine = new Line2D.Double(cursor, dataArea.getY(), cursor, dataArea.getHeight());

    g2.setPaint(getAxisLinePaint());
    g2.setStroke(getAxisLineStroke());
    g2.draw(axisLine);

}

From source file:org.esa.s2tbx.dataio.s2.l1b.L1bSceneDescription.java

public BufferedImage createTilePicture(int width) {

    Color[] colors = new Color[] { Color.GREEN, Color.RED, Color.BLUE, Color.YELLOW };

    double scale = width / sceneRectangle.getWidth();
    int height = (int) Math.round(sceneRectangle.getHeight() * scale);

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = image.createGraphics();
    graphics.scale(scale, scale);/*from   w  w  w.j  a v  a  2s.c  o  m*/
    graphics.translate(-sceneRectangle.getX(), -sceneRectangle.getY());
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    graphics.setPaint(Color.WHITE);
    graphics.fill(sceneRectangle);
    graphics.setStroke(new BasicStroke(100F));
    graphics.setFont(new Font("Arial", Font.PLAIN, 800));

    for (int i = 0; i < tileInfos.length; i++) {
        Rectangle rect = tileInfos[i].rectangle;
        graphics.setPaint(addAlpha(colors[i % colors.length].brighter(), 100));
        graphics.fill(rect);
    }
    for (int i = 0; i < tileInfos.length; i++) {
        Rectangle rect = tileInfos[i].rectangle;
        graphics.setPaint(addAlpha(colors[i % colors.length].darker(), 100));
        graphics.draw(rect);
        graphics.setPaint(colors[i % colors.length].darker().darker());
        graphics.drawString("Tile " + (i + 1) + ": " + tileInfos[i].id, rect.x + 1200F, rect.y + 2200F);
    }
    return image;
}

From source file:savant.view.tracks.TrackRenderer.java

/**
 * Simplest kind of legend is just a list of coloured lines with names next to them.
 *//*from w  w  w .  ja  v  a 2s .com*/
protected void drawSimpleLegend(Graphics2D g2, int x, int y, ColourKey... keys) {
    ColourScheme cs = (ColourScheme) instructions.get(DrawingInstruction.COLOUR_SCHEME);

    g2.setFont(LEGEND_FONT);

    for (ColourKey k : keys) {
        String legendString = k.getName();
        g2.setColor(cs.getColor(k));
        g2.setStroke(TWO_STROKE);
        Rectangle2D stringRect = LEGEND_FONT.getStringBounds(legendString, g2.getFontRenderContext());
        g2.drawLine(x - 25, y - (int) stringRect.getHeight() / 2, x - 5, y - (int) stringRect.getHeight() / 2);
        g2.setColor(cs.getColor(ColourKey.INTERVAL_LINE));
        g2.setStroke(ONE_STROKE);
        g2.drawString(legendString, x, y);

        y += LEGEND_LINE_HEIGHT;
    }
}

From source file:nl.b3p.imagetool.ImageTool.java

public static BufferedImage drawGeometries(BufferedImage bi, CombineImageSettings settings, int srid, Bbox bbox,
        int width, int height) throws Exception {
    List wktGeoms = settings.getWktGeoms();
    if (wktGeoms == null || wktGeoms.size() <= 0) {
        return bi;
    }/*from  w ww . j a  v a  2  s .  c o m*/
    BufferedImage newBufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
    //        BufferedImage newBufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D gbi = newBufIm.createGraphics();
    gbi.drawImage(bi, 0, 0, null);
    for (int i = 0; i < wktGeoms.size(); i++) {
        gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
        CombineImageWkt ciw = (CombineImageWkt) wktGeoms.get(i);
        Color color = settings.getDefaultWktGeomColor();
        if (ciw.getColor() != null) {
            color = ciw.getColor();
        }
        gbi.setColor(color);
        String wktGeom = ciw.getWktGeom();
        Geometry geom = geometrieFromText(wktGeom, srid);
        Shape shape = createImage(geom, srid, bbox, width, height);
        Point centerPoint = null;
        if (geom instanceof Polygon) {
            gbi.fill(shape);
        } else if (geom instanceof com.vividsolutions.jts.geom.Point) {
            centerPoint = calculateCenter(shape, srid, bbox, width, height);
            gbi.draw(new Ellipse2D.Double(centerPoint.getX(), centerPoint.getY(), 4, 4));
        } else {
            gbi.setStroke(new BasicStroke(3));
            gbi.draw(shape);
        }
        if (ciw.getLabel() != null) {
            if (centerPoint == null) {
                centerPoint = calculateCenter(shape, srid, bbox, width, height);
            }
            gbi.setColor(Color.black);
            gbi.drawString(ciw.getLabel(), (float) centerPoint.getX(), (float) centerPoint.getY());
        }
    }
    gbi.dispose();
    return newBufIm;
}

From source file:org.geotools.renderer.chart.GeometryRenderer.java

public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo plotInfo,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairState, int pass) {

    GeometryDataset gd = (GeometryDataset) dataset;
    Geometry g = gd.getGeometries().get(series);

    g2.setPaint(getItemPaint(series, item));
    g2.setStroke(getItemStroke(series, item));

    if (pass == 0) {
        drawGeometry(g, g2, series, item, dataArea, plot, domainAxis, rangeAxis);
    }/*from w w  w. j  a va2s  . c o m*/

    if (pass == 1 && renderCoordinates) {
        // second pass to render coordinates 
        if (g instanceof Point || g instanceof MultiPoint) {
            //already rendered in pass 0 
            return;
        }

        for (Coordinate c : g.getCoordinates()) {
            drawCoordinate(c, g2, series, item, dataArea, plot, domainAxis, rangeAxis);
        }
    }

}

From source file:TapTapTap.java

@Override
public void paint(Graphics g, JComponent c) {
    int w = c.getWidth();
    int h = c.getHeight();

    // Paint the view.
    super.paint(g, c);

    if (!mIsRunning) {
        return;/*from  www.j av a2s.co  m*/
    }

    Graphics2D g2 = (Graphics2D) g.create();

    float fade = (float) mFadeCount / (float) mFadeLimit;
    // Gray it out.
    Composite urComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f * fade));
    g2.fillRect(0, 0, w, h);
    g2.setComposite(urComposite);

    // Paint the wait indicator.
    int s = Math.min(w, h) / 5;
    int cx = w / 2;
    int cy = h / 2;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setStroke(new BasicStroke(s / 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    g2.setPaint(Color.white);
    g2.rotate(Math.PI * mAngle / 180, cx, cy);
    for (int i = 0; i < 12; i++) {
        float scale = (11.0f - (float) i) / 11.0f;
        g2.drawLine(cx + s, cy, cx + s * 2, cy);
        g2.rotate(-Math.PI / 6, cx, cy);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, scale * fade));
    }

    g2.dispose();
}

From source file:org.squidy.designer.zoom.impl.PortShape.java

/**
 * @param paintContext//ww  w . j a  v  a2s. co  m
 */
@Override
protected void paintShape(PPaintContext paintContext) {
    super.paintShape(paintContext);

    Graphics2D g = paintContext.getGraphics();

    PBounds bounds = getBoundsReference();

    int x = (int) bounds.getX();
    int y = (int) bounds.getY();
    int width = (int) bounds.getWidth();
    int height = (int) bounds.getHeight();

    g.setColor(innerColor);
    g.fillOval(x, y, width, width);

    Stroke defaultStroke = g.getStroke();

    g.setStroke(STROKE_PORT);
    g.setColor(Color.BLACK);
    g.drawOval(x, y, width, width);

    g.drawLine(x + 20, height - 20, x + width - 20, height - 20);
    g.drawLine(x + width - 50, height - 40, x + width - 20, height - 20);
    g.drawLine(x + width - 50, height + 0, x + width - 20, height - 20);

    g.setStroke(defaultStroke);

    // //////////////////

    if (isCreatingEdge) {
        Graphics2D g2d = paintContext.getGraphics();

        g2d.setColor(Color.GRAY);

        // // Paint label.
        // if (showLabel) {
        // paintLabel(paintContext);

        // Paint edge if is creating edge.
        if (isCreatingEdge) {
            paintCreatingEdge(paintContext);
        }
    }
}

From source file:org.jax.haplotype.analysis.visualization.SimplePhylogenyTreeImageFactory.java

/**
 * {@inheritDoc}/*ww  w.ja v a  2s . c o m*/
 */
public BufferedImage createPhylogenyImage(PhylogenyTreeNode phylogenyTree, int imageWidth, int imageHeight) {
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Creating phylogeny image for: "
                + phylogenyTree.resolveToSingleStrainLeafNodes(0.0).toNewickFormat());
    }

    BufferedImage bi = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D graphics = bi.createGraphics();
    graphics.setStroke(LINE_STROKE);
    graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setColor(Color.BLACK);

    this.paintPhylogenyTree(graphics, phylogenyTree, imageWidth, imageHeight);

    return bi;
}