Java Applet | Draw a line using drawLine() method Last Updated : 18 Jan, 2019 Comments Improve Suggest changes Like Article Like Report This article shall be explaining the code to draw a line using paint in Java. This uses drawLine() method. Syntax: drawLine(int x1, int y1, int x2, int y2) Parameters: The drawLine method takes four arguments: x1 - It takes the first point's x coordinate. y1 - It takes first point's y coordinate. x2 - It takes second point's x coordinate. y2 - It takes second point's y coordinate Result: This method will draw a line starting from (x1, y1) co-ordinates to (x2, y2) co-ordinates. Below programs illustrate the above problem: Example: Java // Java program to draw a line in Applet import java.awt.*; import javax.swing.*; import java.awt.geom.Line2D; class MyCanvas extends JComponent { public void paint(Graphics g) { // draw and display the line g.drawLine(30, 20, 80, 90); } } public class GFG1 { public static void main(String[] a) { // creating object of JFrame(Window popup) JFrame window = new JFrame(); // setting closing operation window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // setting size of the pop window window.setBounds(30, 30, 200, 200); // setting canvas for draw window.getContentPane().add(new MyCanvas()); // set visibility window.setVisible(true); } } Output : Note: The above function are a part of java.awt package and belongs to java.awt.Graphics class. Also, these codes might not run in an online compiler please use an offline compiler. The x1, x2, y1 and y2 coordinates can be changed by the programmer according to their need. Comment More info R rohitprasad3 Follow Improve Article Tags : Java Java Programs java-applet Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like