Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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

Advertisements