Write a dedicated blessing applet in Java: everyone sweeps out dedicated blessings

1. Foreword

Origin of "blessing": when Jiang Taigong granted a large number of immortals, he granted his wife Ye as a poor God and told her, "you can't go to blessed places." Since then, every family has pasted the word "blessing" for the new year, which is to tell the poor God that I am a blessed place here. You must not come in. The word "blessing" is a symbol of getting rid of poverty and pursuing happiness. The reason why the word Fu is pasted upside down is that it is said that it originated from the palace of Prince Gong in the Qing Dynasty. On the eve of the spring festival that year, the housekeeper wrote several big "Fu" characters as usual and asked people to paste them on the gate of the palace. There was a family member who didn't know Ding and pasted the word "Fu" with the prefix facing down. Prince Gong Fujin was very angry and wanted to be punished. But the housekeeper was an eloquent man and hurriedly knelt down to state: "I often hear people say that Prince Gong has a long life and great blessings. Now the great blessings have really come (fallen). It's a sign of joy." Prince Gong felt reasonable and thought, "no wonder passers-by said that Prince Gong's blessing has come (fallen), auspicious words have been said a thousand times, and gold and silver have increased. Ignorant slaves really didn't expect such a move!" So he rewarded the housekeeper and the servant with fifty Liang silver each, which was really a blessing in disguise.

As the new year is approaching, we start to sweep the blessings again. The annual professional blessings have become a sad barrier for everyone, so we write a blessing word generator to improve the efficiency of sweeping the blessings and the probability of sweeping the professional blessings. The Xiaobian has been swept out. The screenshot at the end of the article proves that we don't talk much nonsense and go directly to the code 😜 (I'm writing something fun in Python, so I'll write something fun in Java)

2. Define tool classes

In fact, these two tool classes can be written at will rather than specially~

①. Font tool class

package com.mrxx.game;
import java.awt.*;
/*Font tool class*/
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);
    }
}

②. Picture tools

package com.mrxx.game;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/*Picture tools*/
public class ImgTool {

  //Extract picture tool 1
  public static BufferedImage getimg(String path){
    BufferedImage img=null;
    try {
      img= ImageIO.read(new File(path));
    } catch (IOException e) {
      e.printStackTrace();
    }
    return img;
  }

  //Tool 2
  public static Image getImage(String filename){
    return Toolkit.getDefaultToolkit().getImage(filename);
  }
}

3. Generate "Fu" main class

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;
/*Main class*/
public class RandFu extends JFrame {
    List<Color> colorList;
    List<String> arrayList;
    Integer index,fontRandom;
    BufferedImage bg=null;
    public RandFu(){
        arrayList= Arrays.asList("Regular script","Song style","Chinese Amber","Chinese block letters","Immature","Hua Wenxin Wei","Hua wencaiyun","official script");
        fontRandom=(int)((Math.random()*10)%8);
        System.out.println("typeface:"+arrayList.get(fontRandom));
        bg=ImgTool.getimg("src/com/mrxx/img/bg.jpg");
        //initialization
        colorList=Arrays.asList(Color.gray,Color.black,Color.PINK, Color.orange,Color.RED);
        index=(int) ((Math.random()*10)%5);
        System.out.println("Font color"+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);

        //Keyboard monitor
        //Set panel keyboard monitoring
        this.addKeyListener(new KeyAdapter() {
            //Rewriting keyboard monitoring method
            @Override
            public void keyPressed(KeyEvent e) {
                //Determine whether it is a space
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    reGame();
                }
            }
        });

    }

    //restart
    public void reGame(){
        //Close current window
        this.dispose();
        //Open a new window
        String[] args={};
        main(args);
    }


    @Override
    public void paint(Graphics g){
        //Painting background
        g.drawImage(bg,15,50,570,535,null);
        FontUtil.drawWord(g,"blessing",colorList.get(index),250,163,400,arrayList.get(fontRandom));
        //repaint();
    }

    public static void main(String[] args) {
        RandFu randFu=new RandFu();
    }
}

That's all the code

4. Run test

After the program is started, the space bar will randomly generate the word "Fu". Some effects are demonstrated:

5. Material picture

One of the following two kinds of pictures is the background and the other is the title picture (if you don't want to do it, you can directly download the resources I uploaded (for free). If you are interested, you can refer to my previous article to package this program into an exe application). The use method is the space bar

Finally, the proof figure: (* ~ (oo) ~) I wish everyone can sweep out their professional blessings smoothly~ Personal test + friend test results: (basic universal blessing and professional blessing can be swept out)

6. Program source code download

Baidu cloud link: https://pan.baidu.com/s/1pQ9wXHw335u7muBHHvYsWA Extraction code: sf4q

Added by AlGale on Thu, 10 Feb 2022 13:54:57 +0200