阶段项目中的添加图片和打乱图片

package Puzzlegame.com.wxj.ui;

import javax.swing.*;

import java.util.Random;

public class GameJframe extends JFrame {

//游戏主界面

//创建一个二维数组

//目的:管理数据

//加载图片的时候,会根据二维数组中的数据进行加载

int \[\]\[\] data=new int44;

public GameJframe(){

//初始化界面

initJFrame();

//初始化菜单

initJmenuBar();

//初始化数据(打乱)

initData();

//初始化图片(根据打乱之后的结果去加载图片)

initImage();

//让界面显示出来

this.setVisible(true);

}

//初始化数据(打乱)

private void initData() {

//1.定义一个数组

int \[\]tempArr={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

//2.打乱数组中的数据顺序

//遍历数组,得到每一个元素,拿着每一个元素跟随机索引上的数据交换

Random r=new Random();

for (int i = 0; i < tempArr.length; i++) {

//获取到随机索引

int index=r.nextInt(tempArr.length);

//拿着遍历到的每一个数据,跟随机索引上的数据进行交换。

int temp=tempArri;

tempArri=tempArrindex;

tempArrindex=temp;

}

//5.给二维数组添加数据

//解法一

//遍历一维数组tempArr得到每一个元素,把每一个元素依次添加到二维数组中

for (int i = 0; i < tempArr.length; i++) {

datai/4i%4=tempArri;

}

}

//初始化图片

//添加图片的时候,就需要按照二维数组中的数据添加图片

private void initImage() {

//外循环

for (int i = 0; i < 4; i++) {

//内循环

for (int j = 0; j < 4; j++) {

//获取当前要加载图片的序号

int num= dataij;

//创建一个JLabel对象(管理容器)

JLabel jLabel=new JLabel(new ImageIcon("F:\\javaEE\\src\\Puzzlegame\\com\\wxj\\ui\\image\\animal\\animal3\\"+num+".jpg"));

//指定图片的位置

jLabel.setBounds(105*j,105*i,105,105);

//把管理容器添加到界面中

this.getContentPane().add(jLabel);

}

}

}

private void initJmenuBar() {

//创建整个菜单对象

JMenuBar jMenuBar=new JMenuBar();

//创建菜单上的两个选项的对象(功能 关于我们)

JMenu functionJMenu=new JMenu("功能");

JMenu aboutJMenu=new JMenu("关于我们");

//创建选项下面的条目对象

JMenuItem replayItem=new JMenuItem("重新游戏");

JMenuItem reLoginItem=new JMenuItem("重新登录");

JMenuItem closeItem=new JMenuItem("关闭游戏");

JMenuItem accountItem=new JMenuItem("公众号");

//将每一个选项下面的条目添加到选项中

functionJMenu.add(replayItem);

functionJMenu.add(reLoginItem);

functionJMenu.add(closeItem);

aboutJMenu.add(accountItem);

//将菜单里的两个选项添加到菜单当中

jMenuBar.add(functionJMenu);

jMenuBar.add(aboutJMenu);

//给整个界面设置菜单

this.setJMenuBar(jMenuBar);

}

//初始化界面

private void initJFrame() {

//设置界面的宽高

this.setSize(603,680);

//设置界面的标题

this.setTitle("拼图单机版 V1.0");

//设置界面置顶

this.setAlwaysOnTop(true);

//设置界面居中

this.setLocationRelativeTo(null);

//设置游戏的关闭模式

this.setDefaultCloseOperation(3);

//取消默认的居中放置,只有取消了才能按照xy轴的形式添加组件

this.setLayout(null);

}

}

相关推荐
卓怡学长1 天前
w180springboot基于Java的悠扬乐器管理
java·spring boot·mysql·spring·maven·intellij-idea
卓怡学长1 天前
w181springboot机场乘客服务系统
java·数据库·spring boot·spring·intellij-idea
卓怡学长1 天前
w192基于springboot教务管理系统
java·数据库·spring boot·spring·maven·intellij-idea
EXI-小洲2 天前
MacOS 环境下 IntelliJ IDEA 的 Maven 配置指南
macos·maven·intellij-idea
小小安于乱3 天前
解决IDEA的git插件pull代码冲突但右下角不提示消息问题
java·git·intellij-idea
EXI-小洲3 天前
MacOS IDEA将本地项目代码上传至SVN空仓库
macos·svn·intellij-idea
学无止步_穷其一生4 天前
把 DeepSeek Harness 智能体嵌进 IntelliJ IDEA —— 类 Qoder 的 AI 编程插件
人工智能·intellij-idea·idea插件·deepseek
极创信息6 天前
国产化信创适配认证高频术语:信创适配、软件自主可控、国产化率、代码溯源率、代码自主率、代码开源率是什么?
java·python·struts·eclipse·开源·php·hibernate
典典分享指南7 天前
用指纹算法给文章去重:mid_signature 实战
jvm·maven·intellij-idea·jetty
冷雨夜中漫步8 天前
IDEA CodeStyle代码风格格式化模板
java·ide·intellij-idea