Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Java Swing GUI

How to draw lines, rectangles, and circles in JFrame

In this tutorial, we are going to see how to draw lines, rectangles and circles in JFrame. Java offers us an easy way to draw graphics using Graphics class in AWT package which allows us to draw primitive geometric types like lines, circles, etc… This tutorial explains the different functions of Graphics class used to draw shapes.
 

Draw a line

Graphics class provides the Graphics.drawline(int x1, int y1, int x2, int y2) method to draw a line on the screen. While x1 is the x-coordinate of the first point in the line and y1 is the y-coordinate of the first point in the line. Similarly, x2 and y2 are the coordinates of the second point in the line.
 


[st_adsense]  
Here is the program that draws a line.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.awt.Graphics;
import javax.swing.*;
public class DrawMyLine extends JPanel
{
public void paint(Graphics g){
g.drawLine(20, 20, 200, 180);
}
public static void main(String[] args){
JFrame f = new JFrame("Draw a line");
f.getContentPane().add(new DrawMyLine());
f.setSize(250, 250);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.Graphics; import javax.swing.*; public class DrawMyLine extends JPanel { public void paint(Graphics g){ g.drawLine(20, 20, 200, 180); } public static void main(String[] args){ JFrame f = new JFrame("Draw a line"); f.getContentPane().add(new DrawMyLine()); f.setSize(250, 250); f.setVisible(true); f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
import java.awt.Graphics;
import javax.swing.*;

public class DrawMyLine extends JPanel
{
  public void paint(Graphics g){
    
    g.drawLine(20, 20, 200, 180);
  }
  
  public static void main(String[] args){
    JFrame f = new JFrame("Draw a line");
    f.getContentPane().add(new DrawMyLine());
    f.setSize(250, 250);
    f.setVisible(true);
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Output:


 
[st_adsense]  

Draw a circle

You can draw a circle and oval using the Graphics.drawOval(int x, int y, int width, int height) method. This function performs both functions. ‘x’ and ‘y’ are the starting point on the screen, and ‘width’ and ‘height’ are the parameters to set the width and height of the oval or circle. For the circle, set the same width and height.
 


Here is the program that draws a circle on the screen.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.awt.Graphics;
import javax.swing.*;
public class DrawMyCercle extends JPanel
{
public void paint(Graphics g){
g.drawOval(50, 40, 150, 150);
}
public static void main(String[] args){
JFrame f = new JFrame("Draw a circle");
f.getContentPane().add(new DrawMyCercle());
f.setSize(250, 250);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.Graphics; import javax.swing.*; public class DrawMyCercle extends JPanel { public void paint(Graphics g){ g.drawOval(50, 40, 150, 150); } public static void main(String[] args){ JFrame f = new JFrame("Draw a circle"); f.getContentPane().add(new DrawMyCercle()); f.setSize(250, 250); f.setVisible(true); f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
import java.awt.Graphics;
import javax.swing.*;

public class DrawMyCercle extends JPanel
{
  public void paint(Graphics g){
    
    g.drawOval(50, 40, 150, 150);  
  }
  
  public static void main(String[] args){
    JFrame f = new JFrame("Draw a circle");
    f.getContentPane().add(new DrawMyCercle());
    f.setSize(250, 250);
    f.setVisible(true);
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Output:


 
[st_adsense]  

Draw a rectangle

Graphics class provides the Graphics.drawRect(int x, int y, int width, int height) method for drawing a rectangle or square. The first two parameters specify the starting point and the last two parameters specify the width and height of the rectangle or square. The width and height of the square must be identical.
 


Here is the program that draws a rectangle on the screen.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.awt.Graphics;
import javax.swing.*;
public class DrawMyRect extends JPanel
{
public void paint(Graphics g){
g.drawRect(50, 35, 150, 150);
}
public static void main(String[] args){
JFrame f = new JFrame("Draw a rectangle");
f.getContentPane().add(new DrawMyRect());
f.setSize(250, 250);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.Graphics; import javax.swing.*; public class DrawMyRect extends JPanel { public void paint(Graphics g){ g.drawRect(50, 35, 150, 150); } public static void main(String[] args){ JFrame f = new JFrame("Draw a rectangle"); f.getContentPane().add(new DrawMyRect()); f.setSize(250, 250); f.setVisible(true); f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
import java.awt.Graphics;
import javax.swing.*;

public class DrawMyRect extends JPanel
{
  public void paint(Graphics g){
    
    g.drawRect(50, 35, 150, 150);  
  }
  
  public static void main(String[] args){
    JFrame f = new JFrame("Draw a rectangle");
    f.getContentPane().add(new DrawMyRect());
    f.setSize(250, 250);
    f.setVisible(true);
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Output:


[st_adsense] mcq

Leave a Reply

Your email address will not be published. Required fields are marked *