Java Draw Thick Line drawThickLine(Graphics g, int x1, int y1, int x2, int y2, double t)

Here you can find the source of drawThickLine(Graphics g, int x1, int y1, int x2, int y2, double t)

Description

Simulates drawing of different thickness lines by using filled polygon.

License

Open Source License

Parameter

Parameter Description
g Graphics on which to draw
x1 The starting x co-ordinate of line
y1 The starting y co-ordinate of line
x2 The ending x co-ordinate of line
y2 The ending y co-ordinate of line
t The thickness of the line in pixels

Declaration

public static final void drawThickLine(Graphics g, int x1, int y1, int x2, int y2, double t) 

Method Source Code

//package com.java2s;
/*//w w  w  .  j  a va2  s  .  c  o m
Copyright (C) 1996, 1997, 1998 State of California, Department of 
Water Resources.
    
VISTA : A VISualization Tool and Analyzer. 
   Version 1.0beta
   by Nicky Sandhu
California Dept. of Water Resources
Division of Planning, Delta Modeling Section
1416 Ninth Street
Sacramento, CA 95814
(916)-653-7552
nsandhu@water.ca.gov
    
Send bug reports to nsandhu@water.ca.gov
    
This program is licensed to you under the terms of the GNU General
Public License, version 2, as published by the Free Software
Foundation.
    
You should have received a copy of the GNU General Public License
along with this program; if not, contact Dr. Francis Chung, below,
or the Free Software Foundation, 675 Mass Ave, Cambridge, MA
02139, USA.
    
THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED BY THE CALIFORNIA
DEPARTMENT OF WATER RESOURCES AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE CALIFORNIA
DEPARTMENT OF WATER RESOURCES OR ITS CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OR SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
    
For more information about VISTA, contact:
    
Dr. Francis Chung
California Dept. of Water Resources
Division of Planning, Delta Modeling Section
1416 Ninth Street
Sacramento, CA  95814
916-653-5601
chung@water.ca.gov
    
or see our home page: http://wwwdelmod.water.ca.gov/
    
Send bug reports to nsandhu@water.ca.gov or call (916)-653-7552
    
 */

import java.awt.Graphics;

public class Main {
    private static int[] xtl = new int[4], ytl = new int[4];

    /**
     * Simulates drawing of different thickness lines by using filled polygon.
     * 
     * @param g
     *            Graphics on which to draw
     * @param x1
     *            The starting x co-ordinate of line
     * @param y1
     *            The starting y co-ordinate of line
     * @param x2
     *            The ending x co-ordinate of line
     * @param y2
     *            The ending y co-ordinate of line
     * @param t
     *            The thickness of the line in pixels
     */
    public static final void drawThickLine(Graphics g, int x1, int y1, int x2, int y2, double t) {
        double theta;
        if (Math.abs(x2 - x1) > 0.01)
            theta = Math.atan((y2 - y1) / (x2 - x1));
        else
            theta = Math.PI / 2;
        double ct = Math.cos(theta), st = Math.sin(theta);
        // faster as it does not create any new objects
        xtl[0] = (int) (x1 - t / 2 * st);
        ytl[0] = (int) (y1 + t / 2 * ct);
        xtl[1] = (int) (x1 + t / 2 * st);
        ytl[1] = (int) (y1 - t / 2 * ct);
        xtl[2] = (int) (x2 + t / 2 * st);
        ytl[2] = (int) (y2 - t / 2 * ct);
        xtl[3] = (int) (x2 - t / 2 * st);
        ytl[3] = (int) (y2 + t / 2 * ct);
        g.fillPolygon(xtl, ytl, 4);
    }
}

Related

  1. drawThickLine(Graphics g, int x1, int y1, int x2, int y2)
  2. drawThickLine(Graphics g, int x1, int y1, int x2, int y2, int thickness)
  3. drawThickLine(Graphics graphics, int x1, int y1, int x2, int y2, int linewidth)