java 2d畫圖樣本分享(用java畫圖)_java

來源:互聯網
上載者:User

Java 2D API通過擴充抽象視窗工具箱(AWT),為Java程式提供了二維映像,文本和圖形的功能。這個複雜的渲染包支援線形映像,文本和圖形,為富使用者介面,複雜繪圖程式和影像處理器開發人員提供靈活的,功能強大的架構。Java 2D對象出現在一個平面中,稱為使用者座標系空間,和裝置座標系空間。當對象在螢幕或印表機中渲染時,使用者空間座標系被轉換成裝置空間座標系。

複製代碼 代碼如下:

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.