今天遇到了一个乱码问题,合成的小票图片上的中文全部变成了口口口,后来在网上查了资料,发现是Graphics2D用了宋体字,而linux服务器上没有对应的字体库。 把本地的字体库上传上去就解决了。
这个需求其实也很常见,java.awt中的Graphics2D就可以完成我目前所有的需求。...例如:填充图片文字(不损失模板帧数的前提下)、给文字设置字体大小、设置字体颜色等 Graphics2D统统可以实现。...Java语言在Graphics类提供绘制各种基本的几何图形的基础上,扩展Graphics类提供一Graphics2D类,它拥用更强大的二维图形处理能力,提供、坐标转换、颜色管理以及文字布局等更精确的控制...Graphics2D定义了几种方法,用于添加或改变图形的状态属性。可以通过设定和修改状态属性,指定画笔宽度和画笔的连接方式;设定平移、旋转、缩放或修剪变换图形;以及设定填充图形的颜色和图案等。...当然了Graphics2D的功能不止于此,如果有类似的需求也可以仔细研究一下。
Graphics2D g2d = bi.createGraphics(); // 画图BasicStroke是JDK中提供的一个基本的画笔类,我们对他设置画笔的粗细,就可以在drawPanel上任意画出自己想要的图形了...BufferedImage对象 BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取Graphics2D...Graphics2D g2d = bi.createGraphics(); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP...BufferedImage对象 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取Graphics2D...Graphics2D g2d = image.createGraphics(); // 增加下面代码使得背景透明 image = g2d.getDeviceConfiguration().createCompatibleImage
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //创建画笔 Graphics2D...需要添加水印图的宽度 * @param height 需要添加水印图的高度 */ private void watermarkCoordinate(Graphics2D
Graphics2D 对象的draw()方法。...Graphics2D类的绘图方法 Graphics2D类仍然保留Graphics类的绘图方法,同时增加了许多新方法。新方法将几何图形(线段、圆等)作为一个对象来绘制。...要用Graphics2D类的新方法画一个图形。...Graphics2D对象的draw()方法绘制这个图形。...例如以下代码用Graphics2D的新方法绘制线段和圆角矩形: Graphics2D g2d = (Graphics2D)g;//将对象g类型从Graphics转换成Graphics2D
Java提供了强大的图形绘制功能,主要通过Graphics2D和Shape接口来实现。在本篇博文中,我们将探讨这两个关键组件,常见的问题,易错点以及如何避免它们。 1. ...Graphics2D简介 Graphics2D是Graphics类的子类,提供了更丰富的二维图形绘制功能,如线条、形状、文本和图像的渲染。...(g); Graphics2D g2d = (Graphics2D) g; // 绘制操作... } 2. ...3.3 忽略Graphics2D的设置 Graphics2D对象可以设置线宽、颜色、抗锯齿等属性。如果不设置,可能会导致默认效果不符合预期。...使用Graphics2D的属性设置 来控制图形的外观。 遵守线程规则,使用SwingUtilities.invokeLater()或EventQueue.invokeLater()更新GUI。
Java 提供了强大的 Graphics2D 类,可以用来在图像上绘制各种图形和文本。...2.1 Graphics2D 简介Graphics2D 是 java.awt.Graphics 类的子类,提供了更强大的图形处理能力。它支持复杂的图形操作,如旋转、缩放、变换、抗锯齿等。...在添加水印时,我们将使用 Graphics2D 对象来绘制水印。2.2 AlphaComposite 控制透明度在为图片添加水印时,我们通常需要控制水印的透明度,使其不会完全遮盖住原图。...我们可以通过控制 Graphics2D 对象的绘制坐标来实现水印的位置调整。3....g2d = (Graphics2D) sourceImage.getGraphics(); // 初始化水印文本的字体、颜色和透明度 AlphaComposite
graphics2D = (Graphics2D)g; Ellipse2D cirle = new Ellipse2D.Float(50, 50, 300, 300);...graphics2D = (Graphics2D) graphics; RenderingHints hints = new RenderingHints(...graphics2D, int x, int y, int r) { Ellipse2D circle = new Ellipse2D.Double(x - r, y - r, 2 *...r, 2 * r); graphics2D.draw(circle); } public static void fillCircle(Graphics2D graphics2D...g, Color color) { g.setColor(color); } public static void setStrokeWidth(Graphics2D
public static void putImage(Graphics2D graphics2D, int x, int y, String imageURL) { ImageIcon...public void drawFractal(Graphics2D graphics2D, int x, int y, int w, int h, int depth) { if...(graphics2D, x, y + 2 * h_3, w_3, h_3, depth + 1); drawFractal(graphics2D, x + 2 * w_3, y...其实和之前是一样的,只是绘画方式不一样: public void drawFractal_Triangle(Graphics2D graphics2D, int Ax, int Ay,...要递归画四条线: public void drawSnow(Graphics2D graphics2D, double x1, double y1, double side, double
图片旋转 图片旋转作为一个常见功能,实际使用中用处挺多,但是这次实现却遇到了个小问题,记录一二 使用的几个类 Graphics2d AffineTransform BufferedImage 1....Graphics2d 方式 利用Graphics2d的rotate方法来实现图片旋转,奇怪的是一直不生效,实现代码如下 BufferedImage bufferedImage = ImageUtil.getImageByPath...("bg.png"); Graphics2D g2d = bufferedImage.createGraphics(); g2d.rotate(Math.toRadians(90), bufferedImage.getWidth
* * 2:Graphics2D继承了Graphics类,实现了功能更加强大的绘图操作的集合。...* 由于Graphics2D类是Graphics类的扩展,也是推荐使用的java绘图类 * 所以本章主要介绍使用Graphics2D类实现JAVA绘图 * * 3:Graphics类使用的不同的方法实现不同的绘制...是继承Graphics类编写的,它包含了Graphics类的绘图方法并添加了更强的功能 * 是推荐使用的绘图类, * Graphics2D可以分别使用不同的类表示不同的形状,如Line2D,Rectangle2D...* * 2:要绘制指定形状的图形,需要先创建并初始化该图类型的对象,这些图形类必须是Shape接口 * 的实现类,然后使用Graphics2D类的draw()方法绘制该图形对象或者使用fill()...g2=(Graphics2D)g;//强制类型转换位Graphics2D类型 //Graphics2D是推荐使用的绘图类,但是程序设计中提供的绘图对象大多是
45 private void drawLine (BufferedImage image) { 46 int num = 3;//一共画3条 47 Graphics2D...g2 = (Graphics2D)image.getGraphics(); 48 for(int i = 0; i < num; i++) {//生成两个点的坐标,即4个值 49...67 BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 68 Graphics2D...g2 = (Graphics2D)image.getGraphics(); 69 g2.setColor(this.bgColor); 70 g2.fillRect...BufferedImage getImage () { 76 BufferedImage image = createImage();//创建图片缓冲区 77 Graphics2D
new Graphics2DRenderer(); // 设置渲染内容 renderer.setDocument(document, document.getDocumentURI()); // 获取Graphics2D...graphics2D = bufferedImage.createGraphics(); renderer.layout(graphics2D, dimension); // 内容渲染 renderer.render...(graphics2D); 说明 为什么并不直接使用 java-html2image ?...graphics2D = GraphicUtil.getG2d(bufferedImage); // 自适应修改生成图片的宽高 if (options.isAutoH...renderer.render(graphics2D); graphics2D.dispose(); return bufferedImage; } } 测试 @
){ BufferStrategy buff = _frm.getBufferStrategy(); while(_gameRunning){ Graphics2D...g = (Graphics2D)buff.getDrawGraphics(); // Rendering _initRendering(g);...e.printStackTrace(); } } } private void _initRendering(Graphics2D...g.setColor(Color.black); g.fillRect(0, 0, 800, 600); } private void _displayInfoText(Graphics2D...``` Graphics2D g = (Graphics2D)buff.getDrawGraphics(); // Rendering _initRendering
CanvasPanel extends JPanel{ public void paint(Graphics g){ super.paint(g); Graphics2D...g2=(Graphics2D)g;//强制类型转换 g2.setColor(Color.CYAN);//设置当前绘图颜色 g2.fill(rect);/...CanvasPanel extends Canvas{ public void paint(Graphics g){ super.paint(g); Graphics2D...g2=(Graphics2D)g; g2.drawImage(img,0,0,this);//显示图片 } } public
BufferedImage( 570, 390, BufferedImage.TYPE_INT_BGR); Graphics gs = image.getGraphics();//获得绘制图像 Graphics2D...g = (Graphics2D) gs;//将绘制图像转换为Graphics2D; strokeButton1.addActionListener(new ActionListener(
.setVisible(true); } class mycanvas extends Canvas{//内部类 public void paint(Graphics g){ Graphics2D...g2 = (Graphics2D) g; Image image = new ImageIcon("src/ycy.jpg").getImage();//获取图片资源 g2.rotate(...Math.toRadians(num));//参数:弧度 Math.toRandians角度转弧度 //旋转图片 Graphics2D 独有方法 g2.drawImage(image, 100
AlgorithmHelper.setColor(graphics2D, AlgorithmHelper.Blue); int w = canvasWidth / money.length...for (int i = 0; i < money.length; i++) { AlgorithmHelper.fillRectangle(graphics2D...(graphics2D, circle.getX(), circle.getY(), circle.getR()); for (int i = 0; i < points.size..., AlgorithmHelper.Red); } else { AlgorithmHelper.setColor(graphics2D..., AlgorithmHelper.Blue); } AlgorithmHelper.fillCircle(graphics2D, p.x
new BufferedImage(width, height, BufferedImage.TYPE\_4BYTE\_ABGR); // 获取图形上下文对象 Graphics2D...graphics = (Graphics2D)image.getGraphics(); graphics.clearRect(0, 0, width, height);...IOUtils.closeQuietly(imageOutputStream); } } private static void setTransparency(BufferedImage image, Graphics2D...= new BufferedImage(width, height, BufferedImage.TYPE\_4BYTE\_ABGR); // 获取画笔 Graphics2D...g2D = (Graphics2D) bufferedImage.getGraphics(); // 绘制Image的图片 g2D.drawImage(imageIcon.getImage
private void drawLine(BufferedImage image) { int num = r.nextInt(10); //定义干扰线的数量 Graphics2D...g = (Graphics2D) image.getGraphics(); for (int i = 0; i < num; i++) { int x1 = r.nextInt...g = (Graphics2D) image.getGraphics(); //设置背景色随机 g.setColor(new Color(255, 255, r.nextInt...*/ public BufferedImage getImage() { BufferedImage image = createImage(); Graphics2D...g = (Graphics2D) image.getGraphics(); //获取画笔 StringBuilder sb = new StringBuilder();
领取专属 10元无门槛券
手把手带您无忧上云