Drawing Lines

From Progzoo
Jump to navigation Jump to search

g.drawLine(60,20,60,100) draws a line from (60,20) to (60,100) of the Graphic g. The current color and stroke will be used.

We can control the thickness of the line and the end-cap.

The cap determines what happens at the end of a line.

With the Graphics and the Graphics2D classes we can specify features of the Stroke used to draw the line.

Sets the thickness of the line to be drawn.
g.setStroke(new BasicStroke(10, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER))
The three parameters are thickness, cap style and join style.
The cap style dictates what happens at the end of the line.
The join style determines what happens where two line of a polygon meet.

Thickness and CAP style

We draw three thick black lines. Each has thickness 15.

  • Square end cap
  • Butt end cap
  • Round end cap

The thin red lines are the same length. LineStyle.png

[Font] [Default] [Show] [Resize] [History] [Profile]