import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class GameJFrame extends JFrame implements KeyListener, ActionListener {
//JFrame 界面,窗体
//子类呢?也表示界面,窗体
//规定:GameJFrame这个界面表示的就是游戏的主界面
//以后跟游戏相关的所有逻辑都写在这个类中
int date[][] = new int[4][4];
int win[][] = new int[4][4];
public GameJFrame(){
//设置界面的宽高
initJFrame();
//设置菜单
initJMenuBar();
//初始化数据
initdate();
//设置图片
initimage();
// 让显示显示出来,建议写在最后
this.setVisible(true);
}
int x=0;
int y=0;
int step = 0;
private void initdate() {
int[] arr ={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
for(int i = 0;i<arr.length;i++){
Random r = new Random();
int num = r.nextInt(arr.length);
int temp = arr[i];
arr[i]= arr[num];
arr[num]= temp;
}
int index = 0;
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
date[i][j]= arr[index];
index++;
}
}
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
if(date[i][j]==0)
{
x= i;
y = j;
}
}
}
}
private void initimage() {
this.getContentPane().removeAll();
JLabel jstep = new JLabel("步数:"+step);
jstep.setBounds(50,30,100,20);
this.getContentPane().add(jstep);
if(victory()){
JLabel win = new JLabel(new ImageIcon("D:\\JAVA\\pintu\\image\\win.png"));
win.setBounds(203,283,197,73);
this.getContentPane().add(win);
}
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
//创建一个图片ImageIcon的对象
//ImageIcon icon = new ImageIcon("D:\\JAVA\\pintu\\image\\animal\\animal2\\"+count+".jpg");
//创建一个Jlabel的对象(管理容器)
JLabel jLabel = new JLabel(new ImageIcon("image\\animal\\animal2\\"+date[i][j]+".jpg"));
//指定图片的位置
jLabel.setBounds(105*j+83,105*i+134,105,105);
jLabel.setBorder(new BevelBorder(1));
//把管理容器添加到界面中
//this.add(jLabel);
this.getContentPane().add(jLabel);
}
}
JLabel backgroud = new JLabel(new ImageIcon("image\\background.png"));
backgroud.setBounds(40,40,508,560);
this.getContentPane().add(backgroud);
this.getContentPane().repaint();
}
private boolean victory() {
int count = 1;
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
win[i][j] = count;
count++;
}
}
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
if(date[i][j]!=win[i][j])
{
return false;
}
}
}
return true;
}
JMenuItem repalyItem = new JMenuItem("重新开始");
JMenuItem reloginItem = new JMenuItem("重新登录");
JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("公共号");
private void initJMenuBar() {
//创建整个的菜单对象
JMenuBar jMenuBar = new JMenuBar();
//创建菜单上面的两个选项的对象(功能 关于我们)
JMenu functionJMenu = new JMenu("功能");
JMenu aboutJMenu = new JMenu("关于我们");
//创建选项下面的条目
repalyItem.addActionListener(this);
reloginItem.addActionListener(this);
closeItem.addActionListener(this);
accountItem.addActionListener(this);
//将每一个选项下面的条目集中到选项的中
functionJMenu.add(repalyItem);
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("拼图单机版 v.10");
//设置界面置顶
this.setAlwaysOnTop(true);
//设置界面居中
this.setLocationRelativeTo(null);
//设置默认的关闭模式
this.setDefaultCloseOperation(3);//0 表示DO_NOTHING_ON_CLOSE 通俗讲:关不掉
//1 表示HIDE_ON_CLOSE 通俗讲:界面关闭但虚拟机未关闭
//2 表示DISPOSE_ON_CLOSE 通俗讲:当开启多个界面的时候,只有把所有界面都关闭虚拟机才会停止。注意:所有界面都要设置成这个样子。
//3 表示EXIT_ON_CLOSE 通俗讲: 关掉一个界面,虚拟机就停止了。
//取消居中放置,按照xy轴的形式添加组件
this.setLayout(null);
this.addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode==65){
this.getContentPane().removeAll();
JLabel jLabel = new JLabel(new ImageIcon("D:\\JAVA\\pintu\\image\\animal\\animal1\\all.jpg"));
jLabel.setBounds(83,134,420,420);
this.getContentPane().add(jLabel);
this.getContentPane().repaint();
JLabel backgroud = new JLabel(new ImageIcon("image\\background.png"));
backgroud.setBounds(40,40,508,560);
this.getContentPane().add(backgroud);
this.getContentPane().repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
if(victory()){
return;
}
int keyCode = e.getKeyCode();
if(keyCode==37){
if(y==3){
return;
}
date[x][y]=date[x][y+1];
date[x][y+1]=0;
y++;
step++;
initimage();
}
if(keyCode==39){
if(y==0){
return;
}
date[x][y]=date[x][y-1];
date[x][y-1]=0;
y--;
step++;
initimage();
}
if(keyCode==38){
if(x==3){
return;
}
date[x][y]=date[x+1][y];
date[x+1][y]=0;
x++;
step++;
initimage();
}
if(keyCode==40){
if(x==0){
return;
}
date[x][y]=date[x-1][y];
date[x-1][y]=0;
x--;
step++;
initimage();
}
if(keyCode==65){
initimage();
}
if(keyCode==87){
int count = 1;
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
date[i][j] = count;
count++;
}
}
initimage();
}
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source==repalyItem){
step=0;
initdate();
initimage();
}
if(source==reloginItem){
this.setVisible(false);
new LoginJFrame();
}
if(source==closeItem)
{
System.exit(0);
}
if(source==accountItem){
JDialog dialog = new JDialog();
JLabel jLabel = new JLabel(new ImageIcon("D:\\JAVA\\pintu\\image\\about.png"));
jLabel.setBounds(0,0,258,258);
dialog.getContentPane().add(jLabel);
dialog.setSize(344,344);
dialog.setAlwaysOnTop(true);
dialog.setLocationRelativeTo(null);
dialog.setModal(true);
dialog.setVisible(true);
}
}
}
JAVA拼图小游戏 (美化界面)
最新推荐文章于 2024-07-22 18:54:55 发布
本文介绍了使用Java编写的拼图游戏,包括GameJFrame类的实现,关键接口如KeyListener和ActionListener的使用,以及游戏界面的初始化、图片处理和键盘事件响应。
522

被折叠的 条评论
为什么被折叠?



