• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1839

# draw thick line method

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 java.awt.graphics.  But they can't be used to specify thickness.

What method can I use?
0
YamSeng
1 Solution

Commented:
Can you use Java2D ? If so look at BasicStroke.
0

Author Commented:
I'm just looking at java.awt.graphics, no higher levels.
0

Senior Developer/ArchitectCommented:
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
g.setColor(c);
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
0

Senior Developer/ArchitectCommented:
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.
http://www.apl.jhu.edu/~hall/java/GraphicsUtil.java (code)

Jim

0

Author Commented:
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 java.awt.graphics.

let me try it out.
cheers!
0

Author Commented:
What do you guys think of drawing a filled rectangle?

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

Senior Developer/ArchitectCommented:
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.

Jim
0

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

Tom
0

Author Commented:
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....

cheers!
YamSeng
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.