Java Graphics Draw Lines


Drawing Thicker Lines

There is no predefined method to draw a thicker line in Java. Drawing lines with a gap of one pixel makes a thicker line. One pixel is the minimum distance that can be measured on the screen.

The following program draws 4 pixel thick horizontal line.

import java.awt.*;       
public class ThickLine extends Frame
{
  public ThickLine()
  {
    setTitle("4 Pixel Thicker");
    setSize(350, 150);
    setVisible(true);
  } 
  public void paint(Graphics g)             
  {                          // drawing 3 pixel thick horizontal line
    g.drawLine(40, 50, 250, 50);            
    g.drawLine(40, 51, 250, 51);
    g.drawLine(40, 52, 250, 52);
    g.drawLine(40, 53, 250, 53);
  }
  public static void main(String args[])
  {
    new ThickLine();
  }
}

Java Graphics Draw Lines
Output of ThickLine.java of Java Graphics Draw Lines

g.drawLine(40, 50, 250, 50);
g.drawLine(40, 51, 250, 51);

Keeping y1 and y2 coordinates same draws a horizontal line; other way, it is vertical when x1 and x2 are same. Observe, y1 and y2 are moved down by one pixel. The next line is drawn just below the previous line.

Leave a Comment

Your email address will not be published.