- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to draw a line on a JFrame in Java
The following is an example to draw a line on a JFrame −
Example
package my; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo extends JFrame { public SwingDemo() { JPanel panel = new JPanel(); getContentPane().add(panel); setSize(550, 300); } public void paint(Graphics gp) { super.paint(gp); Graphics2D graphics = (Graphics2D) gp; Line2D line = new Line2D.Float(200, 150, 150, 220); graphics.draw(line); } public static void main(String[] args) { SwingDemo demo = new SwingDemo(); demo.setVisible(true); } }
Output
- Related Articles
- Draw a border around an undecorated JFrame in Java
- How to disable close button on a JFrame in Java?
- How to close JFrame on the click of a Button in Java
- How to draw a line in OpenCV using Java?
- How to create a Titleless and Borderless JFrame in Java?
- How to display a JFrame to the center of a screen in Java?
- How to maximize JFrame in Java Swing
- How to set minimum size limit for a JFrame in Java
- How to set location of JLabel in a JFrame with Java?
- How to draw a line on a Tkinter canvas?
- How can we minimize/maximize a JFrame programmatically in Java?
- How to activate and deactivate JFrame in Java
- How to change JFrame background color in Java
- How to set FlowLayout for JFrame in Java?
- How to draw a dashed line on a Tkinter canvas?

Advertisements