java-生成印章swing
案例1
package com; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import javax.swing.JFrame; import javax.swing.JPanel; public class Test2 { public static void main(String[] args) { DrawFrame frame = new DrawFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } /** A frame that contains a panel with drawings */ class DrawFrame extends JFrame { public DrawFrame() { setTitle("DrawTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add panel to frame DrawPanel panel = new DrawPanel(); panel.setBackground(Color.WHITE); setLocationRelativeTo(null); add(panel, BorderLayout.CENTER); } public static final int DEFAULT_WIDTH = 320; public static final int DEFAULT_HEIGHT = 340; } /** A panel that displays rectangles and ellipses. */ class DrawPanel extends JPanel { private String message = "公章测试有限公司"; public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.RED); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //绘制圆 int radius = 150; Ellipse2D circle = new Ellipse2D.Double(); circle.setFrameFromCenter(CENTERX, CENTERY, CENTERX + radius, CENTERY + radius); g2.draw(circle); //绘制中间的五角星 Font starFont = new Font("宋体", Font.BOLD, 120); g2.setFont(starFont); g2.drawString("★", CENTERX - 60, CENTERY + 40); //根据输入字符串得到字符数组 String[] messages2 = message.split("",0); String[] messages = new String[messages2.length-1]; System.arraycopy(messages2,1,messages,0,messages2.length-1); //输入的字数 int ilength = messages.length; //设置字体属性 int fontsize = 40; Font f = new Font("Serif", Font.BOLD, fontsize); FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); //字符宽度=字符串长度/字符数 double char_interval = (bounds.getWidth() / ilength); //上坡度 double ascent = -bounds.getY(); int first = 0,second = 0; boolean odd = false; if (ilength%2 == 1) { first = (ilength-1)/2; odd = true; } else { first = (ilength)/2-1; second = (ilength)/2; odd = false; } double radius2 = radius - ascent; double x0 = CENTERX; double y0 = CENTERY - radius + ascent; //旋转角度 double a = 2*Math.asin(char_interval/(2*radius2)); if (odd) { g2.setFont(f); g2.drawString(messages[first], (float)(x0 - char_interval/2), (float)y0); //中心点的右边 for (int i=first+1;i<ilength;i++) { double aa = (i - first) * a; double ax = radius2 * Math.sin(aa); double ay = radius2 - radius2 * Math.cos(aa); AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay); Font f2 = f.deriveFont(transform); g2.setFont(f2); g2.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa))); } //中心点的左边 for (int i=first-1;i>-1;i--) { double aa = (first - i) * a; double ax = radius2 * Math.sin(aa); double ay = radius2 - radius2 * Math.cos(aa); AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay); Font f2 = f.deriveFont(transform); g2.setFont(f2); g2.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa))); } } else { //中心点的右边 for (int i=second;i<ilength;i++) { double aa = (i - second + 0.5) * a; double ax = radius2 * Math.sin(aa); double ay = radius2 - radius2 * Math.cos(aa); AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay); Font f2 = f.deriveFont(transform); g2.setFont(f2); g2.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa))); } //中心点的左边 for (int i=first;i>-1;i--) { double aa = (first - i + 0.5) * a; double ax = radius2 * Math.sin(aa); double ay = radius2 - radius2 * Math.cos(aa); AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay); Font f2 = f.deriveFont(transform); g2.setFont(f2); g2.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa))); } } } public static final int CENTERX = 150; public static final int CENTERY = 150; }
案例2,生产图导出
package com.hwaggLee.swing.graphics2D; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.util.Date; import javax.imageio.ImageIO; public class UtilsGraphics2D { private static final int WIDTH = 500;//图片宽度 private static final int HEIGHT = 500;//图片高度 private static String message = "公章测试有限公司"; private static String centerName = "我是谁"; private static String year = "2016年06月23日"; public static void main(String[] args) throws Exception{ BufferedImage image = startGraphics2D(); try { String filePath = "C:\\Users\\huage\\Desktop\\121231\\"+new Date().getTime()+".png"; ImageIO.write(image, "png", new File(filePath)); //将其保存在C:\\Users\\huage\\Desktop\\121231\\下 } catch (Exception ex) { ex.printStackTrace(); } } public static BufferedImage startGraphics2D(){ // 定义图像buffer BufferedImage buffImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); g.setColor(Color.RED); //设置锯齿圆滑 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //绘制圆 int radius = HEIGHT/3;//周半径 int CENTERX = WIDTH/2;//画图所出位置 int CENTERY = HEIGHT/2;//画图所处位置 Ellipse2D circle = new Ellipse2D.Double(); circle.setFrameFromCenter(CENTERX, CENTERY, CENTERX + radius, CENTERY + radius); g.draw(circle); //绘制中间的五角星 g.setFont(new Font("宋体", Font.BOLD, 120)); g.drawString("★", CENTERX-(120/2), CENTERY+(120/3)); //添加姓名 g.setFont(new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 30));// 写入签名 g.drawString(centerName, CENTERX -(40), CENTERY +(30+50)); //添加年份 g.setFont(new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 20));// 写入签名 g.drawString(year, CENTERX -(60), CENTERY +(30+80)); //根据输入字符串得到字符数组 String[] messages2 = message.split("",0); String[] messages = new String[messages2.length-1]; System.arraycopy(messages2,1,messages,0,messages2.length-1); //输入的字数 int ilength = messages.length; //设置字体属性 int fontsize = 40; Font f = new Font("Serif", Font.BOLD, fontsize); FontRenderContext context = g.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); //字符宽度=字符串长度/字符数 double char_interval = (bounds.getWidth() / ilength); //上坡度 double ascent = -bounds.getY(); int first = 0,second = 0; boolean odd = false; if (ilength%2 == 1) { first = (ilength-1)/2; odd = true; } else { first = (ilength)/2-1; second = (ilength)/2; odd = false; } double radius2 = radius - ascent; double x0 = CENTERX; double y0 = CENTERY - radius + ascent; //旋转角度 double a = 2*Math.asin(char_interval/(2*radius2)); if (odd) { g.setFont(f); g.drawString(messages[first], (float)(x0 - char_interval/2), (float)y0); //中心点的右边 for (int i=first+1;i<ilength;i++) { double aa = (i - first) * a; double ax = radius2 * Math.sin(aa); double ay = radius2 - radius2 * Math.cos(aa); AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay); Font f2 = f.deriveFont(transform); g.setFont(f2); g.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa))); } //中心点的左边 for (int i=first-1;i>-1;i--) { double aa = (first - i) * a; double ax = radius2 * Math.sin(aa); double ay = radius2 - radius2 * Math.cos(aa); AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay); Font f2 = f.deriveFont(transform); g.setFont(f2); g.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa))); } } else { //中心点的右边 for (int i=second;i<ilength;i++) { double aa = (i - second + 0.5) * a; double ax = radius2 * Math.sin(aa); double ay = radius2 - radius2 * Math.cos(aa); AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay); Font f2 = f.deriveFont(transform); g.setFont(f2); g.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa))); } //中心点的左边 for (int i=first;i>-1;i--) { double aa = (first - i + 0.5) * a; double ax = radius2 * Math.sin(aa); double ay = radius2 - radius2 * Math.cos(aa); AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay); Font f2 = f.deriveFont(transform); g.setFont(f2); g.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa))); } } return buffImg; } }
知识只有共享才能传播,才能推崇出新的知识,才能学到更多,这里写的每一篇文字/博客,基本都是从网上查询了一下资料然后记录下来,也有些是原滋原味搬了过来,也有时加了一些自己的想法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架