主界面 603 * 680
3.初始化图片
图片素材
- 未打乱效果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/8956b4f114164cdb97c9f4dd182e1b30.png)
- 代码:
private void initImage_origin() {
ImageIcon icon1 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\1.jpg");
JLabel jLabel1 = new JLabel(icon1);
jLabel1.setBounds(0,0,105,105);
this.getContentPane().add(jLabel1);
ImageIcon icon2 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\2.jpg");
JLabel jLabel2 = new JLabel(icon2);
jLabel2.setBounds(105,0,105,105);
this.getContentPane().add(jLabel2);
ImageIcon icon3 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\3.jpg");
JLabel jLabel3 = new JLabel(icon3);
jLabel3.setBounds(210,0,105,105);
this.getContentPane().add(jLabel3);
ImageIcon icon4 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\4.jpg");
JLabel jLabel4 = new JLabel(icon4);
jLabel4.setBounds(315,0,105,105);
this.getContentPane().add(jLabel4);
ImageIcon icon5 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\5.jpg");
JLabel jLabel5 = new JLabel(icon5);
jLabel5.setBounds(0,105,105,105);
this.getContentPane().add(jLabel5);
ImageIcon icon6 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\6.jpg");
JLabel jLabel6 = new JLabel(icon6);
jLabel6.setBounds(105,105,105,105);
this.getContentPane().add(jLabel6);
ImageIcon icon7 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\7.jpg");
JLabel jLabel7 = new JLabel(icon7);
jLabel7.setBounds(210,105,105,105);
this.getContentPane().add(jLabel7);
ImageIcon icon8 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\8.jpg");
JLabel jLabel8 = new JLabel(icon8);
jLabel8.setBounds(315,105,105,105);
this.getContentPane().add(jLabel8);
ImageIcon icon9 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\9.jpg");
JLabel jLabel9 = new JLabel(icon9);
jLabel9.setBounds(0,210,105,105);
this.getContentPane().add(jLabel9);
ImageIcon icon10 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\10.jpg");
JLabel jLabel10 = new JLabel(icon10);
jLabel10.setBounds(105,210,105,105);
this.getContentPane().add(jLabel10);
ImageIcon icon11 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\11.jpg");
JLabel jLabel11 = new JLabel(icon11);
jLabel11.setBounds(210,210,105,105);
this.getContentPane().add(jLabel11);
ImageIcon icon12 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\12.jpg");
JLabel jLabel12 = new JLabel(icon12);
jLabel12.setBounds(315,210,105,105);
this.getContentPane().add(jLabel12);
ImageIcon icon13 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\13.jpg");
JLabel jLabel13 = new JLabel(icon13);
jLabel13.setBounds(0,315,105,105);
this.getContentPane().add(jLabel13);
ImageIcon icon14 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\14.jpg");
JLabel jLabel14 = new JLabel(icon14);
jLabel14.setBounds(105,315,105,105);
this.getContentPane().add(jLabel14);
ImageIcon icon15 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\15.jpg");
JLabel jLabel15 = new JLabel(icon15);
jLabel15.setBounds(210,315,105,105);
this.getContentPane().add(jLabel15);
ImageIcon icon16 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\16.jpg");
JLabel jLabel16 = new JLabel(icon16);
jLabel16.setBounds(315,315,105,105);
this.getContentPane().add(jLabel16);
}
(1) 先打乱图片的下标(随机实现)
private void initData() {
int[] tempArr = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
Random rand = new Random();
for (int i = 0; i < tempArr.length; i++) {
int index = rand.nextInt(tempArr.length);
int temp = tempArr[i];
tempArr[i] = tempArr[index];
tempArr[index] = temp;
}
for (int i = 0; i < tempArr.length; i++) {
result[i/4][i%4] = tempArr[i];
}
}
(2)此时,result二维数组已经存储了相应的图片下标
- 打乱效果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/b8945106b03240a99a0494579d31e2f6.png)
- 代码:
private void initImage(){
for (int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++){
ImageIcon icon = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\" + result[i][j] + ".jpg");
JLabel jLabel = new JLabel(icon);
jLabel.setBounds(j * 105 ,i * 105 ,105,105);
jLabel.setBorder(new BevelBorder(1));
this.getContentPane().add(jLabel);
}
}
美化背景
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/46726eb6662e4a5a9729f68740b4776c.png)
private void initImage(){
for (int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++){
ImageIcon icon = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\" + result[i][j] + ".jpg");
JLabel jLabel = new JLabel(icon);
jLabel.setBounds(j * 105 + 83,i * 105 + 134,105,105);
jLabel.setBorder(new BevelBorder(1));
this.getContentPane().add(jLabel);
}
}
JLabel background = new JLabel(new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\background.png"));
background.setBounds(40, 40, 508, 560);
this.getContentPane().add(background);
}