Java代码写春联,支持各种字体颜色设置,比python还简单

Posted 洛阳泰山

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java代码写春联,支持各种字体颜色设置,比python还简单相关的知识,希望对你有一定的参考价值。

最近看到一篇 Python代码写春联的文章,什么?!Python能搞,我Java这么牛逼的语言,必须得搞!以彰显我大java的威武,和无所不能!!!

首先找到一张春联背景图案(抱歉,图片素材不好找,那原作者的素材用用!!!)

 接下就是见证奇迹的时刻,上代码


import sun.font.FontDesignMetrics;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class Couplet 

    //设置生成文件存放文件夹
    final static String imageFolderPath = "C:\\\\Users\\\\liuya\\\\Desktop\\\\img\\\\";
    //设置背景图
    final static String sourceFilePath = "C:\\\\Users\\\\liuya\\\\Desktop\\\\img\\\\background.png";
    //设置字体
    final static String fontName = "宋体";


    public static void main(String[] args) 
        write();
    

    public static  void write()
        BufferedImage image = new BufferedImage(640*4, 640+2*640*7, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.createGraphics();
        g.drawImage(writeH("恭喜发财"),0,0,640*4,640,null);
        g.drawImage(writeV("三江进宝百业旺"),0,640,640*2,2*640*7,null);
        g.drawImage(writeV("四海来财万福春"),2*640,640,640*2,2*640*7,null);
        //输出文件
        try 
            ImageIO.write(image,"png",new File(imageFolderPath +"春联.png"));
         catch (IOException e) 
            e.printStackTrace();
        
    


    public static  BufferedImage writeH(String text)
        char[] ch= text.toCharArray();
        BufferedImage image = new BufferedImage(640*ch.length, 640, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.createGraphics();
        for (int i = 0; i < ch.length; i++) 
            char c = ch[i];
            g.drawImage(fontMark(String.valueOf(c)),640*i,0,640,640,null);
        
        g.dispose();
        return image;
    


    public static  BufferedImage writeV(String text)
        char[] ch= text.toCharArray();
        BufferedImage image = new BufferedImage(2*640, 2*640*ch.length, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.createGraphics();
        for (int i = 0; i < ch.length; i++) 
            char c = ch[i];
            g.drawImage(fontMark(String.valueOf(c)),0,2*640*i,2*640,2*640,null);
        
        g.dispose();
        return image;
    


    public static BufferedImage fontMark(String content) 
        try 
            BufferedImage image = ImageIO.read(new File(sourceFilePath));
           return fontMark(image, content);
         catch (IOException e) 
            return null;
        
    

    //文字标记
    public static BufferedImage fontMark(BufferedImage bufImg, String content) 
        Font font = new Font(fontName, Font.BOLD, 400);
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        Graphics2D graphics = bufImg.createGraphics();
        graphics.setColor(Color.black);
        graphics.setFont(font);
        graphics.drawString(content, 120, 460);
        graphics.dispose();
        System.out.println(content);
        return bufImg;
    


启动方法:idea编辑器里运行主方法即可。

看效果,上图

 合成一张图里,方便打印,剪裁,哈哈!!!

以上是关于Java代码写春联,支持各种字体颜色设置,比python还简单的主要内容,如果未能解决你的问题,请参考以下文章

html中改变字体颜色的代码怎么写

html中改变字体颜色的代码怎么写

css中font字体颜色怎么设置

最新版的Python写春联,支持行书隶书楷书,不再有缺失汉字

最新版的Python写春联,支持行书隶书楷书,不再有缺失汉字

echarts各种字体颜色修改