java 2d繪圖示例分享(用java繪圖)。本站提示廣大學習愛好者:(java 2d繪圖示例分享(用java繪圖))文章只能為提供參考,不一定能成為您想要的結果。以下是java 2d繪圖示例分享(用java繪圖)正文
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);
}
}