⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 对对碰游戏源代码 有课设报告 功能较多 积分 计时 棋盘变化等
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package PetsFinder;
import java.io.*;
import java.util.LinkedList;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.JComponent;
import java.util.Vector;
import java.awt.*;
import javax.swing.*;
import java.lang.Math;
import javax.sound.sampled.*;
import javax.swing.Timer;
import java.net.*;
import java.applet.AudioClip;

class BtnImage extends JButton    //生成按钮
{
    public int x,y;               //表示按钮的坐标
    public int samex=1;
    public int samey=1;
    public boolean sameToNextx=false;
    public boolean sameToNexty=false;
    public ImageIcon img;
    public int flag;
    boolean heng=false;
    boolean shu=false;
    boolean cleared=false;
    boolean clicked=false;
    public BtnImage(int x,int y) {
        this.x=x;
        this.y=y;
        int a=(int)(Math.random()*7);
        this.flag=a;
        String s="ima"+a+".gif";
        img=new ImageIcon(s);
        this.setIcon(img);
    }
    
}
class MainFrame extends JFrame implements ActionListener//主窗体
{
    File file6 = new File("file6.txt");
    File file8 = new File("file8.txt");
    File file12 = new File("file12.txt");
    Record record;
    ShowRecord showrecord;
    public JMenuBar menubar;
    public JMenu game;
    public JMenu heros;
    public JMenu easy;
    public JMenu help;
    public JMenu tishi;
    public JMenuItem start;
    public JMenuItem exit;
    public JMenuItem about;
    public JCheckBoxMenuItem music;
    public JMenuItem herosone;
    public JMenuItem youxitishi;
    public JMenuItem youxiwanfa;
    public JMenuItem b;
    public JMenuItem c;
    public JMenuItem d;
    public boolean go=false;
    public boolean changed=false;
    public boolean canClear=false;
    public BtnImage[][] ButtonImage;
    public JLabel showScore;
    public int score=0;
    public int xyz=8;
    public Vector clickedBtn=new Vector(2);
    boolean isPlaying = false;
    
    
    
