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

📄 ninegrids.java

📁 用JAVA实现的九方格问题
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * NineGrids.java
 *
 * Created on 2006年12月10日, 下午9:07
 */

package ninegrids;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Timer;
import java.util.Date;
import java.util.TimerTask;

public class NineGrids extends javax.swing.JFrame {
    public Timer timer;
    public int[][] GridArray=new int[3][3];
    JFrame frame1=new JFrame();
    int MessageType=JOptionPane.QUESTION_MESSAGE;
    int i,j,x,y,Count;
    int ArrayLength=3;
    String Second="2";
    
    public void InitialGrids(){
        Count=0;
        GridArray[0][0]=2;GridArray[0][1]=8;GridArray[0][2]=3;
        GridArray[1][0]=1;GridArray[1][1]=6;GridArray[1][2]=4;
        GridArray[2][0]=7;GridArray[2][1]=0;GridArray[2][2]=5;
        Grid1.setText("");
        Grid2.setText("");
        Grid3.setText("");
        Grid4.setText("");
        Grid5.setText("");
        Grid6.setText("");
        Grid7.setText("");
        Grid8.setText("");
        Grid9.setText("");
        Grid1.setBackground(Color.green);
        Grid2.setBackground(Color.green);
        Grid3.setBackground(Color.green);
        Grid4.setBackground(Color.green);
        Grid5.setBackground(Color.green);
        Grid6.setBackground(Color.green);
        Grid7.setBackground(Color.green);
        Grid8.setBackground(Color.green);
        Grid9.setBackground(Color.green);
        
    }
    public void SetColor(){
        if(Grid1.getText().equals("0")){
            Grid1.setBackground(Color.RED);
            Grid2.setBackground(Color.green);
            Grid3.setBackground(Color.green);
            Grid4.setBackground(Color.green);
            Grid5.setBackground(Color.green);
            Grid6.setBackground(Color.green);
            Grid7.setBackground(Color.green);
            Grid8.setBackground(Color.green);
            Grid9.setBackground(Color.green);
        }
        else if(Grid2.getText().equals("0")){
            Grid1.setBackground(Color.green);
            Grid2.setBackground(Color.RED);
            Grid3.setBackground(Color.green);
            Grid4.setBackground(Color.green);
            Grid5.setBackground(Color.green);
            Grid6.setBackground(Color.green);
            Grid7.setBackground(Color.green);
            Grid8.setBackground(Color.green);
            Grid9.setBackground(Color.green);
        }
        else if(Grid3.getText().equals("0")){
            Grid1.setBackground(Color.green);
            Grid2.setBackground(Color.green);
            Grid3.setBackground(Color.RED);
            Grid4.setBackground(Color.green);
            Grid5.setBackground(Color.green);
            Grid6.setBackground(Color.green);
            Grid7.setBackground(Color.green);
            Grid8.setBackground(Color.green);
            Grid9.setBackground(Color.green);
        }
        else if(Grid4.getText().equals("0")){
            Grid1.setBackground(Color.green);
            Grid2.setBackground(Color.green);
            Grid3.setBackground(Color.green);
            Grid4.setBackground(Color.RED);
            Grid5.setBackground(Color.green);
            Grid6.setBackground(Color.green);
            Grid7.setBackground(Color.green);
            Grid8.setBackground(Color.green);
            Grid9.setBackground(Color.green);
        }
        else if(Grid5.getText().equals("0")){
            Grid1.setBackground(Color.green);
            Grid2.setBackground(Color.green);
            Grid3.setBackground(Color.green);
            Grid4.setBackground(Color.green);
            Grid5.setBackground(Color.RED);
            Grid6.setBackground(Color.green);
            Grid7.setBackground(Color.green);
            Grid8.setBackground(Color.green);
            Grid9.setBackground(Color.green);
        }
        else if(Grid6.getText().equals("0")){
            Grid1.setBackground(Color.green);
            Grid2.setBackground(Color.green);
            Grid3.setBackground(Color.green);
            Grid4.setBackground(Color.green);
            Grid5.setBackground(Color.green);
            Grid6.setBackground(Color.RED);
            Grid7.setBackground(Color.green);
            Grid8.setBackground(Color.green);
            Grid9.setBackground(Color.green);
        }
        else if(Grid7.getText().equals("0")){
            Grid1.setBackground(Color.green);
            Grid2.setBackground(Color.green);
            Grid3.setBackground(Color.green);
            Grid4.setBackground(Color.green);
            Grid5.setBackground(Color.green);
            Grid6.setBackground(Color.green);
            Grid7.setBackground(Color.RED);
            Grid8.setBackground(Color.green);
            Grid9.setBackground(Color.green);
        }
        else if(Grid8.getText().equals("0")){
            Grid1.setBackground(Color.green);
            Grid2.setBackground(Color.green);
            Grid3.setBackground(Color.green);
            Grid4.setBackground(Color.green);
            Grid5.setBackground(Color.green);
            Grid6.setBackground(Color.green);
            Grid7.setBackground(Color.green);
            Grid8.setBackground(Color.RED);
            Grid9.setBackground(Color.green);
        }
        else if(Grid9.getText().equals("0")){
            Grid1.setBackground(Color.green);
            Grid2.setBackground(Color.green);
            Grid3.setBackground(Color.green);
            Grid4.setBackground(Color.green);
            Grid5.setBackground(Color.green);
            Grid6.setBackground(Color.green);
            Grid7.setBackground(Color.green);
            Grid8.setBackground(Color.green);
            Grid9.setBackground(Color.RED);
        }
    }
    public void Display(String s,int index){
        Print1.setText(s);
        x=ZeroPosX(GridArray);
        y=ZeroPosY(GridArray);
        GridAction gridaction=new GridAction(GridArray,x,y,index);
        for(i=0;i<ArrayLength;++i){
            for(j=0;j<ArrayLength;++j){
                GridArray[i][j]=gridaction.GridArray[i][j];
            }
        }
                
        DispGrids();
        DispArray();
    }
     public void DispGrids(){
         Grid1.setText(String.valueOf(GridArray[0][0]));
         Grid2.setText(String.valueOf(GridArray[0][1]));
         Grid3.setText(String.valueOf(GridArray[0][2]));
         Grid4.setText(String.valueOf(GridArray[1][0]));
         Grid5.setText(String.valueOf(GridArray[1][1]));
         Grid6.setText(String.valueOf(GridArray[1][2]));
         Grid7.setText(String.valueOf(GridArray[2][0]));
         Grid8.setText(String.valueOf(GridArray[2][1]));
         Grid9.setText(String.valueOf(GridArray[2][2]));
         Print2.setText(String.valueOf(GridArray[0][0]));
         Print3.setText(String.valueOf(GridArray[0][1]));
         Print4.setText(String.valueOf(GridArray[0][2]));
         Print5.setText(String.valueOf(GridArray[1][0]));
         Print6.setText(String.valueOf(GridArray[1][1]));
         Print7.setText(String.valueOf(GridArray[1][2]));
         Print8.setText(String.valueOf(GridArray[2][0]));
         Print9.setText(String.valueOf(GridArray[2][1]));
         Print10.setText(String.valueOf(GridArray[2][2]));
         SetColor();
     }
     
     public void DispArray(){
         int i,j;
         for(i=0;i<ArrayLength;++i){
             for(j=0;j<ArrayLength;++j){
                 System.out.print(GridArray[i][j]+" ");
             }
             System.out.println("");
         }
     }
    
    public int ZeroPosX(int[][] GridArray){
        int Result=-1;
        for(i=0;i<ArrayLength;++i){
            for(j=0;j<ArrayLength;++j){
                if(GridArray[i][j]==0){
                    Result=i;
                }
            }
        }
        return Result;
    }
    public int ZeroPosY(int[][] GridArray){
        int Result=-1;

⌨️ 快捷键说明

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