How to Draw a Line in Java


The example aims to show how to draw a line in an applet window with drawLine method of Graphics class.

Source Code

package com.beginner.examples;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
 
public class DrawExample4 extends Applet{
 
	public void paint(Graphics g){

		//draw line
		g.drawLine(10, 20, 70, 50);
		System.out.println("ok");
	}
}

Output:

ok

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments