Saturday, July 20, 2013

Graphics2D

Graphics2D is the derived class of the base class Graphics. While Graphics class provides the basics for graphical contexts, the Graphics2D allows you to do sophisticated things in drawing. For example, by using the Graphics2D class you can rotate coordinates, and apply different styles and colors to text and shapes.
Graphics2D is also an abstract class. Its useful methods are shown below.

-clip(Shape sp):void
intersects the current drawing area or clip with the interior of specified shape. The result is the intersection area.
-draw(Shape sp):void
draws the specified shape object.
-drawImage(BufferedImage bi, BufferedImageOp filter, int x,int y): void
draws a BufferedImage object that is filtered with BufferedImageOp.
-drawImage(Image img, AffineTransform af, ImageObserver observer): void
draw an Image object with the transformation specified by AffineTransformed object.
-fill(Shape sp):void
renders a shape that its interior is filled with a default (black color) or specified color, or painting style (GradientPaint, LinearGradientPaint, or TexturePaint).
-rotate(double theta):void
rotate x and y axes of the coordinate system.
-scale(double sx,double sy):void
resizes a shape or an image according to the scaling factors.
-setPaint(Paint paint):void
applies a Paint object to the graphical context.
-setStroke(Stroke stroke):void
applies a Stroke object to the graphical context.
-setTranform(AffineTransform af):void
applies the transformation to the graphical context.
-translate(int x,int y):void
moves the origin to the specified coordinate.


Example 1: draw different shapes

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;

import javax.imageio.ImageIO;

class DrawArea extends Canvas{
public void paint(Graphics g){

Graphics2D g2d=(Graphics2D)g;
g2d.draw(new RoundRectangle2D.Double(10, 50, 100, 100,10,10)); //draw a

round rectangle
g2d.draw(new Ellipse2D.Double(10, 200, 100, 100)); //draw an ellipse

}
}
class WindowShow extends Frame{
public WindowShow(String title){
setTitle(title);
setSize(600,400);
setVisible(true);
add(new DrawArea());

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

}
}
public class AWTTEST {
public static void main(String[] args){

new WindowShow("Draw shapes with Graphics2D");

}


}


draw shapes graphics2d


Example 2: fill shapes with different colors

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D.Double;

class DrawArea extends Canvas{
public void paint(Graphics g){

Graphics2D g2d=(Graphics2D)g;
g2d.setPaint(Color.GREEN);
g2d.fill(new Ellipse2D.Double(20,50,200,200)); //fill an ellipse with

the green color
g2d.setPaint(Color.RED);
g2d.fill(new Arc2D.Double(20,300,200,200,0,180,Arc2D.PIE)); //fill an

arc with the red color



}
}
class WindowShow extends Frame{
public WindowShow(String title){
setTitle(title);
setSize(600,400);
setVisible(true);
add(new DrawArea());

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

}
}
public class AWTTEST {
public static void main(String[] args){
new WindowShow("Fill shapes");

}

}


fill shapes with color graphics2d


Example 3: fill an ellipse with the gradient style


import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Ellipse2D;


class DrawArea extends Canvas{
public void paint(Graphics g){

Graphics2D g2d=(Graphics2D)g;
GradientPaint gp=new GradientPaint(100,100, Color.RED, 150,200,

Color.BLUE); //create GradientPaint object
g2d.setPaint(gp); //apply the GradientPaint object to the graphical

context
g2d.fill(new Ellipse2D.Double(20,50,200,200)); //fill an ellipse


}
}
class WindowShow extends Frame{
public WindowShow(String title){
setTitle(title);
setSize(600,400);
setVisible(true);
add(new DrawArea());

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

}
}
public class AWTTEST {
public static void main(String[] args){
new WindowShow("Fill ellipse with gradient style");

}


}


fill shapes with gradient style graphics2d


Example 4: fill a rectangle with images by using TexturePaint

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

class DrawArea extends Canvas{
public void paint(Graphics g){

Graphics2D g2d=(Graphics2D)g;
try{
BufferedImage bi=ImageIO.read(new File("d:/earth.png"));
TexturePaint tp=new TexturePaint(bi,new Rectangle2D.Double

(100,100,40,40));
g2d.setPaint(tp);
g2d.fill(new Rectangle2D.Double(100, 100, 200, 200));

}catch(IOException e){}


}
}
class WindowShow extends Frame{
public WindowShow(String title){
setTitle(title);
setSize(600,400);
setVisible(true);
add(new DrawArea());

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

}
}
public class AWTTEST {
public static void main(String[] args){

new WindowShow("Fill shape with TexturePaint");

}
}


fill shapes with texturepaint graphics2d


Example 5: rotate an image

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.ImageObserver;

class DrawArea extends Canvas implements ImageObserver{
public void paint(Graphics g){
Graphics2D g2d=(Graphics2D)g;
Image img=Toolkit.getDefaultToolkit().getImage("d:/image02.png");
g2d.translate(300, 0); //move from origin to coordinate (300,0)
g2d.rotate(Math.PI/2); //rotate the image by Math.PI/2(=1.57)
g2d.drawImage(img, 0, 0,img.getWidth(this), img.getHeight(this),this);


}
}
class WindowShow extends Frame{
public WindowShow(String title){
setTitle(title);
setSize(600,400);
setVisible(true);
add(new DrawArea());

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

}
}
public class AWTTEST {
public static void main(String[] args){
new WindowShow("Image rotation");

}
}


rotate image graphics2d


Example 6: make an image gray

import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

class DrawArea extends Canvas{
public void paint(Graphics g){

Graphics2D g2d=(Graphics2D)g;
ColorConvertOp op=new ColorConvertOp(ColorSpace.getInstance

(ColorSpace.CS_GRAY), new RenderingHints(null)); //create gray color filter object to

apply to the image
try{

BufferedImage bi=ImageIO.read(new File("d:/myprogram/image02.png"));

//create BufferedImage object to encapsulate the image
g2d.drawImage(bi,op,20,50); //show the image

}catch(IOException e){}


}
}

class WindowShow extends Frame{
public WindowShow(String title){
setTitle(title);
setSize(600,400);
setVisible(true);
add(new DrawArea());

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

}
}
public class AWTTEST {
public static void main(String[] args){

new WindowShow("Making an image gray");

}

}



Example 7: make an image brighter

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;

class DrawArea extends Canvas{
Image img;
DrawArea(){
//create MediaTracker to control the image load
MediaTracker mt=new MediaTracker(this);
//read image file
img=Toolkit.getDefaultToolkit().getImage("d:/myprogram/image02.png");
mt.addImage(img,0); //add image to the tracker
try{
mt.waitForID(0); //wait the image load
}catch(InterruptedException ie){}
}
public void paint(Graphics g){

Graphics2D g2d=(Graphics2D)g;
BufferedImage bi=makeBrighter(img);
g2d.drawImage(bi,0,0,bi.getWidth(),bi.getHeight(),null);


}
public BufferedImage makeBrighter(Image img){

BufferedImage bi=new BufferedImage(img.getWidth

(null),img.getHeight(null),BufferedImage.TYPE_INT_ARGB);
BufferedImage resultimg=new BufferedImage(img.getWidth

(null),img.getHeight(null),BufferedImage.TYPE_INT_ARGB);
float[] elements = {0.0f, 1.0f, 0.0f, -

1.0f,1.0f,1.0f,0.0f,0.0f,0.0f};
Kernel kernel = new Kernel(3, 3, elements);  //create

kernel object to encapsulate the elements array
ConvolveOp op = new ConvolveOp(kernel,

ConvolveOp.EDGE_NO_OP, null); //create ConvolveOp filter object
Graphics2D graphics2d=bi.createGraphics();
graphics2d.drawImage(img,0,0,bi.getWidth(),bi.getHeight

(),null);
op.filter(bi,resultimg);
return resultimg;
}
}
class WindowShow extends Frame{
public WindowShow(String title){
setTitle(title);
setSize(800,600);
setVisible(true);
DrawArea da=new DrawArea();
add(da);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

validate();

}

}
public class AWTTEST {
public static void main(String[] args){

new WindowShow("Make image brighter");

}

}




Example 8: resize an image

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

class DrawArea extends Canvas{
Image img;
DrawArea(){

//create MediaTracker to control the image load
MediaTracker mt=new MediaTracker(this);
//read image file
img=Toolkit.getDefaultToolkit().getImage("d:/myprogram/image02.png");
mt.addImage(img,0); //add image to the tracker
try{
mt.waitForID(0); //wait the image load
}catch(InterruptedException ie){}
}
public void paint(Graphics g){

Graphics2D g2d=(Graphics2D)g;
//resize image: new width=original width multiplied by 1.2
//new height=original height multiplied by 1.2
g2d.scale(1.2, 1.2);
//display the resized image
g2d.drawImage(img,0,0,null);

}

}
class WindowShow extends Frame{
public WindowShow(String title){
setTitle(title);
setSize(800,600);
setVisible(true);
DrawArea da=new DrawArea();
add(da);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

validate();

}

}
public class AWTTEST {
public static void main(String[] args){

new WindowShow("Resizing an image");

}

}

1 comment: