How to Draw Shapes in Java?

How to draw shapes in Java

Are you wondering how to draw shapes in Java? Well, it’s not that difficult, so let’s explore it in this article, Drawing shapes in Java.  😀

Two-dimensional (2D) text, shapes, and pictures may be rendered in Java programs using the Graphics2D class. The java.awt package contains this class. Additionally, objects that represent geometric forms are defined using the Shape interface. The java.awt.geom package contains these geometries.

In this article, we’ll discuss how to draw geometric 2D visuals using the Graphics2D class and the Shape interface in Java. So without further ado, let’s dive deep into the topic and see some real examples. 👇

 

 

How to Draw Shapes in Java?

As we’ve discussed in Java, there is a package named java.awt.geom that contains geometrical shapes.  And using this package, we can draw shapes in Java.

The four different classes required to draw geometric 2D visuals are:

  • java.awt.Graphics: This is an abstract class that we have used to draw or paint items.
  • javax.swing.JFrame: This class inherits the java.awt Frame class to present the items in a GUI.
  • javax.swing.JPanel: Provides better organization of components in GUIs.
  • java.awt.Color: Used for adding different colors to components. These classes are used in all of the following functions.

 

 

How to Draw a Rectangle in Java?

Let’s see an example of a rectangle.

 

Code

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Rectangle extends JPanel {

public static void main(String[] a) {

     JFrame f = new JFrame();

      f.setSize(400, 400);

      f.add(new Rectangle());

      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      f.setVisible(true);

   }

   public void paint(Graphics g) {

      g.setColor(Color.blue);

      g.fillRect(45, 75, 170,120);

   }
}

 

Output

How to draw a rectangle in Java

 

To draw a geometric shape(rectangle), the Rectangle class extends JPanel and we created a new JFrame for our 2D Graphic shape and selected a 400×400 size of  GUI, and added a new Rectangle() to JFrame to create a rectangle.

In 2nd  Method paint (Graphics g), to draw a rectangle, we wrote a builtin method g.fillRect(int x,  int y, int w,  int h), where x and y coordinate, w is the width, and h is the height of the rectangle. To add color to it, we’ll use the g.setColor(Color.c) method in which we asked for the blue color to be filled in the rectangle.

 

 

How to Draw a Circle in Java?

Code

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Circle extends JFrame{

public static void main(String[] a) {

JFrame f = new JFrame();

      f.setSize(400, 400);

      f.add(new Circle());

      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      f.setVisible(true);

   }

   public void paint(Graphics g) {

      g.setColor(Color.red);

      g.fillArc(45,75,150,150,0,360);

   }
}

 

Output

How to draw a circle in Java

 

To draw a geometric shape(circle), the Circle class extends JPanel and we created a new JFrame for our 2D Graphic shape and selected a 400×400 size of  GUI, and added a new circle() to JFrame to create a circle. Like this, a GUI is made by presenting a 2D geometric shape that is a circle

In 2nd  Method paint(Graphics g), to draw a circle we wrote a builtin method fillArc(int x, int y, int width, int height, int startangle, int arcangle) this method actually makes an arc but by changing the degrees and making changes according to, to let one end of a curve meet other and make it a circle we put arcAngle 360 degrees and the width value should be equal to height. The start angle can be any angle, you can use a start angle of 0. and for adding color to it we wrote the method setColor(Color. c) in which we asked for the red color to be filled in the circle.

 

 

How to Draw a Triangle in Java?

Code

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Triangle extends JFrame{

public static void main(String[] a) {

JFrame f = new JFrame();

      f.setSize(400, 400);

      f.add(new Triangle());

      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      f.setVisible(true);

   }

   public void paint(Graphics g) {

     int p[]={150,70,230};

    int q[]={50,200,200};

     g.setColor(Color.pink);

    g.fillPolygon(p,q,3);


   }

}

 

Output

How to draw a triangle in Java

 

To draw a geometric shape(Triangle), the Triangle class extends JPanel and we created a new JFrame for our 2D Graphic shape and selected a 400×400 size of  GUI, and added a new Triangle() to JFrame to create a triangle.

In 2nd  Method Paint (Graphics g), to draw a triangle, we wrote a builtin method fillPolygon([]p, []q, 3), where p and q are arrays consisting of a set of (x, y) coordinate pairs, where each pair is the vertex of the polygon. So for the triangle, we made 3 vertices, and to fill it with color, we wrote the method setColor (Color. c), in which we asked for the pink color to be filled in a triangle.

 

 

How to Draw Multiple Shapes in Java?

Code

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;




public class AllBasicShapes extends JPanel{

   public static void main(String[] a) {

      JFrame f = new JFrame();

      f.setSize(400, 400);

      f.add(new AllBasicShapes());

      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      f.setVisible(true);

   }




public void paint(Graphics g) {

    g.drawRect(10, 10, 50, 50); //Draws square

    g.drawRect(10, 75, 100, 50); //Draws rectangle

    g.drawPolygon(new int[] {35, 10, 60}, new int[] {150, 200, 200}, 3); //Draws triangle

    g.drawArc(115,135,100,100,0,360); //Draws Circle

    g.dispose();  

   }

}

 

Output

How to draw Multiple shapes in Java

 

To draw a geometric shape(allShapes), Allshapes class extends JPanel and we created a new JFrame for our 2D Graphic shape and selected a 400×400 size of  GUI, and added a new AllShapes() to JFrame to create rectangle, square,  triangle, and circle together in one go.

In 2nd  Method paint(Graphics g), to draw all 4 basic shapes we wrote 4 methods for square drawRect(int x, int y, int width, int height), for rectangle drawRect(int x, int y, int width, int height) is used,  for triangle drawPolygon([]p,[]q,3) is used where  3 is the vertex, for circle drawArc(int x, int y, int width, int height, int start angle, int arc angle) is used where arc angel is 360 degree. Hence we have a square, rectangle, triangle, and circle with no colors in them.

 

 

Conclusion

To conclude the article on drawing shapes in Java, we’ve discussed the different required classes and packages to draw shapes in Java. In addition to that, we’ve discussed how to draw a circle, rectangle, triangle, and multiple shapes in Java, along with code examples.

Let’s have a quick recap of the topics discussed in this article.

  1. How to draw shapes in Java?
  2. How to draw a rectangle in java?
  3. How to draw a circle in java?
  4. How to draw a triangle in Java?
  5. How to draw multiple shapes in Java?

Here is a quick task for you to explore more; Which is to draw a polygon in Java.

If you’ve found this article helpful, don’t forget to share and comment below, 👇 which solutions have helped you solve the problem?

Total
0
Shares
Leave a Reply

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

Related Posts
Total
0
Share