用Java写出敬业福小程序:人人扫出敬业福
1、前言
新年将至,又开始扫福活动,每年的敬业福成了大家难过的坎,于是编写一款福字生成器,提高大家扫福的效率与扫到敬业福的概率,小编已经扫出,文尾截图证明,废话不多说,直接上代码😜(都在用Python写好玩的,我就用Java写点好玩的)
2、定义工具类
“这两个工具类其实可以不专门写出来,随意一点啦~
”
①. 字体工具类
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);
}
}
3、生成"福"主类
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<Color> colorList;
List<String> 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));
//repaint();
}
public static void main(String[] args) {
RandFu randFu=new RandFu();
}
}
4、运行测试
5、素材图片
“最后是证明图:(* ̄(oo) ̄)祝:大家都能顺利扫出敬业福~ 亲测+朋友测试结果:(基本万能福和敬业福都能扫出来)
”
6、程序源码下载
评论