Graphics2D API an introduction to Java2D | Razsoft Education



         To overcome problems with the Graphics class and to enhance some functionality. Java introduces new class called Graphics2D. Which allows to better control over graphics operations? Also java introduces a whole set of package to do 2D graphics operation including image manipulation, drawing and transformation and the package is backward compatible.

               For example Java2D API has separate class for each type of basic shapes. All the shapes implements Shape interface. Also we can define our own shape by implementing shape interface in our own custom shape class.

               Control the fill and line style with control on width of the line. The Stroke and Paint interface was introduced to make custom fill and drawing style.

               AffineTransform allows us to linear transformation of 2D coordinates of shapes like translation, shear, rotation and scale.

               A Font is defined by Glyphs, which is individual shapes to define font characters’.

 

Java 2D API contains following packages.

  1. Java.awt
  2. Java.awt.geom
  3. Java.awt.font
  4. Java.awt.color
  5. Java.awt.image
  6. Java.awt.image.renderable
  7. Java.awt.print

 

Drawing a rectangle using Java2D API

 

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.Rectangle2D;

public class Graphics_2D_API_demo extends Frame {

    @Override

    public void paint(Graphics g) {                         

        //Creating graphics 2D variable

        Graphics2D g2d = (Graphics2D)g;                      

        //Creating a rectangle object

        Rectangle2D.Float rectangle = new Rectangle2D.Float();

        rectangle.setFrame(50, 50, 300, 200);                      

        //drawing rectangle

        g2d.draw(rectangle);

   }

   public static void main(String[] args) {

       Graphics_2D_API_demo mainFrame = new Graphics_2D_API_demo();

       mainFrame.setTitle("Rectangle demo");

       mainFrame.setSize(400, 400);

       mainFrame.addWindowListener(new WindowAdapter() {                

           @Override

           public void windowClosing(WindowEvent e) {

               System.exit(0);

          }

      });

      mainFrame.setVisible(true);

      }

}

Output of the above program

Previous : Computer graphics with java an introduction | Razsoft Education 

Next : 


Comments

Popular posts

"climits" Header file in c++ programming language | Razsoft Education

Integer data type in C++ programming language | Razsoft education

Computer graphics with java an introduction | Razsoft Education