Java Graphics Draw Lines

Java Graphics Draw Lines

Summary: By the end of this tutorial "Java Graphics Draw Lines", you will come to know the drawing of lines on frame.

Drawing graphics is very simple in Java. The line drawing takes four parameters of starting point x and y coordinates and ending point x and y coordinates.

Supporting method from Graphics class to draw lines

void drawLine(int x1, int y1, int x2, int y2): x1 and y1 are starting point coordinates and x2 and y2 are ending point coordinates of the line.

Program using drawLine() method
import java.awt.*;
public class DrawingLines extends Frame
{
  public DrawingLines()
  {          // the following 3 lines creates an empty frame
    setTitle("Line Graphics by S N Rao");
    setSize(300, 350);
    setVisible(true);
  }
  public void paint(Graphics g)
  {        
    g.drawLine(60,90, 150, 200);
    g.drawLine(110, 120, 250, 270);
  }
  public static void main(String args[])
  {
    new DrawingLines();
  }
}


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

It is the first graphics program we are writing and is very necessary to discuss each line of code. The same meaning and syntax is followed in each remaining graphics programs.

import java.awt.*;

Many classes required to create GUI components and drawing classes with methods exist in this package. This package is imported here for the sake of classes Frame and Graphics.

public class Lines extends Frame

Our class DrawingLines extends Frame so that the class can make use of Frame class methods to create a frame. Infact, the following three methods belong to the Frame class (inherited from its super class Component class).

setTitle("Line Graphics by S N Rao");
setSize(300, 350);
setVisible(true);

The first method, setTitle() gives the title "Line Graphics by S N Rao" to the frame and is visible on the title bar of the frame. Infact, a frame is a rectangular space on the monitor and to get the space, a request should go to the OS. The method setSize() does this job. The space is dictated by width and height in terms of pixels. Here, it is 300 pixels width and 350 pixels height. Pixel is an abbreviation for picture element. The setSize() method create a frame and added to the frame, but by default, is not visible. To make it visible, the method setVisible(true) should be called; else, the frame will not be visible.

g.drawLine(60,90, 150, 200);

Graphics class comes with a bunch of methods inevitable to draw graphics. drawLine() is one of such methods needed to draw line. This method takes four parameters, the starting coordinates (60, 90) and ending coordinates (150, 200) of the line to be drawn.

new Lines();

An anonymous object of DrawingLines is created just to call the constructor (as the frame creation methods exist in the constructor).

Callback Method

Observe, the paint() method is not called anywhere in the program as it is a callback method that is called implicitly when the frame is created. A callback method is called by the system implicitly; programmer writes the method but he will not call. It is called automatically whenever the system requires it; for example, main() method is a call back method. All the methods of the applet like init(), start() etc. are callback methods.

Leave a Comment

Your email address will not be published.