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:adams.gui.visualization.stats.paintlet.Gamma.java

/**
 * The paint routine of the paintlet./*from  w  ww. ja v  a2s.c om*/
 *
 * @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) && m_Shape != -1.0) {
        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]);
            }
            GammaDistributionImpl gd = new GammaDistributionImpl(m_Shape, m_Scale);
            //draw the expected diagonal line using the gamma distribution
            for (int i = 0; i < m_Sorted.length - 1; i++) {
                double p1;
                try {
                    p1 = gd.cumulativeProbability(newData[i]);
                } catch (MathException e) {
                    p1 = 0;
                }
                double p2;
                try {
                    p2 = gd.cumulativeProbability(newData[i + 1]);
                } catch (MathException e) {
                    p2 = 0;
                }
                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:AntiAlias.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    BufferedImage image = // Create an off-screen image
            new BufferedImage(65, 35, BufferedImage.TYPE_INT_RGB);
    Graphics2D ig = image.createGraphics(); // Get its Graphics for drawing

    // Set the background to a gradient fill. The varying color of
    // the background helps to demonstrate the anti-aliasing effect
    ig.setPaint(new GradientPaint(0, 0, Color.black, 65, 35, Color.white));
    ig.fillRect(0, 0, 65, 35);//from   w w w .  j a  v a  2 s.  c  o  m

    // Set drawing attributes for the foreground.
    // Most importantly, turn on anti-aliasing.
    ig.setStroke(new BasicStroke(2.0f)); // 2-pixel lines
    ig.setFont(new Font("Serif", Font.BOLD, 18)); // 18-point font
    ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
            RenderingHints.VALUE_ANTIALIAS_ON);

    // Now draw pure blue text and a pure red oval
    ig.setColor(Color.blue);
    ig.drawString("Java", 9, 22);
    ig.setColor(Color.red);
    ig.drawOval(1, 1, 62, 32);

    // Finally, scale the image by a factor of 10 and display it
    // in the window. This will allow us to see the anti-aliased pixels
    g.drawImage(image, AffineTransform.getScaleInstance(10, 10), this);

    // Draw the image one more time at its original size, for comparison
    g.drawImage(image, 0, 0, this);
}

From source file:TextureWithBufferedImage.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    Rectangle2D rec1, rec2, rec3, rec4, rec5;
    rec1 = new Rectangle2D.Float(25, 25, 75, 150);
    rec2 = new Rectangle2D.Float(125, 25, 10, 75);
    rec3 = new Rectangle2D.Float(75, 125, 125, 75);
    rec4 = new Rectangle2D.Float(25, 15, 12, 75);
    rec5 = new Rectangle2D.Float(15, 50, 15, 15);

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1);
    g2D.setComposite(ac);//from w  ww  .  j a v a  2  s  .com

    g2D.setStroke(new BasicStroke(5.0f));
    g2D.draw(rec1);
    GradientPaint gp = new GradientPaint(125f, 25f, Color.yellow, 225f, 100f, Color.blue);
    g2D.setPaint(gp);
    g2D.fill(rec2);
    BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
    Graphics2D big = bi.createGraphics();
    big.setColor(Color.magenta);
    big.fillRect(0, 0, 5, 5);
    big.setColor(Color.black);
    big.drawLine(0, 0, 5, 5);
    Rectangle r = new Rectangle(0, 0, 5, 5);
    TexturePaint tp = new TexturePaint(bi, r);

    g2D.setPaint(tp);
    g2D.fill(rec3);
    g2D.setColor(Color.green);
    g2D.fill(rec4);
    g2D.setColor(Color.red);
    g2D.fill(rec5);
}

From source file:Bouncer.java

protected void setStroke(Graphics2D g2) {
    if (mDotted == false)
        return;//  www  .  ja  v a2s  .  c  o  m

    Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 10, new float[] { 4, 4 },
            0);
    g2.setStroke(stroke);
}

From source file:org.jfree.chart.demo.CircleDrawer.java

/**
 * Draws the circle.//www.  ja va  2  s. c  om
 * 
 * @param g2  the graphics device.
 * @param area  the area in which to draw.
 */
public void draw(final Graphics2D g2, final Rectangle2D area) {
    final Ellipse2D ellipse = new Ellipse2D.Double(area.getX(), area.getY(), area.getWidth(), area.getHeight());
    if (this.fillPaint != null) {
        g2.setPaint(this.fillPaint);
        g2.fill(ellipse);
    }
    if (this.outlinePaint != null && this.outlineStroke != null) {
        g2.setPaint(this.outlinePaint);
        g2.setStroke(this.outlineStroke);
        g2.draw(ellipse);
    }

    g2.setPaint(Color.black);
    g2.setStroke(new BasicStroke(1.0f));
    final Line2D line1 = new Line2D.Double(area.getCenterX(), area.getMinY(), area.getCenterX(),
            area.getMaxY());
    final Line2D line2 = new Line2D.Double(area.getMinX(), area.getCenterY(), area.getMaxX(),
            area.getCenterY());
    g2.draw(line1);
    g2.draw(line2);
}

From source file:org.springframework.cloud.stream.app.pose.estimation.processor.PoseEstimateOutputMessageBuilder.java

