How to draw Line in Applet

Four coordinate are required For drawing the line in the java applet. First two coordinate define the origin point of the line. Origin point start from upper left corner and further x-coordinate increase in right side and y-coordinate increase downward.

Syntax of drawLine in Java Applet

Graphics_Instance . drawLine(int X1, Int Y1, int X2, int Y2);

If you want to set color on line then use setColor( ) method of Graphics class, which is exist in java.awt package.

Example - designline.java

import java.awt.*;
import java.applet.*;
public class designline extends Applet
{
int width,height;
public void init()
{
setBackground(Color.black);

}
public void paint(Graphics g)
{

g.setColor(Color.green);
g.drawLine(0,0,200,200);
}
}

Compilation of Applet code and generate the class file

Compilation of Applet code and generate the class file

Prepare the HTML file in the same location with <applet> tag. Like

<applet code="designline" width="400" height="400" />

Save the .html file in same location also run in any browser.

Code Generate the following output

How to draw Line in Applet

Using the init ( ) method, you can set the back ground color of the applet. Also set the line color using the graphics class. 

Comments

Popular posts from this blog

difference between structure and union in C Language

Difference between Linear search and Binary Search in c language

Difference between static and dynamic websites