Java Graphics2D Class Example - 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 tutorials, we are going to see how to use the Java Graphics2D class.
Java Graphics class is a abstract base class, it allows the application to draw something on different AWT or Swing components.
The Graphics2D Class is extended class of Graphics class, it provides more sophisticated controls over text layout, color management and coordinate transformations.
Since this example on Java Graphics2D, I am going to present an interesting output here.
package com.onlinetutorialspoint.swing;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.util.Random;

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

public class Graphics2D_Demo extends JPanel{

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        
        int[] xValues = {55,67,109,73,83,55,27,37,1,43};
        int[] yValues = {0,36,36,54,96,72,96,54,36,36};
        
        Graphics2D graphics2D  = (Graphics2D) g;
        GeneralPath path = new GeneralPath();
        
        path.moveTo(xValues[0], yValues[0]);
        
        for (int i= 1;i<xValues.length;i++){
            path.lineTo(xValues[i], yValues[i]);
        }
        
        path.closePath();
        graphics2D.translate(150, 150);
        Random rNumbers = new Random();
        for (int i= 1;i<=20;i++){
            graphics2D.rotate(Math.PI/10.0);
            graphics2D.setColor(new Color(rNumbers.nextInt(256),rNumbers.nextInt(256),rNumbers.nextInt(256)));
            graphics2D.fill(path);
        }
        
    }
    public static void main(String[] args) {
        JFrame frame = new JFrame("Graphics2D Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        Graphics2D_Demo demo = new Graphics2D_Demo();
        frame.add(demo);
        frame.setBackground(Color.WHITE);
        frame.setSize(315, 330);
        frame.setVisible(true);
    }

}
Output :
Happy Learning 🙂

How to draw shapes using Graphics
How to create Java Smiley Swing
How to create Java Rainbow using Swing
How to use Java JSlider Example
Java Swing JList Example
Java Swing JTabbedPane Example
Java Swing JLabel Example
Java Swing Login Example
Java JList Multiple Selection Example
Java LinkedList class Example
Java WeakHashMap class Example
Properties class Example in Java
Java Class Example Tutorials
Java Static Variable Method Block Class Example
Java Reflection Get Class Hierarchy

How to draw shapes using Graphics
How to create Java Smiley Swing
How to create Java Rainbow using Swing
How to use Java JSlider Example
Java Swing JList Example
Java Swing JTabbedPane Example
Java Swing JLabel Example
Java Swing Login Example
Java JList Multiple Selection Example
Java LinkedList class Example
Java WeakHashMap class Example
Properties class Example in Java
Java Class Example Tutorials
Java Static Variable Method Block Class Example
Java Reflection Get Class Hierarchy
 


Δ
 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