    public MainFrame() {
        super("PetsFinder");
        setSize(700,660);
        this.setLayout(new BorderLayout());
        showScore=new JLabel("分数"+score);
        menubar=new JMenuBar();
        game=new JMenu("游戏");
        heros=new JMenu("英雄榜");
        easy=new JMenu("难度");
        help=new JMenu("帮助");
        
        tishi=new JMenu("提示");
        youxitishi=new JMenuItem("游戏提示");
        youxiwanfa=new JMenuItem("游戏玩法");
        music=new JCheckBoxMenuItem("背景音乐");
        start=new JMenuItem("开始游戏");
        exit=new JMenuItem("退出游戏");
        herosone=new JMenuItem("玩家排行");
        about=new JMenuItem("关于");
        b=new JMenuItem("6*6" );
        c=new JMenuItem("8*8");
        d=new JMenuItem("12*12");
        
        this.getContentPane().removeAll();
        final JPanel panelBack = new JPanel() {
            public void paintComponent(Graphics g) {
                ImageIcon img;
                img = new ImageIcon("1.jpg");
                g.drawImage(img.getImage(), 0, 0, null);
                
            }
        };
        this.add(panelBack,BorderLayout.CENTER);
        game.add(start);
        game.add(exit); 
        easy.add(b);
        easy.add(c);
        easy.add(d);
        help.add(about);
        help.add(music);
        heros.add(herosone);
        tishi.add(youxitishi);
        tishi.add(youxiwanfa);
        menubar.add(game);
        menubar.add(help);
        menubar.add(heros);
        menubar.add(easy);
        menubar.add(tishi);
       this.setJMenuBar(menubar);
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);}
        });
        this.setVisible(true);     
     music.addItemListener(new ItemListener()   
             {
                URL musicUrl = getClass().getResource("1.mid");
                AudioClip backGroundMusic = java.applet.Applet.newAudioClip(musicUrl);
                public void itemStateChanged(ItemEvent e)
                {

                    if(music.getState())
                    {
                        backGroundMusic.loop();                      
                    }else if(!music.getState())
                    {
                        backGroundMusic.stop();
                    }
                }
            });


        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                JPanel c = new JPanel();
                xyz=6;
                if(isPlaying==true)
                    
                {
                    getContentPane().removeAll();
                    creat(c);
                    score=0;
                    Hero.time=0;
                } else {
                    creat(panelBack);
                    replay();
                    score=0;
                          Hero.time=0;
                }
                start.setText("重新游戏");
                
            }
        });
        c.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                JPanel c = new JPanel();
                xyz=8;
                if(isPlaying==true)
                    
                {
                    getContentPane().removeAll();
                    
                    
                    creat(c);
                    score=0;
                          Hero.time=0;
                }
                
                
                
                else {
                    creat(panelBack);
                    replay();
                    score=0;     
                    Hero.time=0;
                    
                }
                start.setText("重新游戏");
                
                
            }
        });
        d.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                JPanel c = new JPanel();
                xyz=12;
                
                if(isPlaying==true) {
                    getContentPane().removeAll();
                    
                    creat(c);
                    score=0;    
                    Hero.time=0;
                    
                    
                } else {
                    creat(panelBack);
                    replay();
                    score=0;
                          Hero.time=0;
                }
                start.setText("重新游戏");
                
            }
        });
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               if(start.getText()=="开始游戏"){ 
                   creat(panelBack);
                   timeListener.t.start();
//                if(start.getText().equalsIgnoreCase("重新游戏")){
//                    timeListener.t.start();
//                    replay();
//                    score=0;
//                } else {
//                    
//                    replay();
//                    score=0;
//                }
               // creat(panelBack);
                   start.setText("重新游戏");
               }
               if(start.getText()=="重新游戏"){  
                    //creat(panelBack);
                   replay();
                   Hero.time=0;
                   timeListener.t.start();
                     score=0;
               }
            }});
            herosone.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    
                    showrecord=new ShowRecord(file6,
                            file8,file12);
                    showrecord.setVisible(true);
                    
                    
                    
                    
                }});        
                exit.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.exit(0);}});
                        youxitishi.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                 ;}});
                        about.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                JOptionPane msg = new JOptionPane();
                                JOptionPane.showMessageDialog(null,"北京工业大学计算机学院060701陈浩设计\n欢迎使用\nPetsFinder1.0版" );}});
                                youxiwanfa.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent e) {
                                        JOptionPane msg1 = new JOptionPane();
                                        JOptionPane.showMessageDialog(null,"点击相邻两个方格内动物交换位置,使得三个或三个以上动物相连,即可得分并重新排列行或列\n在最短的时间内达到规定的分数" );}});
    }
    
    void replay() {
        this.isPlaying = true;
        this.score=0;
        this.showScore.setText("分数"+score);
        for(int i=0;i<xyz;i++)//生成按钮对象
            for(int j=0;j<xyz;j++) {
            int a;
            a=getRandom();
            ButtonImage[i][j].x=i;
            ButtonImage[i][j].y=j;
            ButtonImage[i][j].flag=a;
            ButtonImage[i][j].setIcon(new ImageIcon("ima"+a+".gif"));
            ButtonImage[i][j].heng=false;
            ButtonImage[i][j].shu=false;
            ButtonImage[i][j].sameToNextx=false;
            ButtonImage[i][j].cleared=false;
            ButtonImage[i][j].clicked=false;
            ButtonImage[i][j].sameToNexty=false;
            ButtonImage[i][j].samex=1;
            ButtonImage[i][j].samex=1;
            ButtonImage[i][j].setEnabled(true);
            }
        this.canClear=false;
        this.changed=false;
        this.clickedBtn.removeAllElements();
        checkClear();
    }
    void creat(JPanel c) {
        this.remove(c);
        JPanel status=new JPanel();
        status.add(showScore);
        status.add(timeListener.显示时间);
        
        JPanel p1=new JPanel();
     p1.setLayout(new GridLayout(xyz,xyz));
         //p1.setLayout(null);
        Container container = new Container();
        container=this.getContentPane();
        container.add(status,BorderLayout.NORTH);
        container.add(p1,BorderLayout.CENTER);
        ButtonImage=new BtnImage[xyz][xyz];
        
        
        
        for(int i=0;i<xyz;i++)//生成按钮对象
            for(int j=0;j<xyz;j++) {
            ButtonImage[i][j]=new BtnImage(i,j);}
        
        for(int i=0;i<xyz;i++)
            for(int j=0;j<xyz;j++) {
            p1.add(ButtonImage[i][j]);
            ButtonImage[i][j].setSize(40,40);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -