The Java 2D API provides a two-dimensional image, text, and graphics functionality for Java programs by extending the Abstract window Toolbox (AWT). This complex rendering package supports linear images, text and graphics, providing a flexible, powerful framework for rich user interfaces, complex drawing programs, and image processor developers. The Java 2D object appears in a plane, called the user coordinate system space, and the device coordinate system space. When an object is rendered in a screen or printer, the user space coordinate system is converted into a device space coordinate system.
Copy Code code as follows:
Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import java.awt.RenderingHints;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Java.util.Iterator;
Import Java.util.Random;
Import javax.imageio.*;
Import Javax.imageio.stream.ImageOutputStream;
public class Test {
public static void Main (string[] args) throws IOException {
int width = 800;
int height = 800;
BufferedImage image = New BufferedImage (
width, height, bufferedimage.type_int_rgb);
Graphics2D g2d = Image.creategraphics ();
G2d.setcolor (Color.White);
G2d.fillrect (0, 0, width, height);
G2d.setrenderinghint (Renderinghints.key_antialiasing,
RENDERINGHINTS.VALUE_ANTIALIAS_ON);
Random Random = new Random ();
int wo = 0;
int ho = 0;
for (int i = 0; i < i++) {
int x = random.nextint (width);
int y = random.nextint (height);
G2d.setcolor (New Color (Random.nextint (256),
Random.nextint (256), Random.nextint (256)));
G2d.setstroke (New Basicstroke (1));
G2d.drawstring ("Node" +i, x, y);
G2d.drawarc (x, Y, 20, 20, 0, 360);
if (i!=0) {
G2d.drawline (Wo, ho, x, y);
}
wo = x;
Ho = y;
}
G2d.dispose ();
ImageWriter writer = null;
imagetypespecifier type =
imagetypespecifier.createfromrenderedimage (image);
Iterator iter = imageio.getimagewriters (type, "JPG");
if (Iter.hasnext ()) {
writer = (ImageWriter) iter.next ();
}
if (writer = null) {
return;
}
iioimage iioimage = new Iioimage (image, NULL, NULL);
imagewriteparam param = Writer.getdefaultwriteparam ();
Param.setcompressionmode (IMAGEWRITEPARAM.MODE_EXPLICIT);
Param.setcompressionquality ((float) ((10)/10.0));
Imageoutputstream OutputStream =
Imageio.createimageoutputstream (New File ("c:/test.jpg"));
Writer.setoutput (OutputStream);
Writer.write (null, iioimage, param);
}
}