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.jfree.graphics2d.demo.BufferedImageDemo.java

/**
 * Starting point for the demo.//  w  w w.j a v  a 2s. co m
 * 
 * @param args  ignored.
 * 
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    BufferedImage image = new BufferedImage(600, 400, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    ImageIcon icon = new ImageIcon(BufferedImageDemo.class.getResource("jfree_chart_1.jpg"));
    g2.rotate(Math.PI / 12);
    g2.setStroke(new BasicStroke(2.0f));
    g2.setPaint(Color.WHITE);
    g2.fill(new Rectangle(0, 0, 600, 400));
    g2.setPaint(Color.RED);
    g2.draw(new Rectangle(0, 0, 600, 400));
    g2.drawImage(icon.getImage(), 10, 20, null);
    ImageIO.write(image, "png", new File("image-test.png"));
}

From source file:org.jcurl.demo.tactics.CurveShapeDemo.java

public static void main(String[] args) {
    log.info("Version: " + Version.find());
    final R1RNFunction c;
    {/*from   w ww .ja  v  a 2 s .  c  o m*/
        final R1R1Function[] f = new R1R1Function[2];
        final double[] fx = { 200, 150 };
        final double[] fy = { 4, 4, 4, 4, 4 };
        f[0] = new Polynome(fx);
        f[1] = new Polynome(fy);
        c = new CurveFkt(f);
    }
    final CurveShapeDemo frame = new CurveShapeDemo(c);
    frame.setSize(500, 400);
    frame.setVisible(true);
    frame.setContentPane(new JPanel() {

        private static final long serialVersionUID = -3582299332757831635L;

        private final double[] sections = new double[10];

        final Stroke st = new BasicStroke(20, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0);

        public void paintComponent(Graphics g) {
            setBackground(new Color(255, 255, 255));
            super.paintComponent(g);
            setBackground(new Color(255, 255, 255));
            final Graphics2D g2 = (Graphics2D) g;
            g2.scale(0.75, 0.75);
            g2.setPaint(new Color(0, 0, 255));
            g2.setStroke(st);
            g2.drawLine(0, 0, 650, 500);
            g2.setPaint(new Color(255, 170, 0, 128));
            // FIXME g2.draw(CurveShape.approximate(frame.curve, CurveShape.sections(-1, 3, sections)));
        }
    });
}

From source file:Main.java

static public void main(String args[]) throws Exception {
    int width = 200, height = 200;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D ig2 = bi.createGraphics();
    ig2.fillRect(0, 0, width - 1, height - 1);

    BasicStroke stroke = new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    ig2.setPaint(Color.lightGray);
    ig2.setStroke(stroke);
    ig2.draw(new Ellipse2D.Double(0, 0, 100, 100));

    ImageIO.write(bi, "GIF", new File("a.gif"));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");

    final BufferedImage originalImage = ImageIO.read(url);
    int width = originalImage.getWidth();
    int height = originalImage.getHeight();
    final BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = textImage.createGraphics();

    FontRenderContext frc = g.getFontRenderContext();
    Font font = new Font("Arial", Font.BOLD, 50);
    GlyphVector gv = font.createGlyphVector(frc, "java2s.com");

    int xOff = 0;
    int yOff = 50;

    Shape shape = gv.getOutline(xOff, yOff);
    g.setClip(shape);//from ww  w .j  av a 2s .  c o  m
    g.drawImage(originalImage, 0, 0, null);

    g.setStroke(new BasicStroke(2f));
    g.setColor(Color.BLACK);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.draw(shape);
    g.dispose();

    ImageIO.write(textImage, "png", new File("cat-text.png"));

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(textImage)));
        }
    });
}

From source file:Main.java

public static BufferedImage getStrokedImage(BufferedImage bi, Shape shape, int strokeWidth) {
    int w = bi.getWidth();
    int h = bi.getHeight();
    BufferedImage bib = new BufferedImage(w, h, bi.getType());
    Graphics2D g = bib.createGraphics();

    BasicStroke bs = new BasicStroke(strokeWidth);
    g.setStroke(bs);
    Rectangle rect = new Rectangle(0, 0, w, h);
    TexturePaint tp = new TexturePaint(bi, rect);
    g.setPaint(tp);/*from  ww  w .jav a 2s .  c  o m*/
    g.draw(shape);

    g.dispose();
    return bib;
}

From source file:GraphicsUtil.java

static public void switchToWidth(Graphics g, int width) {
    if (g instanceof Graphics2D) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke((float) width));
    }/* w  ww .j a  va  2  s .  c  om*/
}

From source file:net.sqs2.omr.session.logic.PageImageRenderer.java

private static DeskewedImageSource drawCorners(FormMaster master, Point2D[] corners, BufferedImage image,
        Graphics2D g) {
    DeskewedImageSource pageSource = new DeskewedImageSource(image, master.getDeskewGuideCenterPoints(),
            corners);/*from ww w  . j  a  va 2  s. co  m*/
    g.setColor(CORNER_COLOR);
    g.setStroke(new BasicStroke(5));
    double size = 40;
    for (int y = 0; y < 2; y++) {
        int i = y * 2;
        g.fillPolygon(pageSource.createPolygon(new Point2D[] { corners[i],
                new Point2D.Double(corners[i].getX() - 1 * size, corners[i].getY() + 1 * size),
                new Point2D.Double(corners[i].getX() - 1 * size, corners[i].getY() - 1 * size) }));
        i++;
        g.fillPolygon(pageSource.createPolygon(new Point2D[] { corners[i],
                new Point2D.Double(corners[i].getX() + 1 * size, corners[i].getY() - 1 * size),
                new Point2D.Double(corners[i].getX() + 1 * size, corners[i].getY() + 1 * size) }));
    }
    return pageSource;
}

