How to draw shapes using Graphics - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, I am going to show you how to draw shapes using Graphics in Java. Graphics is an abstract base class for all graphics contexts.  This class allows us to draw on components.
For this example, I am going to draw Rectangles and Ovals on the JPanel component.
drawRect() : This method allows us to draw a rectangle with a specific/given dimensions.
drawOval() : This method allows us to draw oval with given dimensions.
package com.onlinetutorialspoint.swing;

import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class DrawShapes extends JPanel {
    private int input;

    public DrawShapes(int choice) {
        input = choice;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < 10; i++) {
            switch (input) {
            case 1:
                g.drawRect(10+i*10, 10+i*10, 50+i*10, 50+i*10);
                break;
            case 2:
                g.drawOval(10+i*10, 10+i*10, 50+i*10, 50+i*10);
                break;
            }
        }
    }
    public static void main(String[] args) {
        String choice = JOptionPane.showInputDialog("Enter 1 to draw rectangles \n"+"Enter 2 to draw Ovals");
        DrawShapes shapes = new DrawShapes(Integer.parseInt(choice));
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(shapes);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}
Output :
Draw Rectangles :
Selecting Ovals :
Draw Ovals :
Happy Learning 🙂

Java Graphics2D Class Example
How to create Java Smiley Swing
How to create Java Rainbow using Swing
Java Swing JOptionPane Example
Java Swing BorderFactory Example
How to use Java JSlider Example
Java Swing JSplitPane Example
Java Swing JTabbedPane Example
Java Swing JOptionPane Html Content Example
Java Swing Login Example
Java JColorChooser Example
Java Swing ProgressBar Example
Java Swing JTable Example
Java Swing JList Example
Java Swing JLabel Example

Java Graphics2D Class Example
How to create Java Smiley Swing
How to create Java Rainbow using Swing
Java Swing JOptionPane Example
Java Swing BorderFactory Example
How to use Java JSlider Example
Java Swing JSplitPane Example
Java Swing JTabbedPane Example
Java Swing JOptionPane Html Content Example
Java Swing Login Example
Java JColorChooser Example
Java Swing ProgressBar Example
Java Swing JTable Example
Java Swing JList Example
Java Swing JLabel Example
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String