背景:
项目中需要用到一个小程序海报,然后分享给别人,因为前端绘制太慢,所以思路就是: 在后端绘制,绘制完毕后,提前存起来该海报的url地址,前端进入分享页面直接显示即可,增强体验性。为此,作为Java后端的我,花了2天时间学习了下Java的Graphics2D的基本的绘制技巧,基本能实现既定的目标。后面的绘制后面再说,这里先把网上搜到的好的案例记录下:
绘制线
2个点连起来:
import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class DrawLineDemo extends JFrame { public DrawLineDemo() { setSize(500, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); } public void paint(Graphics g) { g.setColor(Color.blue); g.drawLine(50, 30, 450, 260); } public static void main(String[] args) { new DrawLineDemo().setVisible(true); } }
绘制矩形 圆形矩形 利用矩形的圆角魔改成圆形
import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class DrawRectDemo extends JFrame { public DrawRectDemo() { setSize(500, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); } public void paint(Graphics g) { g.setColor(Color.blue); //设置颜色 // g.drawRect(100, 100, 300, 100); //画出长为300,宽为100的矩形框。 //圆角矩形 // g.drawRoundRect(100,100,300,100, 40, 40); //当矩形的宽和高相等,圆角弧的横向直径和圆角弧的纵向直径也相等,并等于矩形的宽和高时,画的就是圆形。 g.fillRoundRect(200,110,100,100, 100, 100); } public static void main(String[] args) { new DrawRectDemo().setVisible(true); } }
写字:
import javax.swing.*; import java.awt.*; public class DrawStringDemo extends JFrame { public DrawStringDemo() { setSize(500, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); } public void paint(Graphics g) { g.setColor(Color.GREEN); g.setFont(new Font("楷体", Font.HANGING_BASELINE, 20)); //用此图形上下文的当前字体和颜色绘制由指定 string 给定的文本。 //最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处。 g.drawString("使用画笔绘制的字符串内容", 80, 150); } public static void main(String[] args) { new DrawStringDemo().setVisible(true); } }
小案例
import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.imageio.ImageIO; public class Graphic2DTest { public static void draw() throws IOException{ //绘制宽=480,长=640的图板 int width=480,hight=720; BufferedImage image = new BufferedImage(width,hight,BufferedImage.TYPE_INT_RGB); //获取图形上下文,graphics想象成一个画笔 Graphics2D graphics = (Graphics2D)image.getGraphics(); //消除线条锯齿 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //对指定的矩形区域填充颜色 graphics.setColor(Color.ORANGE); //GREEN:绿色; 红色:RED; 灰色:GRAY graphics.fillRect(0, 0, 240, 720); //对指定的矩形区域填充颜色 graphics.setColor(Color.PINK); graphics.fillRect(240, 0, 240, 720); //生成随机数 Random random = new Random(); /* * 画线 x,y是坐标,定义线段的两个坐标点 */ graphics.setColor(Color.BLACK); int x=100,y=100,x1=100,y1=y; graphics.drawLine(x,y,x+x1,y1); /* *画出一个折线 */ int[] xPoints = {100,100,250,250}; int[] yPoints = {180,150,150,180}; graphics.drawPolyline(xPoints, yPoints, 4); /* * 画出一个闭合多边形(三角形) */ int[] xPoints1 = {100,100,200}; int[] yPoints1 = {240,320,280}; graphics.drawPolygon(xPoints1, yPoints1, 3); /* * 画出一个闭合多边形(菱形) */ int[] xPoints2 = {240,300,360,300}; int[] yPoints2 = {280,240,280,320}; graphics.drawPolygon(xPoints2, yPoints2, 4); graphics.setColor(Color.ORANGE); graphics.fillPolygon(xPoints2, yPoints2, 4); /* *绘制一个椭圆形 */ graphics.setColor(Color.GREEN); int xOval=100,yOval=360; graphics.drawOval(xOval, yOval, 100, 100); /* *绘制一个矩形 */ //graphics.setColor(Color.GRAY);//--设置矩形边框颜色 。GREEN:绿色; 红色:RED; 灰色:GRAY int xRect=240,yRect=360; graphics.drawRect(xRect, yRect, 200, 100); //设置文字颜色 graphics.setColor(new Color( 20+random.nextInt(100), 20+random.nextInt(100), 20+random.nextInt(100) )); //设置文字内容、位置 graphics.drawString("直线",100+50,100-5); graphics.drawString("折线", 200, 150-5); graphics.drawString("空心三角形", 110, 280); graphics.drawString("实心菱形", 300-20, 280); graphics.drawString("椭圆形", 100+50, 360+50); graphics.drawString("矩形", 240+50, 360+50); //graphics.drawString("错误的背景颜色", 100, 540); //graphics.setPaintMode(); //graphics.translate(400, 600); graphics.dispose();//释放此图形的上下文并释放它所使用的所有系统资源 FileOutputStream out=new FileOutputStream("d:/1.jpeg"); ImageIO.write(image,"JPEG",out); out.flush(); out.close(); //super.doGet(request, response); } public static void main(String[] args) { try { draw(); } catch (IOException e) { e.printStackTrace(); } } }
海报案例关键代码
public InputStream videoPoster(GoodsPosterEntity goodsPosterEntity) { try { BufferedImage bimg = getUrlByBufferedImage(goodsPosterEntity.getBackground()); //得到Graphics2D 对象 Graphics2D g2d = getG2d(bimg); //用户头像 BufferedImage userLogo = getSque(getUrlByBufferedImage(goodsPosterEntity.getUserLogo())); g2d.drawImage(setRadius(userLogo,3), 40, 40, 102, 102, null); String userName = goodsPosterEntity.getUserName(); Font font2 = new Font(typeface, Font.BOLD, 28); g2d.setFont(font2); g2d.setColor(Color.WHITE); g2d.drawString(userName, 160, 71); font2 = new Font(typeface, Font.PLAIN, 28); g2d.setFont(font2); g2d.setColor(Color.WHITE); g2d.drawString("给您分享了精彩视频", 160, 112); BufferedImage goodsImg = getUrlByBufferedImage(goodsPosterEntity.getGoodsUrl()); //视频图片 g2d.drawImage(setRadius(goodsImg,2), 40, 182, 670, 660, null); BufferedImage iconPlay = getUrlByBufferedImage(goodsPosterEntity.getIconPlay()); //播放按钮图片 g2d.drawImage(iconPlay, 294, 436, 160, 160, null); //右上角背景 BufferedImage rightImgBg = getUrlByBufferedImage(goodsPosterEntity.getImagePath()); g2d.drawImage(setRadius(rightImgBg,1), 480, 50, 230, 82, null); BufferedImage bottomBg = getUrlByBufferedImage(goodsPosterEntity.getBgCardBottom()); g2d.drawImage(setRadius(bottomBg,2), 40, 790, 670, 504, null); g2d.setColor(new Color(56, 56, 56)); String goodsName = goodsPosterEntity.getGoodsName(); Font font1 = new Font(typeface, Font.BOLD, 36); g2d.setFont(font1); g2d.drawString(goodsName, 82, 880); Image sunCodeUrl = getUrlByBufferedImage(goodsPosterEntity.getSunCodeUrl()); g2d.drawImage(sunCodeUrl, 490, 1074, 180, 180, null); g2d.setColor(new Color(168, 168, 168)); //太阳码中间logo BufferedImage goodsImgSun = getUrlByBufferedImage(goodsPosterEntity.getGoodsUrl()); //商品图片 g2d.drawImage(setRadius(goodsImgSun,3), 540, 1124, 80, 80, null); // 释放对象 g2d.dispose(); return upload(bimg); } catch (Exception c) { c.printStackTrace(); log.info("goodsCard:{}",c.getMessage()); } return null; }
效果
git代码地址:
https://gitee.com/hfl-learn-note/java-jui-demo.git