Java Draw Line awt drawLine(Graphics g, int x1, int y1, int x2, int y2, boolean thick)

Here you can find the source of drawLine(Graphics g, int x1, int y1, int x2, int y2, boolean thick)

Description

draw Line

License

Open Source License

Declaration

public static void drawLine(Graphics g, int x1, int y1, int x2, int y2,
            boolean thick) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Jay Unruh, Stowers Institute for Medical Research.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 ******************************************************************************/

import java.awt.Graphics;

public class Main {
    public static void drawLine(Graphics g, int x1, int y1, int x2, int y2,
            boolean thick) {
        if (thick) {
            drawThickLine(g, x1, y1, x2, y2);
        } else {// ww w .j  a  va  2  s  .  co m
            g.drawLine(x1, y1, x2, y2);
        }
    }

    public static void drawThickLine(Graphics g, int x1, int y1, int x2,
            int y2) {
        float length = (float) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1)
                * (y2 - y1));
        float dx = (x2 - x1) / length;
        float dy = (y2 - y1) / length;
        float xpos = x1;
        float ypos = y1;
        for (int i = 0; i < (int) length; i++) {
            drawDot(g, (int) xpos, (int) ypos);
            xpos += dx;
            ypos += dy;
        }
        drawDot(g, x2, y2);
    }

    public static void drawDot(Graphics g, int x, int y) {
        g.drawLine(x - 1, y - 1, x, y - 1);
        g.drawLine(x - 1, y, x, y);
    }
}

Related

  1. drawLine(Graphics g, int x1, int y1, int x2, int y2)
  2. drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)
  3. drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)
  4. drawLine(Graphics render, int row, String text)