From source file:PaintUtils.java

/** Paints 3 different strokes around a shape to indicate focus.
 * The widest stroke is the most transparent, so this achieves a nice
 * "glow" effect./*from   w w  w.  ja v a2  s  . com*/
 * <P>The catch is that you have to render this underneath the shape,
 * and the shape should be filled completely.
 * 
 * @param g the graphics to paint to
 * @param shape the shape to outline
 * @param biggestStroke the widest stroke to use.
 */
public static void paintFocus(Graphics2D g, Shape shape, int biggestStroke) {
    Color focusColor = getFocusRingColor();
    Color[] focusArray = new Color[] {
            new Color(focusColor.getRed(), focusColor.getGreen(), focusColor.getBlue(), 255),
            new Color(focusColor.getRed(), focusColor.getGreen(), focusColor.getBlue(), 170),
            new Color(focusColor.getRed(), focusColor.getGreen(), focusColor.getBlue(), 110) };
    g.setStroke(new BasicStroke(biggestStroke));
    g.setColor(focusArray[2]);
    g.draw(shape);
    g.setStroke(new BasicStroke(biggestStroke - 1));
    g.setColor(focusArray[1]);
    g.draw(shape);
    g.setStroke(new BasicStroke(biggestStroke - 2));
    g.setColor(focusArray[0]);
    g.draw(shape);
    g.setStroke(new BasicStroke(1));
}

From source file:ala.soils2sat.DrawingUtils.java

public static Rectangle drawString(Graphics g, Font font, String text, int x, int y, int width, int height,
        int align, boolean wrap) {
    g.setFont(font);//from  w w  w . j a  v a 2  s. c  o  m
    FontMetrics fm = g.getFontMetrics(font);

    setPreferredAliasingMode(g);

    Rectangle ret = new Rectangle(0, 0, 0, 0);

    if (text == null) {
        return ret;
    }
    String[] alines = text.split("\\n");
    ArrayList<String> lines = new ArrayList<String>();
    for (String s : alines) {
        if (wrap && fm.stringWidth(s) > width) {
            // need to split this up into multiple lines...
            List<String> splitLines = wrapString(s, fm, width);
            lines.addAll(splitLines);
        } else {
            lines.add(s);
        }
    }
    int numlines = lines.size();
    while (fm.getHeight() * numlines > height) {
        numlines--;
    }
    if (numlines > 0) {
        int maxwidth = 0;
        int minxoffset = y + width;
        int totalheight = (numlines * fm.getHeight());

        int linestart = ((height / 2) - (totalheight / 2));

        if (!wrap) {
            ret.y = y + linestart;
        } else {
            ret.y = y;
            linestart = 0;
        }
        for (int idx = 0; idx < numlines; ++idx) {
            String line = lines.get(idx);
            int stringWidth = fm.stringWidth(line);
            // the width of the label depends on the font :
            // if the width of the label is larger than the item
            if (stringWidth > 0 && width < stringWidth) {
                // We have to truncate the label
                line = clipString(null, fm, line, width);
                stringWidth = fm.stringWidth(line);
            }

            int xoffset = 0;
            int yoffset = linestart + fm.getHeight() - fm.getDescent();
            if (align == TEXT_ALIGN_RIGHT) {
                xoffset = (width - stringWidth);
            } else if (align == TEXT_ALIGN_CENTER) {
                xoffset = (int) Math.round((double) (width - stringWidth) / (double) 2);
            }

            if (xoffset < minxoffset) {
                minxoffset = xoffset;
            }
            g.drawString(line, x + xoffset, y + yoffset);
            if (stringWidth > maxwidth) {
                maxwidth = stringWidth;
            }
            linestart += fm.getHeight();
        }

        ret.width = maxwidth;
        ret.height = totalheight;
        ret.x = x + minxoffset;

        // Debug only...
        if (DEBUG) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setStroke(new BasicStroke(1));
            g.setColor(Color.blue);
            g.drawRect(ret.x, ret.y, ret.width, ret.height);
            g.setColor(Color.green);
            g.drawRect(x, y, width, height);
        }

        return ret;
    }
    return ret;
}

From source file:Main.java

public static void draw(BufferedImage imageBG, BufferedImage imageFG) {
    Ellipse2D.Double ellipse1 = new Ellipse2D.Double(20, 20, 30, 30);
    Ellipse2D.Double ellipse2 = new Ellipse2D.Double(25, 25, 30, 30);
    Area circle = new Area(ellipse1);
    circle.subtract(new Area(ellipse2));

    Graphics2D g = imageBG.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setClip(circle);//from ww w.  ja v  a 2 s . co m
    g.drawImage(imageFG, 0, 0, null);
    g.setClip(null);
    Stroke s = new BasicStroke(2);
    g.setStroke(s);
    g.setColor(Color.BLACK);
    g.draw(circle);
    g.dispose();

    JLabel l = new JLabel(new ImageIcon(imageBG));
    JOptionPane.showMessageDialog(null, l);
}