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:Main.java

public void drawImage() {
    Graphics2D g = img.createGraphics();
    RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHints(hints);//from  w  w w.ja v  a 2  s.  c om

    g.setColor(Color.RED);
    int x = (int) mouse.getX();
    int y = (int) mouse.getY();
    g.setStroke(new BasicStroke(2));
    int s = 3;
    g.drawLine(x - s, y, x + s, y);
    g.drawLine(x, y - s, x, y + s);
    l.setIcon(new ImageIcon(img));

    g.dispose();
}

From source file:net.sf.fspdfs.chartthemes.spring.ScaledDialPointer.java

/**
 * Draws the pointer./*from w w  w .j  a  v a2  s  . c o m*/
 *
 * @param g2  the graphics target.
 * @param plot  the plot.
 * @param frame  the dial's reference frame.
 * @param view  the dial's view.
 */
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    g2.setStroke(new BasicStroke(1.0f));
    Rectangle2D lengthRect = DialPlot.rectangleByRadius(frame, this.getRadius(), this.getRadius());
    Rectangle2D widthRect = DialPlot.rectangleByRadius(frame, this.getWidthRadius(), this.getWidthRadius());
    double value = ChartThemesUtilities.getScaledValue(plot.getValue(this.getDatasetIndex()), scale);
    DialScale scale = plot.getScaleForDataset(this.getDatasetIndex());
    double angle = scale.valueToAngle(value);

    Arc2D arc1 = new Arc2D.Double(lengthRect, angle, 0, Arc2D.OPEN);
    Point2D pt1 = arc1.getEndPoint();
    Arc2D arc2 = new Arc2D.Double(widthRect, angle - 90.0, 180.0, Arc2D.OPEN);
    Point2D pt2 = arc2.getStartPoint();
    Point2D pt3 = arc2.getEndPoint();
    Arc2D arc3 = new Arc2D.Double(widthRect, angle - 180.0, 0.0, Arc2D.OPEN);
    Point2D pt4 = arc3.getStartPoint();

    GeneralPath gp = new GeneralPath();
    gp.moveTo((float) pt1.getX(), (float) pt1.getY());
    gp.lineTo((float) pt2.getX(), (float) pt2.getY());
    gp.lineTo((float) pt4.getX(), (float) pt4.getY());
    gp.lineTo((float) pt3.getX(), (float) pt3.getY());
    gp.closePath();
    g2.setPaint(this.fillPaint);
    g2.fill(gp);

    g2.setPaint(this.getOutlinePaint());
    Line2D line = new Line2D.Double(frame.getCenterX(), frame.getCenterY(), pt1.getX(), pt1.getY());
    //      g2.draw(line);

    line.setLine(pt2, pt3);
    g2.draw(line);

    line.setLine(pt3, pt1);
    g2.draw(line);

    line.setLine(pt2, pt1);
    g2.draw(line);

    line.setLine(pt2, pt4);
    g2.draw(line);

    line.setLine(pt3, pt4);
    g2.draw(line);
}

From source file:net.sf.jasperreports.chartthemes.spring.ScaledDialPointer.java

/**
 * Draws the pointer.//from  w  ww  .  ja  v  a 2  s .  c  o  m
 *
 * @param g2  the graphics target.
 * @param plot  the plot.
 * @param frame  the dial's reference frame.
 * @param view  the dial's view.
 */
@Override
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    g2.setStroke(new BasicStroke(1.0f));
    Rectangle2D lengthRect = DialPlot.rectangleByRadius(frame, this.getRadius(), this.getRadius());
    Rectangle2D widthRect = DialPlot.rectangleByRadius(frame, this.getWidthRadius(), this.getWidthRadius());
    double value = ChartThemesUtilities.getScaledValue(plot.getValue(this.getDatasetIndex()), scale);
    DialScale scale = plot.getScaleForDataset(this.getDatasetIndex());
    double angle = scale.valueToAngle(value);

    Arc2D arc1 = new Arc2D.Double(lengthRect, angle, 0, Arc2D.OPEN);
    Point2D pt1 = arc1.getEndPoint();
    Arc2D arc2 = new Arc2D.Double(widthRect, angle - 90.0, 180.0, Arc2D.OPEN);
    Point2D pt2 = arc2.getStartPoint();
    Point2D pt3 = arc2.getEndPoint();
    Arc2D arc3 = new Arc2D.Double(widthRect, angle - 180.0, 0.0, Arc2D.OPEN);
    Point2D pt4 = arc3.getStartPoint();

    GeneralPath gp = new GeneralPath();
    gp.moveTo((float) pt1.getX(), (float) pt1.getY());
    gp.lineTo((float) pt2.getX(), (float) pt2.getY());
    gp.lineTo((float) pt4.getX(), (float) pt4.getY());
    gp.lineTo((float) pt3.getX(), (float) pt3.getY());
    gp.closePath();
    g2.setPaint(this.fillPaint);
    g2.fill(gp);

    g2.setPaint(this.getOutlinePaint());
    Line2D line = new Line2D.Double(frame.getCenterX(), frame.getCenterY(), pt1.getX(), pt1.getY());
    //      g2.draw(line);

    line.setLine(pt2, pt3);
    g2.draw(line);

    line.setLine(pt3, pt1);
    g2.draw(line);

    line.setLine(pt2, pt1);
    g2.draw(line);

    line.setLine(pt2, pt4);
    g2.draw(line);

    line.setLine(pt3, pt4);
    g2.draw(line);
}

