Java Random "Fu" Character Generator – Code Tutorial
This article introduces a Java-based 'Fu' character generator that randomly renders the Chinese 福 character with varying fonts and colors, explains the supporting FontUtil and ImgTool utility classes, provides the full source code, and demonstrates how to run and test the application using the space key.
The article begins with a folklore story about the origin of the Chinese "福" character and explains the motivation to create a "福" generator to help users quickly produce festive images during the New Year.
Two utility classes are defined to simplify drawing and image handling: a FontUtil for rendering text with specific fonts and colors, and an ImgTool for loading background and icon images.
package com.mrxx.game; import java.awt.*; /*字体工具类*/ public class FontUtil { public static void drawWord(Graphics g,String str,Color color,int size,int x,int y,String font){ g.setColor(color); g.setFont(new Font(font,Font.BOLD,size)); g.drawString(str,x,y); } }
package com.mrxx.game; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; /*图片工具类*/ public class ImgTool { //提取图片工具1 public static BufferedImage getimg(String path){ BufferedImage img=null; try { img= ImageIO.read(new File(path)); } catch (IOException e) { e.printStackTrace(); } return img; } //工具2 public static Image getImage(String filename){ return Toolkit.getDefaultToolkit().getImage(filename); } }
The main class RandFu extends JFrame , randomly selects a font from a predefined list, chooses a color, loads a background image, and draws the "福" character at a fixed position. Pressing the space bar restarts the window to generate a new random appearance.
package com.mrxx.game; import javax.swing.*; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.util.Arrays; import java.util.List; /*主类*/ public class RandFu extends JFrame { List colorList; List arrayList; Integer index,fontRandom; BufferedImage bg=null; public RandFu(){ arrayList= Arrays.asList("楷体","宋体","华文琥珀","华文行楷","幼圆","华文新魏","华文彩云","隶书"); fontRandom=(int)((Math.random()*10)%8); System.out.println("字体:"+arrayList.get(fontRandom)); bg=ImgTool.getimg("src/com/mrxx/img/bg.jpg"); //初始化 colorList=Arrays.asList(Color.gray,Color.black,Color.PINK, Color.orange,Color.RED); index=(int) ((Math.random()*10)%5); System.out.println("字体颜色"+index); setSize(600,600); setIconImage(ImgTool.getImage("src/com/mrxx/img/fu.png")); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setBackground(Color.red); //键盘监听 this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_SPACE) { reGame(); } } }); } //重启 public void reGame(){ this.dispose(); String[] args={}; main(args); } @Override public void paint(Graphics g){ g.drawImage(bg,15,50,570,535,null); FontUtil.drawWord(g,"福",colorList.get(index),250,163,400,arrayList.get(fontRandom)); } public static void main(String[] args) { RandFu randFu=new RandFu(); } }
After launching the program, pressing the space bar generates a new random "福" character; screenshots in the article illustrate the visual results.
The article also provides two image resources (background and title icon) and a Baidu Cloud link (with extraction code) for downloading the full source code.
Finally, the author encourages readers to like, share, and comment to support further content creation.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.