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

📄 mainframe.java

📁 一个Java开发的小游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.*;

/*
 * MainFrame.java
 *
 * Created on 2008年2月18日, 下午9:04
 */

/**
 *
 * @author  Jihang.Chen
 */
public class MainFrame extends javax.swing.JFrame {
    
    /** Creates new form NewJFrame */
    public final int WIDTH = 16;
    public final int HEIGHT = 9;
    public final int ImageNum = 16;
    public final File ImagePathName = new File("Image\\");
    
   
    public String ImageName;
    public JLabel jLabel;
    public LianLianKan ImageArray;
    public int scrX;
    public int scrY;
    public int dstX;
    public int dstY;
    public int okTimes = 0;
    
    private int state = 0;
    private int line = 0;
    private JLabel scrLabel;
    private JLabel dstLabel;
    private Point start;
    private Point first;    
    private Point second;   
    private Point finish;
    private int sleepTime = 100;
    private String ImagePath;
    private final int maxTime = 500;
    private ProgressBarThread progressBarThread;
    
    public MainFrame() {
        initComponents();
        initAgain();
    }
    public void initAgain(){
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screenSize.width - 1000)/2;
        int y = (screenSize.height - 700)/2;
        setLocation(x, y);
        jProgressBar.setMaximum(maxTime);
        ImagePath = ImagePathName.getAbsolutePath();
        this.getContentPane().setBackground(Color.WHITE);
        jPanel.setBackground(Color.WHITE);
        jButtonStart.setEnabled(false);