From source file:org.kalypso.kalypsomodel1d2d.ui.map.channeledit.DrawBanklineWidget.java

@Override
public void paint(final Graphics g) {
    final IMapPanel mapPanel = getMapPanel();
    if (mapPanel == null)
        return;//from  ww w . j a v  a  2  s.  c o m

    super.paint(g);

    // if edit mode is active, paint edited line
    if (m_edit && m_bankline != null && m_lineEditor != null)
        m_lineEditor.paint(g);
    else {
        // paint drawn line
        if (m_currentPos != null) {
            final Graphics2D g2 = (Graphics2D) g;

            g2.setStroke(new BasicStroke(1));
            g2.setColor(new Color(255, 100, 100));

            final Point currentPoint = MapUtilities.retransform(getMapPanel(), m_currentPos);
            if (m_lineBuilder != null)
                m_lineBuilder.paint(g2, mapPanel.getProjection(), currentPoint);
        }
    }

    final Rectangle bounds = mapPanel.getScreenBounds();

    final String tooltipMsg = getTooltipMessage();

    m_toolTipRenderer.setTooltip(tooltipMsg);
    m_toolTipRenderer.paintToolTip(new Point(5, bounds.height - 5), g, bounds);

    // TODO: validate current line and set warning message
    m_warningRenderer.paintToolTip(new Point(5, bounds.height - 80), g, bounds);
}

From source file:com.joliciel.jochre.search.highlight.ImageSnippet.java

