Important Java Concept

Drawing Shapes in Java

Let’s see how to draw different shapes in Java using the graphics class which is present in java.awt package.

Methods in Graphics Class 

Before going into methods, you have to know a few things i.e., coordinates of the shapes.

x1 = It is the x coordinate of the first point
y1 = It is the y coordinate of the first point
x2 = It is the x coordinate of the second point
y2 = It is the y coordinate of the second point

1. drawLine(int x1, int y1, int x2, int y2)
This method is useful to draw a line connecting (x1,y1) and (x2,y2).

2. drawRect(int x,int y,int w,int h)
This method outlines a rectangle. The left top corner of the rectangle starts
at (x,y) the width is w and the height is h.

3. drawOval(int x, int y, int w, int h)
This method draws a circle or eclipse bounded in the region of a rectangle starting at
(x,y) with width w and height h.

4. drawArc(int x, int y, int w, int h, int startAngle, int arcAngle)
Draws an arc bounded by the rectangle region of (x,y) with width w and height h.
Here, startAngle represents the beginning angle measured from the 3’0 clock position in the clock. arcAngle represents the arc angle relative to the start angle.

Let us see the usage of these methods using a java program.

Java Program

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {


    public static void main(String[] a) {
        JFrame f = new JFrame();
        f.setSize(400, 400);
        f.add(new Main());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public void paint(Graphics g) {
        int x = 10;
        int y = 10;
        int w = getSize().width - 20;
        int h = getSize().height - 20;
        g.drawLine(x, y, w, h);
        g.drawRect(x, y, w, h);
        g.drawOval(x, y, w, h);
        int startAngle = 45;
        int arcAngle = -60;
        g.drawArc(x, y, w / 2, h / 2, startAngle, arcAngle);
    }
}

Output

Drawing circle in java awt

Explanation 

To construct shapes, firstly we need to import some packages. They are:


import java.awt.Graphics; 
import javax.swing.JFrame; 
import javax.swing.JPanel;

After importing the required packages we need to create a frame in order to show the shapes inside the frame. The following line of code is used to create a frame.

JFrame f = new JFrame();

JFrame is the advanced version of Frame. ‘f’ is the name of the frame. new is the keyword, and the new JFrame() represents the creation of a new java frame.

f.setSize(400, 400);
f.add(new Main());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

The above lines of code is is used to prepare the frame f. f.setSize(400,400) is used to set the size of the frame. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); is used to close the frame whenever we clicked on the close button(i.e., ‘x’ button on the top right corner). f.setVisible(true); is used to display the frame.

int x = 10;

int y=10;
int w = getSize().width-20;
int h = getSize().height-20;

Here, ‘x’ represents the x coordinate of the starting point, and ‘y’ represents the y coordinate of the starting point. ‘w’ and ‘h’ are the width and height of the polygon respectively, here getSize() method will return the size of the frame.

g.drawLine(x, y, w, h);
g.drawRect(x, y, w, h);
g.drawOval(x, y, w, h);
int startAngle = 45;
int arcAngle = -60;
g.drawArc(x, y, w / 2, h / 2, startAngle, arcAngle);

Here, various methods are used to draw various shapes. drawLine() is used to draw a line using the x, y, w, and h values. drawRect() method is used to draw a rectangle using the values x, y, w, and h. drawOval(), using this method we can draw an oval and a circle. drawArc() method is used to draw an arc using x, y, w, h, startAngle, and arcAngle.

Conclusion

In this article, we have learned about how to create a frame, various packages, methods used to draw shapes in java, and java code to draw various shapes.

Do you want

Java Tutor?

Aniket Malik

Aniket Malik

ORACLE CERTIFIED JAVA TUTOR

🟢 Oracle Certified Java Expert

🟢 300 + ⭐ ⭐ ⭐ ⭐ ⭐

🟢 B.tech & M.tech in CSE

🟢 6+ Years of Teaching Experience

🟢 Worked as SE in Virtusa and Digidez

Do you want

Java Tutor?