private byte[] drawPoses(byte[] imageBytes, List<Body> bodies) throws IOException {

    if (bodies != null) {

        BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageBytes));

        Graphics2D g = originalImage.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Stroke stroke = g.getStroke();
        g.setStroke(new BasicStroke(this.poseProperties.getDrawLineWidth()));

        for (Body body : bodies) {
            for (Limb limb : body.getLimbs()) {

                Color limbColor = findLimbColor(body, limb);

                Part from = limb.getFromPart();
                Part to = limb.getToPart();

                if (limb.getLimbType() != Model.LimbType.limb17
                        && limb.getLimbType() != Model.LimbType.limb18) {
                    g.setColor(limbColor);
                    g.draw(new Line2D.Double(from.getNormalizedX(), from.getNormalizedY(), to.getNormalizedX(),
                            to.getNormalizedY()));
                }/*ww w .j a va 2  s.  com*/

                g.setStroke(new BasicStroke(1));
                drawPartOval(from, this.poseProperties.getDrawPartRadius(), g);
                drawPartOval(to, this.poseProperties.getDrawPartRadius(), g);
                g.setStroke(new BasicStroke(this.poseProperties.getDrawLineWidth()));
            }
        }

        g.setStroke(stroke);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(originalImage, IMAGE_FORMAT, baos);
        baos.flush();
        imageBytes = baos.toByteArray();
        baos.close();
        g.dispose();
    }

    return imageBytes;
}

From source file:be.ugent.maf.cellmissy.gui.view.renderer.jfreechart.CompassRenderer.java

@Override
public void drawSeries(Graphics2D g2, Rectangle2D dataArea, PlotRenderingInfo info, PolarPlot plot,
        XYDataset dataset, int seriesIndex) {

    // compute the right color for the paint
    int length = GuiUtils.getAvailableColors().length;
    Color color = GuiUtils.getAvailableColors()[index % length];
    // get all the data points
    int numPoints = dataset.getItemCount(seriesIndex);
    // set STroke and Paint of the graphics
    g2.setStroke(new BasicStroke(1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
    g2.setPaint(new Color(color.getRed(), color.getGreen(), color.getBlue(), 175));

    // iterate through the points of the dataset
    for (int i = 0; i < numPoints; i++) {
        double theta = dataset.getXValue(seriesIndex, i); // the angle at the center         
        double radius = dataset.getYValue(seriesIndex, i); // the frequency

        Point p0 = plot.translateToJava2D(0, 0, plot.getAxis(), dataArea);
        Point p1 = plot.translateToJava2D(theta, radius, plot.getAxis(), dataArea);

        Line2D line = new Line2D.Double(p0, p1);
        g2.draw(line);//w  w w  .j a  v a2  s . c om
    }
}

From source file:MWC.GUI.JFreeChart.StepperXYPlot.java

/**
 * draw the new stepper line into the plot
 * /*from  w  w w.  jav  a2 s. c  om*/
 * @param g2
 * @param linePosition
 * @param dataArea
 */
protected void plotStepperLine(final Graphics2D g2, final double linePosition, final Rectangle2D dataArea) {
    // prepare to draw
    final Stroke oldStroke = g2.getStroke();
    g2.setXORMode(Color.darkGray);

    // thicken up the line
    g2.setStroke(new BasicStroke(3));

    if (this.getOrientation() == PlotOrientation.VERTICAL) {
        // draw the line
        g2.drawLine((int) linePosition - 1, (int) dataArea.getY() + 1, (int) linePosition - 1,
                (int) dataArea.getY() + (int) dataArea.getHeight() - 1);
    } else {
        // draw the line
        g2.drawLine((int) dataArea.getY() + 1, (int) linePosition - 1,
                (int) dataArea.getY() + (int) dataArea.getHeight() - 1, (int) linePosition - 1);

    }

    // and restore everything
    g2.setStroke(oldStroke);
    g2.setPaintMode();
}

From source file:IconDemoApp.java

public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2d = (Graphics2D) g.create();

    g2d.setColor(Color.WHITE);//from ww  w  .  j av a2  s  .c  om
    g2d.fillRect(x + 1, y + 1, width - 2, height - 2);

    g2d.setColor(Color.BLACK);
    g2d.drawRect(x + 1, y + 1, width - 2, height - 2);

    g2d.setColor(Color.RED);

    g2d.setStroke(stroke);
    g2d.drawLine(x + 10, y + 10, x + width - 10, y + height - 10);
    g2d.drawLine(x + 10, y + height - 10, x + width - 10, y + 10);

    g2d.dispose();
}

From source file:com.projity.pm.graphic.graph.GraphInteractor.java

private void drawLinkShadow(double x, double y, boolean alternate) {
    if (x == -1 || y == -1)
        return;/*from  w  w  w . j a  va  2 s  . c  o  m*/
    Graphics2D g = initGraphics();

    Line2D line = new Line2D.Double(x0link, y0link, x, y);
    g.setStroke(new BasicStroke(2));
    g.draw(line);

    if (alternate) {
        lastLinkShadowX = (lastLinkShadowX == -1) ? x : -1;
        lastLinkShadowY = (lastLinkShadowY == -1) ? y : -1;
    }
}