You are here:Home » Java 2D » Introduction to Java 2D tutorials

Introduction to Java 2D tutorials

Introduction

In this part of the Java 2D tutorial, we will introduce the Java 2D technology.

About

This is Java 2D tutorial. It is aimed at beginners. This tutorial will teach you basics of programming in Java 2D. The images used in this tutorial can be downloaded here.

Vector graphics

There are two different computer graphics. Vector and raster graphics. Raster graphics represent images as a collection of pixels. Vector graphics is the use of geometrical primitives such as points, lines, curves or polygons to represent images. These primitives are created using mathematical equations. Both types of computer graphics have advantages and disadvantages. The advantages of vector graphics are:
  • smaller size
  • ability to zoom indefinitely
  • moving, scaling, filling or rotating does not degrade the quality of an image
The Java 2D API provides tools to work with both vector and raster (bitmap) graphics.

Java 2D API

Java 2D is an API for drawing two-dimensional graphics using the Java programming language.
The Java 2D API provides following capabilities:
  • A uniform rendering model for display devices and printers
  • A wide range of geometric primitives
  • Hit detection on shapes, text, and images
  • A compositing model
  • Enhanced color support
  • Printing documents
  • Control of the quality of the rendering
The Java 2D API enhances the graphics, text, and imaging capabilities of the Abstract Windowing Toolkit (AWT). AWT was the original toolkit for creating user interfaces and graphics in Java. For compatibility purposes, Java 2D is technically a superset of the AWT toolkit.
Java 2D is a powerful technology. It can be used to create rich user interfaces, games, animations, multimedia applications or various special effects.

Java 2D skeleton

Next we will show a skeleton of a Java 2D application.
Skeleton.java
package com.zetcode;

import java.awt.Graphics;

import java.awt.Graphics2D;

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

public class Skeleton extends JPanel {

public void paint(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

g2d.drawString("Java 2D", 50, 50);
}


public static void main(String[] args) {

JFrame frame = new JFrame("Java 2D Skeleton");
frame.add(new Skeleton());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(280, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
This code repeats throughout the Java 2D tutorial. We will go through it once here.
public void paint(Graphics g) {
...
}
All the drawing is done in the paint() method.
Graphics2D g2d = (Graphics2D) g;
The Graphics2D class is a fundamental class for rendering graphics in Java 2D. It represents number of devices in a generic way. It extends the old Graphics object. This casting is necessary to get access to all advanced operations.
g2d.drawString("Java 2D", 50, 50);
Here we draw a string on the panel.
Java 2D Skeleton
Figure: Java 2D skeleton
This part of the Java 2D tutorial was an introduction to the Java 2D library.

0 comments:

Post a Comment