1、程序需要两张图片和两个配置文件。其中奖池文件写奖品内容,config.properties文件中写相关配置。
![在这里插入图片描述](https://img-blog.csdnimg.cn/7c3a32eb17b94eceaf295a6eb11275f6.png)
2、PrizePool文件内容,每行一个奖品,奖品由多少,红包个数有多少:
铅笔
笔记本
橡皮
棒棒糖
拥抱
羽毛球
棒球
李子
桃子
梅子
沃柑
橙子
橘子
无花果
山竹
烧饼
鸡叉骨
麦克风
小音箱
小台灯
大宝
抽纸
空奖
空奖
空奖
3、config.properties文件内容
# 每一行的红包个数,奖池中的奖品个数数字最好是该参数的整数倍,可以铺满屏幕
com.zzh.num = 10
# 两个红包之间的间隔像素数
com.zzh.gap = 5
# 显示的字号
com.zzh.fontSize = 24
# 显示的字体
com.zzh.font = 黑体
4、两张红包图片
![请添加图片描述](https://img-blog.csdnimg.cn/7e7935c15fbc442da5f9dc02c840e841.jpeg)
5、Java代码
其中private String path = System.getProperty("exe.path"); 是使用了exe4j打包为exe文件后,将exe文件与上述文件放在一起。在编译器中,将java文件和上述文件放在一起,或者修改自定义路径。
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
public class GetPrize extends JFrame{
private static final long serialVersionUID = 1L;
private List prizes = null; // 奖池
private Dimension dimension; // 窗口大小
private int num = 4; // 每一行的红包个数
private int gap = 5; // 两个红包之间的间隔像素数
private String path = System.getProperty("exe.path"); // exe打包路径
// private String path = this.getClass().getResource("").getPath(); // 当前文件的路径
private int fontSize = 18;
private String fontName = "黑体";
public GetPrize() {
getProperties(); // 初始化参数
initWindow(); // 初始化窗体
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击×关闭程序
this.setVisible(true);
}
/**
* 初始化窗口
*/
private void initWindow() {
this.getPrizePool(); // 加载奖池
Collections.shuffle(this.prizes, new Random()); // 打乱奖池顺序
int rows = this.prizes.size() / this.num; // 显示的红包的行数
this.dimension = getWindowSize(); // 获取窗口大小
this.setSize(dimension); // 设置主面板的大小
BufferedImage bi = getImageScale((int)this.dimension.getWidth(), this.path + "/hongbao.jpg"); // 计算红包图片大小
BufferedImage biA = getImageScale((int)this.dimension.getWidth(), this.path + "/chaikaihongbao.jpg"); // 计算拆开红包后的图片大小
Dimension totalPaneDimension = new Dimension((int) this.dimension.getWidth(), (int) ((bi.getHeight() + this.gap) * rows)); // 计算组件面板的大小
JPanel totalJPanel = new JPanel(); // 放置组件的面板
totalJPanel.setPreferredSize(totalPaneDimension); // 限制组件面板的大小
for(int i = 0; i
File file = new File(path + "/config.properties");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
Map res = readBr(br);
this.num = Integer.parseInt(res.get("com.zzh.num"));
this.gap = Integer.parseInt(res.get("com.zzh.gap"));
this.fontSize = Integer.parseInt(res.get("com.zzh.fontSize"));
this.fontName = res.get("com.zzh.font");
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 从配置文件中取出数据
* @param br 输入缓冲字符流
* @return 配置名+参数的键值对map
*/
private Map readBr(BufferedReader br) {
String str;
Map res = new HashMap();
try {
while((str = br.readLine()) != null) {
str = str.trim();
if(str.startsWith("#")) { // 读取到注释内容
continue;
}
if(str.contains("#")) { // 包含注释内容,#号之后的内容为注释部分
str = str.split("#")[0]; // 取#号前面的内容
}
String[] temp = str.split("=");
res.put(temp[0].trim(), temp[1].trim());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
/**
* 获取屏幕大小
* @return 包含屏幕大小信息的Dimension对象
*/
private Dimension getWindowSize() {
return Toolkit.getDefaultToolkit().getScreenSize();
}
/**
* 从文件中加载奖池内容
*/
private void getPrizePool() {
String pathPool = this.path + "/PrizePool";
File file = new File(pathPool);
if (this.prizes == null) {
this.prizes = new ArrayList();
}
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String temp;
while((temp = br.readLine()) != null){
this.prizes.add(temp); // 将奖品加入到列表中
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 按照设置的每行所放置的红包个数,按比例调整图片大小,防止图片变形
* @param width 屏幕宽度
* @param pathImage 图片的路径
* @return 调整大小之后的BufferedImage
*/
private BufferedImage getImageScale(int width, String pathImage) {
File file = new File(pathImage);
try {
BufferedImage bi = ImageIO.read(file); // 读取图片
// 调整宽高
int imageWidth = (width - this.gap * (this.num + 1)) / this.num; // 去除间隙宽度
int imageHeight = (int)(bi.getHeight() / ((double)bi.getWidth() / imageWidth)); // 按宽度的比例缩放高度
BufferedImage newBi = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_3BYTE_BGR);
newBi.getGraphics().drawImage(bi.getScaledInstance(imageWidth, imageHeight, Image.SCALE_DEFAULT), 0, 0,imageWidth, imageHeight, null);
return newBi;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 红包按钮
* @param bi 红包图片
* @param biA 红包被拆开的图片
* @param prizeName 奖品名称
* @return 返回带有监听事件的按钮
*/
private JButton buttonWithListener(BufferedImage bi, BufferedImage biA, String prizeName) {
ByteArrayOutputStream baosR = new ByteArrayOutputStream();
ByteArrayOutputStream baosA = new ByteArrayOutputStream();
try {
ImageIO.write(bi, "jpg", baosR);
ImageIO.write(biA, "jpg", baosA);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Icon rIcon = new ImageIcon(baosR.toByteArray()); // 初始的红包图片
Icon aIcon = new ImageIcon(baosA.toByteArray()); // 拆开后的红包图片
JButton button = new JButton(rIcon);
button.setFocusPainted(false); // 去除虚线框
button.setBorderPainted(false); // 不打印边框
button.setBorder(null); // 去除边框
button.setContentAreaFilled(false); // 去除背景
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
button.setHorizontalTextPosition(SwingConstants.CENTER); // 文本水平居中
button.setVerticalTextPosition(SwingConstants.CENTER); // 文本垂直居中
button.setText(prizeName); // 设置文本
Font font = new Font(fontName, Font.BOLD, fontSize); // 设置文字字体、加粗、字号
button.setFont(font); // 字体样式添加到按钮
button.setIcon(aIcon); // 设置点击之后所显示的图片
}
});
return button;
}
public static void main(String[] args) {
new GetPrize();
}
}
6、运行效果:可以上下滑动
注意:因为用的是FlowLayout,因此将打开的窗口拖动到其他分辨率不同的显示器上,每行的红包个数会改变。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/e2d7bdd7e1a543c08e4517c797d39ef0.png)
|