        startGame();
    }
    public void startGame(){
        okTimes = 0;
        ImageArray = new LianLianKan(WIDTH,HEIGHT,ImageNum);
        int m=0;
        int [] record = new int [HEIGHT*WIDTH/2];
        for(int j=1; j <= HEIGHT;j++){
            for(int i=1; i <= WIDTH; i++){
                double k = java.lang.Math.random();
                int r = (int) (k * ImageNum); 
                jLabel = new JLabel();
                if(j <= (HEIGHT/2) || (j == HEIGHT/2 +1 && i <= WIDTH/2)){ // the first half
                    ImageName = ImagePath + '\\' + r +".jpg";
                    record[(j-1) * WIDTH + (i-1)] = r;
                }
                else{
                    r = record[m];
                    m = m + 2;
                    if(m >= WIDTH*HEIGHT/2 )
                        m = 1;
                    ImageName = ImagePath + '\\' + r +".jpg";
                }
                jLabel.setIcon(new ImageIcon(ImageName));
                jPanel.add(jLabel);
                ImageArray.setImageArray(i,j,r);
                jLabel.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mousePressed(java.awt.event.MouseEvent evt) {
                        jLabelMousePressed(evt);
                    }
                });
            }
        }
        progressBarThread = new ProgressBarThread();
        progressBarThread.start();
    }
    public void reStartGame(){
        okTimes = 0;
        int [] record = new int [HEIGHT*WIDTH/2];
        int m = 0;
        for(int j=1; j <= HEIGHT;j++){
            for(int i=1; i <= WIDTH; i++){
                double k = java.lang.Math.random();
                int val = (int) (k * ImageNum); 
                if(j <= (HEIGHT/2) || (j == HEIGHT/2 +1 && i <= WIDTH/2)){ // the first half
                    ImageName = ImagePath + '\\' + val +".jpg";
                    record[(j-1) * WIDTH + (i-1)] = val;
                }
                else{
                    val = record[m];
                    m = m + 2;
                    if(m >= WIDTH*HEIGHT/2 )
                        m = 1;
                    ImageName = ImagePath + '\\' + val +".jpg";
                }
                ((JLabel)jPanel.getComponent((j-1)*WIDTH + i-1)).setIcon(new ImageIcon(ImageName));
                ImageArray.setImageArray(i,j,val);
            }
        }
        progressBarThread = new ProgressBarThread();
        progressBarThread.start();
        jButtonArrange.setEnabled(true);
    }
    private void jLabelMousePressed(java.awt.event.MouseEvent evt){
        
        int x = evt.getXOnScreen();
        int y = evt.getYOnScreen();

        Point p = jPanel.getLocationOnScreen();
        x = x - p.x;
        y = y - p.y;
        
        x = x/55 + 1;
        y = (y + 2 +1)/54 + 1;

        if(!ImageArray.isNullImage(x,y)){
            if(state == 0){
                line = 0;
                state = 1;
                scrX = x;
                scrY = y;
                scrLabel = (JLabel)evt.getSource();
                scrLabel.setBorder(new LineBorder(Color.RED));
                start = getCenterPoint(scrX,scrY);
             }
            else if(state == 1){
                state = 0;
                dstX = x;
                dstY = y;
                dstLabel = (JLabel)evt.getSource();
                dstLabel.setBorder(new LineBorder(Color.RED));
                finish = getCenterPoint(dstX,dstY);
            }
            if(state == 0){
                if(ImageArray.ifImageSame(scrX,scrY,dstX,dstY)){
                     
                    if(ImageArray.ifCanOneLineConnect(scrX,scrY,dstX,dstY)){

                        scrLabel.setBorder(null);
                        dstLabel.setBorder(null);
                        progressBarThread.addTime(maxTime/100);
                        line = 1;
                        okTimes++;
                        this.paint(this.getGraphics());
                        if(okTimes == WIDTH * HEIGHT/2){
                            showPassDialog();
                            
                        }
                    }
                    else if(ImageArray.ifCanTwoLinesConnect(scrX,scrY,dstX,dstY)){

                        scrLabel.setBorder(null);
                        dstLabel.setBorder(null);
                        progressBarThread.addTime(maxTime/100);
                        line = 2;
                        first = getCenterPoint(ImageArray.getFirstX(),ImageArray.getFirstY());
                        okTimes++;
                        this.paint(this.getGraphics());
                        if(okTimes == WIDTH * HEIGHT/2){
                            showPassDialog();
                        }
                    }
                    else if(ImageArray.ifCanThreeLinesConnect(scrX,scrY,dstX,dstY)){

                        scrLabel.setBorder(null);
                        dstLabel.setBorder(null);
                        progressBarThread.addTime(maxTime/100);
                        line = 3;
                        first = getCenterPoint(ImageArray.getFirstX(),ImageArray.getFirstY());
                        second = getCenterPoint(ImageArray.getSecondX(),ImageArray.getSecondY());                        
                        okTimes++;
                        this.paint(this.getGraphics());
                        if(okTimes == WIDTH * HEIGHT/2){
                            showPassDialog();
                        }
                    }
                    else{
                        state = 1;
                        scrX = dstX;
                        scrY = dstY;
                        scrLabel.setBorder(null);
                        scrLabel = dstLabel;
                        scrLabel.setBorder(new LineBorder(Color.red));
                        start = finish;
                    }
                }
                else{
                    
                    state = 1;
                    scrX = dstX;
                    scrY = dstY;
                    scrLabel.setBorder(null);
                    scrLabel = dstLabel;
                    scrLabel.setBorder(new LineBorder(Color.red));
                    start = finish;
                   
                }
            }
        }
             
    }
    public Point getCenterPoint(int x,int y){
        Point centerPoint = new Point();
        centerPoint.x = 25 +(x-1)*55;
        centerPoint.y = 50 + (y-1)*54;
        centerPoint.x = centerPoint.x + jPanel.getLocation().x;
        centerPoint.y = centerPoint.y + jPanel.getLocation().y;

        return centerPoint;
    }
    public void paint(Graphics g){
       super.paint(g);
       if(line == 1){
            g.setColor(Color.red);
            g.drawLine(start.x,start.y,finish.x,finish.y);

⌨️ 快捷键说明

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