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

public static void main(String[] args) throws Exception {
    BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_USHORT_GRAY);
    Graphics2D g2d = bi.createGraphics();
    g2d.setColor(Color.WHITE);/*from   w  ww .  j a  v  a2 s. c om*/
    g2d.fillRect(0, 0, 100, 100);
    g2d.setColor(new Color(255, 123, 0));

    g2d.drawLine(100, 100, 1, 1);

    g2d.dispose();
    BufferedImage scaledImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_USHORT_GRAY);

    Graphics2D graphics2D = scaledImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(bi, 0, 0, 1000, 1000, null);
    graphics2D.dispose();
    ImageIO.write(scaledImage, "png", new File("c:/Java_Dev/Test.png"));
    bi.flush();
}

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 w  w  . ja v a2  s  .c  om*/
        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:org.jcurl.core.swing.RockEditDisplay.java

/**
 * @param g//  ww w  .  j av a  2 s  .  c om
 * @param abs1_dc
 * @param abs2_dc
 */
protected static void lineDC(final Graphics2D g, final Point2D abs1_dc, final Point2D abs2_dc) {
    // perpendicular line for the "strength"
    g.drawLine((int) abs1_dc.getX(), (int) abs1_dc.getY(), (int) abs2_dc.getX(), (int) abs2_dc.getY());
}

From source file:Main.java

static void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
    // x1 and y1 are coordinates of circle or rectangle
    // x2 and y2 are coordinates of circle or rectangle, to this point is directed the arrow
    Graphics2D g = (Graphics2D) g1.create();
    double dx = x2 - x1;
    double dy = y2 - y1;
    double angle = Math.atan2(dy, dx);
    int len = (int) Math.sqrt(dx * dx + dy * dy);
    AffineTransform t = AffineTransform.getTranslateInstance(x1, y1);
    t.concatenate(AffineTransform.getRotateInstance(angle));
    g.transform(t);//from  w  w  w  .  jav  a  2s.  c  om
    g.drawLine(0, 0, len, 0);
    int basePosition = len - ARROW_HEAD_SIZE.width;
    int height = ARROW_HEAD_SIZE.height;
    g.fillPolygon(new int[] { len, basePosition, basePosition, len }, new int[] { 0, -height, height, 0 }, 4);
}

From source file:Main.java

/**
 * Draws a double headed arrow with arrow heads of a given width and height.
 * /*from   www .  j  a  v a2 s  .c  o m*/
 * @param aG
 *          the canvas to draw on;
 * @param aX1
 *          the starting X position of the arrow;
 * @param aY1
 *          the starting Y position of the arrow;
 * @param aX2
 *          the ending X position of the arrow;
 * @param aY2
 *          the ending Y position of the arrow;
 * @param aArrowWidth
 *          the total width of the arrow head;
 * @param aArrowHeight
 *          the total height of the arrow head.
 */
public static final void drawDoubleHeadedArrow(final Graphics aG, final int aX1, final int aY1, final int aX2,
        final int aY2, final int aArrowWidth, final int aArrowHeight) {
    final Graphics2D g2d = (Graphics2D) aG.create();

    final int lineWidth = Math.abs(aX2 - aX1);
    final int threshold = (2 * aArrowWidth) + 2;
    try {
        int x1 = aX1;
        int x2 = aX2;

        if (lineWidth > threshold) {
            drawArrowHead(g2d, aX1, aY1, LEFT_FACING, aArrowWidth, aArrowHeight);
            // why x2 needs to be shifted by one pixel is beyond me...
            drawArrowHead(g2d, aX2 + 1, aY2, RIGHT_FACING, aArrowWidth, aArrowHeight);

            x1 += aArrowWidth - 1;
            x2 -= aArrowWidth + 1;
        }

        g2d.drawLine(x1, aY1, x2, aY2);
    } finally {
        g2d.dispose();
    }
}

From source file:com.simiacryptus.mindseye.applications.ArtistryUtil.java

/**
 * Paint lines./*from  w  w w  .  j av  a 2s.  co  m*/
 *
 * @param canvas the canvas
 */
public static void paint_Lines(final Tensor canvas) {
    BufferedImage originalImage = canvas.toImage();
    BufferedImage newImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = (Graphics2D) newImage.getGraphics();
    IntStream.range(0, 100).forEach(i -> {
        Random random = new Random();
        graphics.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
        graphics.drawLine(random.nextInt(originalImage.getWidth()), random.nextInt(originalImage.getHeight()),
                random.nextInt(originalImage.getWidth()), random.nextInt(originalImage.getHeight()));
    });
    canvas.set(Tensor.fromRGB(newImage));
}

From source file:edu.ku.brc.ui.GraphicsUtils.java

/**
 * Draws an arrow from <code>(xCenter,yCenter)</code> to <code>(x,y)</code>.
 * Code stolen from http://forum.java.sun.com/thread.jspa?threadID=378460&tstart=135.
 * /*from  www. j  ava  2s .c o m*/
 * @param g the graphics context to draw in
 * @param headSize the size of the arrow head
 * @param xCenter the x-coord of the arrow tail
 * @param yCenter the y-coord of the arrow tail
 * @param x the x-coord of the arrow head's tip
 * @param y the y-coord of the arrow head's tip
 * @param stroke the <code>Stroke</code> to use
 */
public static void drawArrow(Graphics g, int xCenter, int yCenter, int x, int y, int headSize, float stroke) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.addRenderingHints(hints);

    double aDir = Math.atan2(xCenter - x, yCenter - y);
    Stroke origStroke = g2d.getStroke();
    g2d.setStroke(new BasicStroke(stroke)); // make the arrow head solid even if dash pattern has been specified
    g2d.drawLine(x, y, xCenter, yCenter);
    Polygon tmpPoly = new Polygon();
    int i1 = 2 * headSize + (int) stroke; //(stroke * 2);
    int i2 = headSize + (int) stroke; // make the arrow head the same size regardless of the length length
    tmpPoly.addPoint(x, y); // arrow tip
    tmpPoly.addPoint(x + xCor(i1, aDir + .5), y + yCor(i1, aDir + .5));
    tmpPoly.addPoint(x + xCor(i2, aDir), y + yCor(i2, aDir));
    tmpPoly.addPoint(x + xCor(i1, aDir - .5), y + yCor(i1, aDir - .5));
    tmpPoly.addPoint(x, y); // arrow tip
    g2d.drawPolygon(tmpPoly);
    g2d.fillPolygon(tmpPoly); // remove this line to leave arrow head unpainted
    g2d.setStroke(origStroke);
}

From source file:LinesDashes2.java

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

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawLine(20, 40, 250, 40);
    float[] dash2 = { 1f, 1f, 1f };

    BasicStroke bs2 = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dash2, 2f);
    g2d.setStroke(bs2);/* ww w . j a  va2 s.  c o m*/
    g2d.drawLine(20, 20, 250, 20);
}

From source file:LinesDashes1.java

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

    Graphics2D g2d = (Graphics2D) g;

    float[] dash1 = { 2f, 0f, 2f };

    g2d.drawLine(20, 40, 250, 40);

    BasicStroke bs1 = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dash1, 2f);
    g2d.setStroke(bs1);/*w  w  w .j a  va 2s.c o  m*/
    g2d.drawLine(20, 80, 250, 80);

}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setColor(Color.RED);//from   ww  w  .  j a v  a 2s .c om
    g2d.drawLine(0, 0, getWidth(), getHeight());
    g2d.drawLine(getWidth(), 0, 0, getHeight());
    g2d.dispose();
}