Java 2d drawing illustration example sharing (with Java drawing) _java

Source: Internet
Author: User

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);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.