draw thick line method

Posted on 2001-08-21
Last Modified: 2012-06-21
I think this is a fairly simple thing to do, but I can't find the method to use.

I need to draw a line with a thickness (in pixels) determined by me.  But I've looked at drawLine() and drawPolyLine() in  But they can't be used to specify thickness.

What method can I use?
Question by:YamSeng
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 27

Expert Comment

ID: 6410560
Can you use Java2D ? If so look at BasicStroke.

Author Comment

ID: 6410605
I'm just looking at, no higher levels.
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6410779
You must draw several lines next to each other to create the effect of one thick line. Here is a method, untested by me, which claims to encapsulate the needed functionality.

void thickLine(Graphics g, Color c, int x1, int y1, int x2, int y2, int thickness) {
    // Method thickLine to draw a thick line in a specified color
    int height, width;
    boolean mostlyHorizontal;

    // Compute height and width of line; check whether line is more horizontal or vertical
    height = y2 - y1;
    if (height < 0) {
        height = -height;
    width = x2 - x1;
    if (width < 0) {
        width = -width;
    mostlyHorizontal = width > height;

    // Draws line repeatedly to desired thickness and color
    for (int i=0; i<thickness; ++i) {
        if (mostlyHorizontal) {
            g.drawLine(x1, y1 + i, x2, y2 + i);
        } else {
            g.drawLine(x1 + i, y1, x2 + i, y2);

Hope this helps.

Best regards,
Jim Cakalic
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 19

Accepted Solution

Jim Cakalic earned 30 total points
ID: 6410793
Also, here's a link to a GraphicsUtil class that appeared in the book Core Web Programming. It has a drawLine method with a similar signature to the one I posted above. It differs in that it computes the polygon that corresponds to the thick line and then draws a filled polygon. I would tend to trust this solution, knowing its source, than the one above. (javadoc) (code)



Author Comment

ID: 6412084
hmm....that's how I thought of initially....(drawing many lines), but I thought it would be inefficient....

haha....but I think that may be the way to go since I only want to use

let me try it out.

Author Comment

ID: 6412965
What do you guys think of drawing a filled rectangle?

Wouldn't that look like a thick line as well?
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6413254
Hi. Take a look again at the links in my second post. The drawLine method of the GraphicsUtil package computes the points for a closed polygon that outlines a line with a given thickness and then draws a filled polygon to simulate the line.


Expert Comment

ID: 6426138
YamSeng, a filled rectangle also works, but ONLY for horizontal and vertical lines.


Author Comment

ID: 6435868
Thanks Jim.  Finally worked out how the method did it with the help of my maths textbook.....well, I wanted to know what's happening in that method.....instead of just using it blindly......

Hmm.....looks like computer graphics seems to require some form of maths background....


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Configure a Bean in an XML file 4 49
jsp insert to database example 2 64
Java: The Public Class Main 4 45
IBM TS2900 (3572) Tape Autoloader Java? 12 61
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question