Example usage for java.awt Graphics2D drawLine

List of usage examples for java.awt Graphics2D drawLine

Introduction

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

Prototype

public abstract void drawLine(int x1, int y1, int x2, int y2);

Source Link

Document

Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.

Usage

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);/*w ww . j a  va 2  s . c om*/

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

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    float[] dash3 = { 4f, 0f, 2f };

    BasicStroke bs3 = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dash3, 2f);

    g2d.setStroke(bs3);//from  w w  w  .  j a  v  a 2s . co  m
    g2d.drawLine(20, 60, 250, 60);
}

From source file:LinesDashes4.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    float[] dash4 = { 4f, 4f, 1f };

    BasicStroke bs4 = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dash4, 2f);

    g2d.setStroke(bs4);// w ww  . j a  va2s  .co m
    g2d.drawLine(20, 20, 250, 20);
}

From source file:TexturedText.java

/** Construct the object */
public TexturedText() {
    super();/*from  w  ww.  j  a  v  a  2s  .  com*/
    setBackground(Color.white);
    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
        g2.setPaint(colors[(i / 2) % colors.length]);
        g2.drawLine(0, i, i, 0);
        g2.drawLine(width - i, height, width, height - i);
    }
    Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());
    tp = new TexturePaint(bim, r);
}

From source file:org.shredzone.commons.captcha.impl.DefaultCaptchaGenerator.java

/**
 * Draws the background grid./*from  w  w  w.j a  v a 2  s  . c o m*/
 */
private void drawGrid(Graphics2D g2d) {
    for (int y = 2; y < height; y += gridSize) {
        g2d.drawLine(0, y, width - 1, y);
    }

    for (int x = 2; x < width; x += gridSize) {
        g2d.drawLine(x, 0, x, height - 1);
    }
}

From source file:juicebox.tools.utils.juicer.apa.APAPlotter.java

/**
 * Plot number value axis for color scale bar.
 *
 * @param g2      graphics2D object//from  www.java2s. c o  m
 * @param heatMap object
 */
private static void plotColorScaleValues(Graphics2D g2, HeatChart heatMap) {
    // size, increment calculations
    double valIncrement = Math.max(heatMap.getDataRange() / ((double) numDivisions), epsilon);
    double depthIncrement = ((double) (fullHeight - 2 * colorScaleVerticalMargin)) / ((double) numDivisions);
    int verticalDepth = fullHeight - colorScaleVerticalMargin;
    int csBarRightEdgeX = fullWidth - colorScaleHorizontalMargin - extraWidthBuffer;

    // formatting
    g2.setFont(heatMap.getAxisValuesFont());
    DecimalFormat df = new DecimalFormat("0.#");

    // draw each tick mark and its value
    for (double i = heatMap.getLowValue(); i <= heatMap
            .getHighValue(); i += valIncrement, verticalDepth -= depthIncrement) {

        if (i > heatMap.getHighValue() - epsilon)
            verticalDepth = colorScaleVerticalMargin;
        g2.drawString(df.format(i), csBarRightEdgeX + 5, verticalDepth); // value
        g2.drawLine(csBarRightEdgeX - 5, verticalDepth, csBarRightEdgeX, verticalDepth); // tick mark
    }
}

From source file:SimpleAttributes.java

public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setBackground(Color.GRAY);
    g2d.clearRect(0, 0, getWidth(), getHeight());

    // String and line with default attributes
    g2d.drawString("Default Font", 10, 20);
    g2d.drawLine(10, 22, 80, 22);

    // Change the font, foreground color, and Stroke
    g2d.setFont(g.getFont().deriveFont(Font.BOLD | Font.ITALIC, 24f));
    g2d.setColor(Color.WHITE);/*from   w ww  .java  2s  . c  o  m*/
    g2d.setStroke(new BasicStroke(10f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));

    // String and line with new attributes
    g2d.drawString("New Font", 10, 50);
    g2d.drawLine(10, 57, 120, 57);
    g2d.dispose();
}

From source file:Points.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(Color.red);//from w ww .j a v a 2 s.c o m

    for (int i = 0; i <= 100000; i++) {
        Dimension size = getSize();
        int w = size.width;
        int h = size.height;

        Random r = new Random();
        int x = Math.abs(r.nextInt()) % w;
        int y = Math.abs(r.nextInt()) % h;
        g2d.drawLine(x, y, x, y);
    }
}

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 ww. ja  v a 2s.co  m*/

    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.refractions.udig.catalog.wmsc.server.AbstractTileRange.java

protected BufferedImage createErrorImage() {
    BufferedImage bf = new BufferedImage(tileset.getWidth(), tileset.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bf.createGraphics();
    g.setColor(Color.RED);/*w  w w. ja va  2s  .c om*/
    g.drawLine(0, 0, tileset.getWidth(), tileset.getHeight());
    g.drawLine(0, tileset.getHeight(), tileset.getWidth(), 0);
    return bf;
}