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

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

Description

_more_

License

Open Source License

Parameter

Parameter Description
g _more_
x1 _more_
y1 _more_
x2 _more_
y2 _more_
lineWidth _more_

Declaration

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

Method Source Code

//package com.java2s;
/**//from  w ww  . jav  a2s. c o m
* Copyright (c) 2008-2015 Geode Systems LLC
* This Software is licensed under the Geode Systems RAMADDA License available in the source distribution in the file 
* ramadda_license.txt. The above copyright notice shall be included in all copies or substantial portions of the Software.
*/

import java.awt.*;

public class Main {
    /**
     * _more_
     *
     * @param g _more_
     * @param x1 _more_
     * @param y1 _more_
     * @param x2 _more_
     * @param y2 _more_
     * @param lineWidth _more_
     */
    public static void drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth) {
        if (lineWidth == 1) {
            g.drawLine(x1, y1, x2, y2);
        } else {
            g.drawLine(x1, y1, x2, y2);
            double halfWidth = ((double) lineWidth) / 2.0;
            double deltaX = (double) (x2 - x1);
            double deltaY = (double) (y2 - y1);
            double angle = ((x1 == x2) ? Math.PI : Math.atan(deltaY / deltaX) + Math.PI / 2);
            int xOffset = (int) (halfWidth * Math.cos(angle));
            int yOffset = (int) (halfWidth * Math.sin(angle));
            int[] xCorners = { x1 - xOffset, x2 - xOffset + 1, x2 + xOffset + 1, x1 + xOffset };
            int[] yCorners = { y1 - yOffset, y2 - yOffset, y2 + yOffset + 1, y1 + yOffset + 1 };
            g.fillPolygon(xCorners, yCorners, 4);

            Color c = g.getColor();
            g.setColor(Color.red);
            int hw = (int) halfWidth;
            int dw = hw * 2;
            //      g.fillArc(x1-hw,y1-hw,dw,dw,0,360);
            //      g.drawLine(x1,y1,x2,y2);
            g.setColor(c);

        }
    }

    /**
     * _more_
     *
     * @param value _more_
     *
     * @return _more_
     */
    public static Color getColor(String value) {
        return getColor(value, Color.black);
    }

    /**
     * _more_
     *
     * @param value _more_
     * @param dflt _more_
     *
     * @return _more_
     */
    public static Color getColor(String value, Color dflt) {
        if (value == null) {
            return dflt;
        }
        value = value.trim();
        if (value.equals("null")) {
            return null;
        }
        String s = value;
        String lookFor = ",";
        int i1 = s.indexOf(lookFor);
        if (i1 < 0) {
            lookFor = " ";
            i1 = s.indexOf(lookFor);
        }
        if (i1 > 0) {
            String red = s.substring(0, i1);
            s = s.substring(i1 + 1).trim();
            int i2 = s.indexOf(lookFor);
            if (i2 > 0) {
                String green = s.substring(0, i2);
                String blue = s.substring(i2 + 1);
                try {
                    return new Color(Integer.decode(red).intValue(), Integer.decode(green).intValue(),
                            Integer.decode(blue).intValue());
                } catch (Exception exc) {
                    System.err.println("Bad color:" + value);
                }
            }
        }

        try {
            return new Color(Integer.decode(s).intValue());
        } catch (Exception e) {
            s = s.toLowerCase();
            if (s.equals("blue")) {
                return Color.blue;
            }
            if (s.equals("black")) {
                return Color.black;
            }
            if (s.equals("red")) {
                return Color.red;
            }
            if (s.equals("gray")) {
                return Color.gray;
            }
            if (s.equals("lightgray")) {
                return Color.lightGray;
            }
            if (s.equals("white")) {
                return Color.white;
            }
            if (s.equals("green")) {
                return Color.green;
            }
            if (s.equals("orange")) {
                return Color.orange;
            }

            return dflt;
        }
    }

    /**
     * _more_
     *
     * @param source _more_
     * @param patterns _more_
     *
     * @return _more_
     */
    public static int indexOf(String source, String[] patterns) {
        int idx = -1;
        for (int i = 0; i < patterns.length; i++) {
            int tmpIdx = source.indexOf(patterns[i]);
            if ((idx == -1) || ((tmpIdx >= 0) && (tmpIdx < idx))) {
                idx = tmpIdx;
            }
        }

        return idx;
    }
}

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, boolean thick)
  3. drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)
  4. drawLine(Graphics render, int row, String text)