项目场景:
工作中运用了Java画图片,有兴趣的伙伴可以看看swing,我这里简单说几个常用的方法,下面是我的具体业务中的简单使用,别傻傻的复制粘贴
问题描述:
提示:生成图片的流程
1.创建BufferedImage类
2.根据BufferedImage类得到一个Graphics2D对象
3.根据Graphics2D对象进行逻辑操作
4.处理绘图
5.将绘制好的图片写入到图片
@Override
//创建BufferedImage类
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
//根据BufferedImage类得到一个Graphics2D对象
Graphics2D graphics = bufferedImage.createGraphics();
//设置画笔粗细
graphics.setStroke(new BasicStroke(this.getThickness()));
//设置画笔颜色
graphics.setPaint(color);
//画实心椭圆,具体看源码
graphics.fillOval(x,y,h,w);
//画空心椭圆
graphics.drawOval(x,y,h,w);
//写文字
graphics.setFont(new Font("宋体", Font.PLAIN , 80));
graphics.drawString(this.getContent(), this.getX(), this.getY());
//画线段
graphics.drawLine(startX, startY, endX, endY);
//处理绘图
graphics.dispose();
//输出图片,我这里是存到一个流中了,你们可以生成一个图片
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(this.getBufferedImage(), "png", os);
2277

被折叠的 条评论
为什么被折叠?



