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

📄 llkjm.java

📁 用Java编写的连连看
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * DisplayApplet.java
 *
 * Created on 2007年1月12日, 下午5:44
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import java.io.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import javax.swing.JMenuBar;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;

import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import java.awt.event.*;

public class Llkjm extends JFrame implements ActionListener{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JMenu menuGame,menuSelect,menuHelp;
    JMenuItem jmiEasy,jmiNormal,jmiHard,jmiGiveUp;
    static JMenuItem jmiEnd;
    JMenuItem jmiExplain,jmiAbout,jmiPaihang;
    JMenuItem jmiReset,jmiHint;
    static int hintCount = 6,resetCount = 4;
    Container container;
    JLayeredPane pane = new JLayeredPane();
    JPanel backPanel = new JPanel();
    JPanel head = new JPanel();
    BgPanel p;
    static DrawPicturesPanel drawPicturesPanel = new DrawPicturesPanel();
    static DrawLabelPanel drawLabelPanel = new DrawLabelPanel();
    static TimeRestrict timeRestrict = new TimeRestrict();
    
                    
    /** Creates a new instance of DisplayApplet */
    public Llkjm() {
        setTitle("连连看");
        setResizable(false);
        
        menuGame = new JMenu("游戏(G)");
        menuSelect = new JMenu("选项(S)");
        menuHelp = new JMenu("帮助(H)");
        menuGame.setMnemonic('G');
        menuSelect.setMnemonic('S');
        menuHelp.setMnemonic('H');
        
        JMenuBar menubar = new JMenuBar();
        setJMenuBar(menubar);
        menubar.setVisible(true);
        menubar.add(menuGame);
        menubar.add(menuSelect);
        menubar.add(menuHelp);
        
        jmiEasy = new JMenuItem("初级(C)");
        jmiEasy.addActionListener(this);
        jmiEasy.setMnemonic('C');
        jmiNormal = new JMenuItem("中级(Z)");
        jmiNormal.addActionListener(this);
        jmiNormal.setMnemonic('Z');
        jmiHard = new JMenuItem("高级(G)");
        jmiHard.addActionListener(this);
        jmiHard.setMnemonic('G');
        jmiEnd = new JMenuItem("结束(E)");
        jmiEnd.addActionListener(this);
        jmiEnd.setMnemonic('E');
        jmiGiveUp = new JMenuItem("退出(X)");
        jmiGiveUp.addActionListener(this);
        jmiGiveUp.setMnemonic('X');
        jmiExplain = new JMenuItem("游戏说明(M)");
        jmiExplain.addActionListener(this);
        jmiExplain.setMnemonic('M');
        jmiAbout = new JMenuItem("关于(A)");
        jmiAbout.addActionListener(this);
        jmiAbout.setMnemonic('A');
        jmiHint = new JMenuItem("提示(H)");
        jmiHint.addActionListener(this);
        jmiHint.addActionListener(drawLabelPanel);
        jmiHint.setMnemonic('H');
        jmiHint.setEnabled(false);
        jmiReset = new JMenuItem("重排(R)");
        jmiReset.addActionListener(this);
        jmiReset.addActionListener(drawLabelPanel);
        jmiReset.setMnemonic('R');
        jmiReset.setEnabled(false);
        jmiPaihang = new JMenuItem("排行(P)");
        jmiPaihang.addActionListener(this);
        jmiPaihang.setMnemonic('P');
        jmiEnd.setEnabled(false);
        
        menuGame.add(jmiEasy);
        menuGame.add(jmiNormal);
        menuGame.add(jmiHard);
        menuGame.add(jmiEnd);
        menuGame.add(jmiGiveUp);
        menuSelect.add(jmiHint);
        menuSelect.add(jmiReset);
        menuSelect.add(jmiPaihang);
        menuHelp.add(jmiExplain);
        menuHelp.add(jmiAbout);
        
        p = new BgPanel();
        p.setBounds(0,0,800,700);
        pane.add(p, JLayeredPane.DEFAULT_LAYER);
        
        container = getContentPane();
        container.add(pane);
    }
/*    static{
    	Font fnt = new Font("宋体" , 0 ,14);     
		  UIManager.put("Menu.font",fnt);       
		  UIManager.put("MenuItem.font",fnt);
		  UIManager.put("TitledBorder.font",fnt);   
    }
*/    
    public static void main(String[] args){
        Llkjm frame = new Llkjm();
        String lnfName = "org.jvnet.substance.skin.SubstanceCremeLookAndFeel";
		try {
		    UIManager.setLookAndFeel(lnfName);
		    SwingUtilities.updateComponentTreeUI(frame);
		}
		catch (UnsupportedLookAndFeelException ex1) {
		    System.err.println("Unsupported LookAndFeel: " + lnfName);
		}
		catch (ClassNotFoundException ex2) {
		    System.err.println("LookAndFeel class not found: " + lnfName);
		}
		catch (InstantiationException ex3) {
		    System.err.println("Could not load LookAndFeel: " + lnfName);
		}
		catch (IllegalAccessException ex4) {
		    System.err.println("Cannot use LookAndFeel: " + lnfName);
		}
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800,700);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e){
        int lengthOfx = 2,lengthOfy = 2;
        if (e.getSource() instanceof JMenuItem){
            if (e.getSource() == jmiEasy || e.getSource() == jmiNormal || e.getSource() == jmiHard){
                if (e.getSource() == jmiEasy){
                    lengthOfx = 4;
                    lengthOfy = 4;
                    timeRestrict.setDelay(200);
                }
                if (e.getSource() == jmiNormal){
                    lengthOfx = 6;
                    lengthOfy = 6;
                    timeRestrict.setDelay(400);
                }
                if (e.getSource() == jmiHard){
                    lengthOfx = 6;
                    lengthOfy = 10;
                    timeRestrict.setDelay(700);
                }
                jmiEasy.setEnabled(false);
                jmiNormal.setEnabled(false);
                jmiHard.setEnabled(false);
                jmiEnd.setEnabled(true);
                jmiHint.setEnabled(true);
                jmiReset.setEnabled(true);
                Image img = ResourseReader.getImageFromJar("/small-pic/bg.jpg", this.getClass());
                JLabel backLabel = new JLabel(null,new ImageIcon(img),SwingConstants.CENTER);
                backPanel.add(backLabel);
                backPanel.setBounds(0, 20, 800, 650);
                pane.remove(p);
                pane.add(backPanel, JLayeredPane.DEFAULT_LAYER );
                drawPicturesPanel = new DrawPicturesPanel(lengthOfx,lengthOfy);
                drawLabelPanel = new DrawLabelPanel(hintCount,resetCount);
                timeRestrict = new TimeRestrict(true);
                JPanel timePanel = new JPanel();
                timePanel.setBounds(0,0,400,23);
                timePanel.setBorder(new LineBorder(Color.black,2));
                timePanel.setLayout(new BorderLayout());
                timePanel.add(timeRestrict,BorderLayout.CENTER);
                head.setBounds(0,0,800,24);
                head.setLayout(new GridLayout(1,2,0,0));
                head.add(drawLabelPanel);
                head.add(timePanel);
                pane.add(head,JLayeredPane.PALETTE_LAYER );
                pane.add(drawPicturesPanel,JLayeredPane.POPUP_LAYER );
                container.validate();
            }
            if (e.getSource() == jmiGiveUp) {
                System.exit(0);
            }
            if (e.getSource() == jmiEnd) {
                head.removeAll();
                pane.removeAll();
                pane.add(p);
                container.repaint();
                timeRestrict.suspend();
                jmiEasy.setEnabled(true);
                jmiNormal.setEnabled(true);
                jmiHard.setEnabled(true);
                jmiEnd.setEnabled(false);
                jmiHint.setEnabled(false);
                jmiReset.setEnabled(false);
                hintCount = 6;
                resetCount = 4;
                DrawPicturesPanel.setCount(1);
            }
            if (e.getSource() == jmiHint){
                drawPicturesPanel.hint(drawPicturesPanel.randomNumber,drawPicturesPanel.buttonArray,false);
                if (--hintCount == 0 || DrawPicturesPanel.isNull && DrawPicturesPanel.getCount() == 3) {
					jmiHint.setEnabled(false);
				}
                container.validate();
            }
            if (e.getSource() == jmiReset){
                drawPicturesPanel.reset(drawPicturesPanel.randomNumber,drawPicturesPanel.buttonArray);
                if (--resetCount == 0 || DrawPicturesPanel.isNull && DrawPicturesPanel.getCount() == 3) {
					jmiReset.setEnabled(false);
				}
                container.validate();
            }
            if (e.getSource() == jmiPaihang){
                try {
                    PaiHang.showPaiHang();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (e.getSource() == jmiExplain){
                new Explain();
            }
            if (e.getSource() == jmiAbout){
                JOptionPane.showMessageDialog( Llkjm.this,
                    "版本--------连连看2.0\n" +
                    "制作者------张磊",
                    "关于", JOptionPane.PLAIN_MESSAGE );
            }
        }
    }
}
class BgPanel extends JPanel {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Image img = ResourseReader.getImageFromJar("/bg/firstbg.jpg", this.getClass());
	ImageIcon bg = new ImageIcon(img);
	
	protected void paintComponent(Graphics g){
        super.paintComponent(g);
       
        g.drawImage(bg.getImage(),0,0,getWidth(),getHeight(),null);
    }
}

class DrawPicturesPanel extends JPanel implements ActionListener{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int IMGNUM = 64;
    private static int count = 1;
    private static int score = 0;
    int jbtW = 61;
    int jbtH = 71;
    int lengthOfx,lengthOfy;
    int distance;
    int x1 = -1;
    int y1 = -1;
    int x2 = -1;
    int y2 = -1;
    static boolean isNull;
    JButton formerButton = new JButton();
    Icon[][] pictures;
    JButton[][] buttonArray ;
    int[][] randomNumber;
    int[] middlePoints = {0,0,0,0};
    int swerveNum;
    Graphics g;
    
    public static void setCount(int newCount){
        count = newCount;
    }
    public static int getCount(){
        return count;
    }
    //分数算法
    private static void setScore(int d,int countN){
        if (d <= 3) {
            score += d*(10+Llkjm.hintCount*Llkjm.resetCount)*countN + 
                    (TimeRestrict.WIDTH - Llkjm.timeRestrict.getWidth()) * countN;
            Llkjm.timeRestrict.setWidth(Llkjm.timeRestrict.getWidth()+10);
        }
        else if (d <= 9) {
            score += d*(20+Llkjm.hintCount*Llkjm.resetCount)*countN + 
                    (TimeRestrict.WIDTH - Llkjm.timeRestrict.getWidth()) * countN;
            Llkjm.timeRestrict.setWidth(Llkjm.timeRestrict.getWidth()+12);
        }
        else if (d <= 12){
            score += d*(30+Llkjm.hintCount*Llkjm.resetCount)*countN + 
                    (TimeRestrict.WIDTH - Llkjm.timeRestrict.getWidth()) * countN;
            Llkjm.timeRestrict.setWidth(Llkjm.timeRestrict.getWidth()+16);
        }
        else if (d <= 18) {
            score += d*(50+Llkjm.hintCount*Llkjm.resetCount)*countN + 
                    (TimeRestrict.WIDTH - Llkjm.timeRestrict.getWidth()) * countN;
            Llkjm.timeRestrict.setWidth(Llkjm.timeRestrict.getWidth()+20);
        }
        else {
            score += d*(100+Llkjm.hintCount*Llkjm.resetCount)*countN + 
                    (TimeRestrict.WIDTH - Llkjm.timeRestrict.getWidth()) * countN;
            Llkjm.timeRestrict.setWidth(Llkjm.timeRestrict.getWidth()+40);
        }
    }
    
    public static int getScore(){
        return score;
    }
    
    public DrawPicturesPanel(){
        super();
    }
    
    public DrawPicturesPanel(int newlengthOfx,int newlengthOfy){
        lengthOfx = newlengthOfx;
        lengthOfy = newlengthOfy;
        setLayout(new GridLayout(lengthOfx + 2,lengthOfy + 2,0,0));// 设置布局管理器
        setBounds((800 - (lengthOfy + 2) * jbtW) / 2,(650 - (lengthOfx + 2 )* jbtH) / 2,
                lengthOfy * jbtW + jbtW * 2 ,lengthOfx * jbtH + jbtH * 2);
        setOpaque(false);
        pictures = FormButton(lengthOfx,lengthOfy);
        buttonArray = new JButton[lengthOfx + 2][lengthOfy + 2];
        
        
        // 实例化按钮
        for (int i = 0; i < lengthOfx + 2 ; i++)
            for (int j = 0 ; j < lengthOfy + 2; j++)
                if (i == 0 || i == lengthOfx + 1 || j == 0 || j == lengthOfy + 1){
                    JButton button = new JButton(new ImageIcon());
                    button.setVisible(false);
                    button.setSelected(false);
                    add(button);
                    buttonArray[i][j] = button;
                }
                else{
                    JButton button = new JButton(pictures[i - 1][j - 1]);
               //     button.setIcon(pictures[i - 1][j - 1]);
                    button.setBorderPainted(false);
                    button.addActionListener(this);
                    button.addActionListener(Llkjm.drawLabelPanel);
                    add(button);
                    buttonArray[i][j] = button;
                }
        hint(randomNumber,buttonArray,true);
    }
    
    public void newBoard(){
        count++;
        pictures = FormButton(lengthOfx,lengthOfy);
        // 实例化按钮
        for (int i = 1; i <= lengthOfx ; i++)
            for (int j = 1 ; j <= lengthOfy; j++){
                buttonArray[i][j].setBorderPainted(false);
                buttonArray[i][j].setBorder(null);
                buttonArray[i][j].setVisible(true);

⌨️ 快捷键说明

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