Graphics 2D OR Java 2D all shapes in java

Java 2D enables the artistic imagination of any programmer to produce high-quality, professional graphics. Java 2D opens the door to numerous possibilities; you can 
  •  Draw or fill any Shape. Simply call the Graphics2D’s draw or fill methods with the shape as an argument. 
  •  Take advantage of the setPaint method in the Graphics2D class to paint shapes in solid colors (Color), gradient fills (GradientPaint), or with tiled images (TexturePaint). 
  •  Explore transparent shapes and change mixing rules for joining shapes. Numerous Porter-Duff mixing rules in the AlhpaComposite class define how shapes are combined with the background. 
  • Break the “one pixel wide” pen boundary and create a BasicStroke to control the width of the pen, create dashing patterns, and define how line segments are joined. 
  •  Create an AffineTransform object and call setTransform on the Graphics2D object to translate, rotate, scale, and shear those shapes before drawing. 
  •  Control the quality of image through the RenderingHints. In addition, the RenderingHints can control antialiasing of colors at shape boundaries for a smoother, more appealing presentation.
Download Topics in This Chapter Book 

Java version of the  Java 2D all shapes problem  is given below: 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
package graphics;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

 * @author ACHCHUTHAN
public class drawing2d extends JFrame {

    public drawing2d() {
        super("Graphics 2D Shapes By Achchuthan @");
        setSize(600, 500);

    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        g2d.setStroke(new BasicStroke(6.0f));
        g2d.draw(new Line2D.Double(100, 150, 500, 150));
        g2d.setStroke(new BasicStroke(7.0f));
        g2d.draw(new Rectangle2D.Double(100, 200, 65, 100));
        g2d.setStroke(new BasicStroke(5.0f));
        g2d.draw(new RoundRectangle2D.Double(100, 40, 75, 100, 50, 50));
        g2d.setStroke(new BasicStroke(9.0f));
        g2d.draw(new Rectangle.Double(200, 200, 50, 50));
        g2d.setStroke(new BasicStroke(3.0f));
        g2d.draw(new Ellipse2D.Double(200, 40, 165, 100));
        g2d.setStroke(new BasicStroke(2.0f));
        g2d.fill(new Ellipse2D.Double(400, 40, 100, 100));
        g2d.setStroke(new BasicStroke(4.0f));
        g2d.draw(new Arc2D.Double(280, 200, 75, 100, 0, -270, Arc2D.PIE));
        //--------------------Arc Chord--------------------------
        g2d.setStroke(new BasicStroke(4.0f));
        g2d.draw(new Arc2D.Double(280, 200, 75, 100, 0, -270, Arc2D.CHORD));
        //--------------------Arc Open--------------------------
        g2d.setStroke(new BasicStroke(4.0f));
        g2d.draw(new Arc2D.Double(280, 200, 75, 100, 0, -270, Arc2D.OPEN));
        Color c1 = new Color(255, 175, 175);
        Color c2 = new Color(255, 0, 0);
        g2d.setPaint(new GradientPaint(5, 30, c1, 35, 100, c2, true));
        g2d.setStroke(new BasicStroke(4.0f));
        g2d.fill(new Ellipse2D.Double(100, 340, 150, 100));
        float dashes[] = {10};
        g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND,
                                  BasicStroke.JOIN_ROUND, 10, dashes, 0));
        g2d.draw(new Line2D.Double(100, 320, 500, 320));
        g2d.draw(new RoundRectangle2D.Double(300, 340, 200, 100, 50, 50));


    public static void main(String args[]) {
        drawing2d d = new drawing2d();
Output of this program is given bellow :

Previous Post Next Post