java 2d画图示例分享(用java画图)
时间:2023-07-25 22:03:52
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);
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教你轻松制作java音乐播放器
![](https://img.aspxhome.com/file/2023/0/91670_0s.jpg)
springmvc的文件保存方法详解
![](https://img.aspxhome.com/file/2023/0/71170_0s.png)
JAVA 实现磁盘文件加解密操作的示例代码
Springboot+AOP实现返回数据提示语国际化的示例代码
![](https://img.aspxhome.com/file/2023/5/73125_0s.png)
Android Scroller的使用方法
![](https://img.aspxhome.com/file/2023/0/125370_0s.gif)
CentOS 7系统下配置自定义JDK的教程
SpringBoot实现二维码扫码登录的原理及项目实践
![](https://img.aspxhome.com/file/2023/5/77995_0s.png)
MyBatis核心源码深度剖析SQL语句执行过程
![](https://img.aspxhome.com/file/2023/5/128845_0s.png)
Spring Boot(二)之web综合开发
Android6.0蓝牙出现无法扫描设备或闪退问题解决办法
![](https://img.aspxhome.com/file/2023/5/139605_0s.jpg)
Android编程实现获取当前连接wifi名字的方法
Android实现加载圈
![](https://img.aspxhome.com/file/2023/7/137227_0s.jpg)
关于Spring Boot项目的 log4j2 核弹漏洞问题(一行代码配置搞定)
![](https://img.aspxhome.com/file/2023/9/85539_0s.png)
一篇文章带你了解XGBoost算法
![](https://img.aspxhome.com/file/2023/2/61082_0s.jpg)
Unity实现攻击范围检测并绘制检测区域
![](https://img.aspxhome.com/file/2023/7/67557_0s.jpg)
使用SpringBoot注解方式处理事务回滚实现
![](https://img.aspxhome.com/file/2023/3/127353_0s.png)
c#将list类型转换成DataTable方法示例
基于WPF实现简单放大镜效果
![](https://img.aspxhome.com/file/2023/3/111123_0s.png)
C#切换鼠标左右键习惯无需控制面板中修改
C#使用表达式树(LambdaExpression)动态更新类的属性值(示例代码)
![](https://img.aspxhome.com/file/2023/2/88372_0s.png)