public BufferedImage getImage() {
    try {//from   w ww . ja va 2 s.  c om
        BufferedImage originalImage = ImageIO.read(imageFile);
        BufferedImage imageSnippet = new BufferedImage(this.rectangle.getWidth(), this.rectangle.getHeight(),
                BufferedImage.TYPE_INT_ARGB);
        originalImage = originalImage.getSubimage(this.rectangle.getLeft(), this.rectangle.getTop(),
                this.rectangle.getWidth(), this.rectangle.getHeight());
        Graphics2D graphics2D = imageSnippet.createGraphics();
        graphics2D.drawImage(originalImage, 0, 0, this.rectangle.getWidth(), this.rectangle.getHeight(), null);
        int extra = 2;
        for (Rectangle rect : this.highlights) {
            graphics2D.setStroke(new BasicStroke(1));
            graphics2D.setPaint(Color.BLACK);
            graphics2D.drawRect(rect.getLeft() - this.rectangle.getLeft() - extra,
                    rect.getTop() - this.rectangle.getTop() - extra, rect.getWidth() + (extra * 2),
                    rect.getHeight() + (extra * 2));
            graphics2D.setColor(new Color(255, 255, 0, 127));
            graphics2D.fillRect(rect.getLeft() - this.rectangle.getLeft() - extra,
                    rect.getTop() - this.rectangle.getTop() - extra, rect.getWidth() + (extra * 2),
                    rect.getHeight() + (extra * 2));
        }
        return imageSnippet;
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:org.polymap.core.data.feature.FeatureRenderProcessor2.java

protected void drawErrorMsg(Graphics2D g, String msg, Throwable e) {
    g.setColor(Color.RED);//from   ww  w.  j ava 2  s . c o m
    g.setStroke(new BasicStroke(1));
    g.getFont().deriveFont(Font.BOLD, 12);
    if (msg != null) {
        g.drawString(msg, 10, 10);
    }
    if (e != null) {
        g.drawString(e.toString(), 10, 30);
    }
}

From source file:adams.gui.visualization.stats.paintlet.Exponential.java

/**
 * The paint routine of the paintlet.//  www  .  j a v a2s .c  o m
 *
 * @param g      the graphics context to use for painting
 * @param moment   what {@link PaintMoment} is currently being painted
 */
@Override
protected void doPerformPaint(Graphics g, PaintMoment moment) {
    if ((m_Data != null) && (m_Sorted != null)) {
        GUIHelper.configureAntiAliasing(g, m_AntiAliasingEnabled);

        for (int i = 0; i < m_Sorted.length; i++) {
            Graphics2D g2d = (Graphics2D) g;
            //If data points are to be filled
            if (m_Fill) {
                g2d.setColor(m_FillColor);
                g2d.setStroke(new BasicStroke(0));
                g2d.fillOval(m_AxisBottom.valueToPos(m_Sorted[i]) - m_Size / 2,
                        m_AxisLeft.valueToPos(m_TransformedY[i]) - m_Size / 2, m_Size, m_Size);
            }
            //outline of data point
            g2d.setStroke(new BasicStroke(m_StrokeThickness));
            g2d.setColor(m_Color);
            g2d.drawOval(m_AxisBottom.valueToPos(m_Sorted[i]) - m_Size / 2,
                    m_AxisLeft.valueToPos(m_TransformedY[i]) - m_Size / 2, m_Size, m_Size);
        }
        //if drawing regression fit diagonal
        if (m_RegressionLine) {
            g.setColor(Color.BLACK);
            double[] newData = new double[m_Sorted.length];
            for (int i = 0; i < m_Sorted.length; i++) {
                newData[i] = Math.log(m_Sorted[i]);
            }
            ExponentialDistributionImpl ex = new ExponentialDistributionImpl(StatUtils.mean(newData));
            //draw the expected diagonal line using the exponential distribution
            for (int i = 0; i < m_Sorted.length - 1; i++) {
                double prob1;
                try {
                    prob1 = ex.cumulativeProbability(newData[i]);
                } catch (MathException e) {
                    prob1 = 0;
                }
                double prob2;
                try {
                    prob2 = ex.cumulativeProbability(newData[i + 1]);
                } catch (MathException e) {
                    prob2 = 0;
                }
                double p1 = -Math.log(1 - prob1);
                double p2 = -Math.log(1 - prob2);
                g.drawLine(m_AxisBottom.valueToPos(m_Sorted[i]), m_AxisLeft.valueToPos(p1),
                        m_AxisBottom.valueToPos(m_Sorted[i + 1]), m_AxisLeft.valueToPos(p2));
            }
        }
    }
}

From source file:PaintingAndStroking.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    double x = 15, y = 50, w = 70, h = 70;
    Ellipse2D e = new Ellipse2D.Double(x, y, w, h);
    GradientPaint gp = new GradientPaint(75, 75, Color.white, 95, 95, Color.gray, true);
    // Fill with a gradient.
    g2.setPaint(gp);/*from ww  w. j  av  a 2s .  com*/
    g2.fill(e);
    // Stroke with a solid color.
    e.setFrame(x + 100, y, w, h);
    g2.setPaint(Color.black);
    g2.setStroke(new BasicStroke(8));
    g2.draw(e);
    // Stroke with a gradient.
    e.setFrame(x + 200, y, w, h);
    g2.setPaint(gp);
    g2.draw(e);
}

From source file:com.joliciel.jochre.graphics.VectorizerImpl.java

public BufferedImage drawArrayLists(JochreImage jochreImage) {
    long startTime = (new Date()).getTime();
    BufferedImage vectorizedImage = new BufferedImage(jochreImage.getWidth(), jochreImage.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics2D = vectorizedImage.createGraphics();
    graphics2D.setStroke(new BasicStroke(1));
    graphics2D.setPaint(Color.BLACK);

    for (Paragraph paragraph : jochreImage.getParagraphs()) {
        for (RowOfShapes row : paragraph.getRows()) {
            for (GroupOfShapes group : row.getGroups()) {
                for (Shape shape : group.getShapes()) {
                    List<LineSegment> lines = this.vectorize(shape);

                    for (LineSegment line : lines)
                        graphics2D.drawLine(shape.getLeft() + line.getStartX(),
                                shape.getTop() + line.getStartY(), shape.getLeft() + line.getEndX(),
                                shape.getTop() + line.getEndY());
                }//www . j a v  a 2 s. c  o m
            }
        }
    }

    if (LOG.isDebugEnabled()) {
        long endTime = (new Date()).getTime();
        long diff = endTime - startTime;
        LOG.debug("Time elapsed: " + ((double) diff / 1000));
    }
    return vectorizedImage;
}

From source file:org.uva.itast.blended.omr.scanners.BarcodeScanner.java

/**
 * @param campo//from w w  w  .j av a  2 s.c  om
 */
public void markBarcode(Field campo) {
    try {
        //get bbox in pixels
        Rectangle rect = pageImage.toPixels(campo.getBBox());
        // expand the area for some tolerance
        Rectangle2D expandedArea = getExpandedArea(campo.getBBox(), (float) BARCODE_AREA_PERCENT);
        Rectangle expandedRect = pageImage.toPixels(expandedArea);

        Graphics2D g = pageImage.getReportingGraphics();
        AffineTransform t = g.getTransform();
        g.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 1,
                new float[] { (float) (3 / t.getScaleX()), (float) (6 / t.getScaleY()) }, 0));
        if (lastResult != null)
            g.setColor(Color.BLUE);
        else
            g.setColor(Color.RED);

        g.drawRoundRect(rect.x, rect.y, rect.width, rect.height, 3, 3);
        g.drawRoundRect(expandedRect.x, expandedRect.y, expandedRect.width, expandedRect.height, 3, 3);

        g.setFont(new Font("Arial", Font.BOLD, (int) (12 / t.getScaleX())));
        String message;
        if (lastResult != null)
            message = ((Result) lastResult.getResult()).getBarcodeFormat().toString() + "="
                    + getParsedCode(lastResult);
        else
            message = "UNRECOGNIZED!";
        g.drawString(message, rect.x, rect.y);

    } catch (Exception e) {
        logger.error("Unexpected errr while logging the image:", e);
    }

}