This article shall be explaining the code to draw a line using paint in Java. This uses drawLine() method.
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:
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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Draw a Polygon in Java Applet
- Draw a Smiley in Java Applet
- Draw a Chessboard in Java Applet
- Draw a ellipse and a rectangle in Java Applet
- Draw an Olympic Symbol in Java Applet
- Java Applet Basics
- Difference between a Java Application and a Java Applet
- Java Applet | Digital Stopwatch
- Java Applet | How to display a Digital Clock
- Java Applet | How to display an Analog Clock
- Java Code for Moving Text | Applet | Thread
- Java Applet | Implementing Flood Fill algorithm
- How to draw a Clock using StillClock in Java
- Command Line arguments in Java
- Swap two variables in one line in C/C++, Python, PHP and Java
- How to initialize a list in a single line in Java with a specified value?
- Initialize a list in a single line with a specified value using Java Stream
- How to draw Horizontal and Vertical lines in an Android App using XML
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.