Java 将颜色设置为数独单元格

Java 将颜色设置为数独单元格,java,applet,sudoku,Java,Applet,Sudoku,我一直在开发一个9x9数独小程序,我想为每个数字设置一个颜色。例如,如果单元格包含“1”,则单元格颜色应为蓝色。然而,我得到了错误。我花了几个小时试图找出答案,但每次都会给我带来不同的错误。这是我的密码: 谢谢你的帮助 import java.applet.* ; import java.awt.* ; /** * Solves a sudoku puzzle by recursion and backtracking */ public cla

我一直在开发一个9x9数独小程序,我想为每个数字设置一个颜色。例如,如果单元格包含“1”,则单元格颜色应为蓝色。然而,我得到了错误。我花了几个小时试图找出答案,但每次都会给我带来不同的错误。这是我的密码:

谢谢你的帮助

    import java.applet.* ;
    import java.awt.* ;

    /**
    * Solves a sudoku puzzle by recursion and backtracking
    */
    public class SimplifiedSudoku extends Applet implements Runnable
    {
    /** The model */
    protected int model[ ][ ] ;

    /** The view */
    protected Button view[ ][ ] ;

    /** Creates the model and sets up the initial situation */
    protected void createModel()
    {
    model = new int[9][9] ;

// Clear all cells
for( int row = 0; row < 9; row++ )
  for( int col = 0; col < 9; col++ )
  model[row][col] = 0 ;

// Create the initial situation
model[0][0] = 9 ;
model[0][4] = 2 ;
model[0][6] = 7 ;
model[0][7] = 5 ;

model[1][0] = 6 ;
model[1][4] = 5 ;
model[1][7] = 4 ;

model[2][1] = 2 ;
model[2][3] = 4 ;
model[2][7] = 1 ;

model[3][0] = 2 ;
model[3][2] = 8 ;

model[4][1] = 7 ;
model[4][3] = 5 ;
model[4][5] = 9 ;
model[4][7] = 6 ;

model[5][6] = 4 ;
model[5][8] = 1 ;

model[6][1] = 1 ;
model[6][5] = 5 ;
model[6][7] = 8 ;

model[7][1] = 9 ;
model[7][4] = 7 ;
model[7][8] = 4 ;

model[8][1] = 8 ;
model[8][2] = 2 ;
model[8][4] = 4 ;
model[8][8] = 6 ;
 }

/** Creates an empty view */
protected void createView()
{
 setLayout( new GridLayout(9,9) ) ;

 view = new Button[9][9] ;
 // setBackground(Color.blue);

// something.setBackground(Color.red);

// Create an empty view
for( int row = 0; row < 9; row++ )
  for( int col = 0; col < 9; col++ )
{
  view[row][col]  = new Button() ;
  add( view[row][col] ) ;

}
}

  /** Updates the view from the model */
  protected void updateView()
  {
  for( int row = 0; row < 9; row++ )
  for( int col = 0; col < 9; col++ )

  if( model[row][col] != 0 )
  {
  view[row][col].setLabel( String.valueOf(model[row][col]) ) ;
  }
  else
  view[row][col].setLabel( "" ) ;
import java.applet.*;
导入java.awt.*;
/**
*通过递归和回溯解决数独难题
*/
公共类SimplifiedSudoku扩展Applet实现可运行
{
/**模型*/
受保护的int模型[];
/**景色*/
受保护的按钮视图[];
/**创建模型并设置初始情况*/
受保护的void createModel()
{
模型=新整数[9][9];
//清除所有单元格
对于(int行=0;行<9;行++)
for(int col=0;col<9;col++)
模型[行][列]=0;
//创造最初的局面
模型[0][0]=9;
模型[0][4]=2;
模型[0][6]=7;
模型[0][7]=5;
模型[1][0]=6;
模型[1][4]=5;
模型[1][7]=4;
模型[2][1]=2;
模型[2][3]=4;
模型[2][7]=1;
模型[3][0]=2;
模型[3][2]=8;
模型[4][1]=7;
模型[4][3]=5;
模型[4][5]=9;
模型[4][7]=6;
模型[5][6]=4;
模型[5][8]=1;
模型[6][1]=1;
模型[6][5]=5;
模型[6][7]=8;
模型[7][1]=9;
模型[7][4]=7;
模型[7][8]=4;
模型[8][1]=8;
模型[8][2]=2;
模型[8][4]=4;
模型[8][8]=6;
}
/**创建一个空视图*/
受保护的void createView()
{
setLayout(新网格布局(9,9));
视图=新按钮[9][9];
//挫折背景(颜色:蓝色);
//某物。挫折背景(颜色。红色);
//创建一个空视图
对于(int行=0;行<9;行++)
for(int col=0;col<9;col++)
{
查看[行][列]=新建按钮();
添加(查看[行][列]);
}
}
/**从模型更新视图*/
受保护的void updateView()
{
对于(int行=0;行<9;行++)
for(int col=0;col<9;col++)
如果(型号[行][列]!=0)
{
查看[row][col].setLabel(String.valueOf(model[row][col]);
}
其他的
查看[行][col].setLabel(“”);
这就是我得到错误的地方:

   if(model[0][0].equals("9"))
   {
     //[row][col].setColor(Color.red);
    // view[row][col].setBackground(Color.red);
    model[row][col].setBackground(Color.blue);
   }
}

/** This method is called by the browser when the applet is loaded */
public void init()
{
createModel() ;
createView() ;
updateView() ;
}

/** Checks if num is an acceptable value for the given row */
protected boolean checkRow( int row, int num )
 {
for( int col = 0; col < 9; col++ )
  if( model[row][col] == num )
  return false ;

  return true ;
 }

 /** Checks if num is an acceptable value for the given column */
 protected boolean checkCol( int col, int num )
 {
 for( int row = 0; row < 9; row++ )
  if( model[row][col] == num )
  return false ;

 return true ;
}

/** Checks if num is an acceptable value for the box around row and col */
protected boolean checkBox( int row, int col, int num )
{
row = (row / 3) * 3 ;
col = (col / 3) * 3 ;

for( int r = 0; r < 3; r++ )
  for( int c = 0; c < 3; c++ )
  if( model[row+r][col+c] == num )
  return false ;

 return true ;
}

/** This method is called by the browser to start the applet */
public void start()
{
 // This statement will start the method 'run' to in a new thread
 (new Thread(this)).start() ;
 }

 /** The active part begins here */
 public void run()
 {
  try
  {
  // Let the observers see the initial position
  Thread.sleep( 100 ) ;

   // Start to solve the puzzle in the left upper corner
   solve( 0, 0 ) ;
   }
    catch( Exception e )
    {
    }
  }

  /** Recursive function to find a valid number for one single cell */
  public void solve( int row, int col ) throws Exception
 {
 // Throw an exception to stop the process if the puzzle is solved
 if( row > 8 )
  throw new Exception( "Solution found" ) ;

// If the cell is not empty, continue with the next cell
if( model[row][col] != 0 )
  next( row+1, col ) ;
else
{
  // Find a valid number for the empty cell
  for( int num = 1; num < 10; num++ )
  {
    if( checkRow(row,num) && checkCol(col,num) && checkBox(row,col,num) )
    {
      model[row][col] = num ;
      updateView() ;

      // Let the observer see it
      Thread.sleep( 100 ) ;

      // Delegate work on the next cell to a recursive call
      next( row+1, col) ;
    }
  }

  // No valid number was found, clean up and return to caller
  model[row][col] = 0 ;
  updateView() ;
}
}

/** Calls solve for the next cell */
public void next( int row, int col ) throws Exception
{
 if( col < 8 )
   solve( row+1, col ) ;
 else
   solve( row+1, 0 ) ;
 } 
}
if(模型[0][0]。等于(“9”))
{
//[行][col].setColor(Color.red);
//视图[行][列].背景(颜色.红色);
模型[行][列].背景(颜色.蓝色);
}
}
/**加载小程序时,浏览器将调用此方法*/
公共void init()
{
createModel();
createView();
updateView();
}
/**检查num是否为给定行的可接受值*/
受保护的布尔校验行(int行,int num)
{
for(int col=0;col<9;col++)
if(型号[行][列]==num)
返回false;
返回true;
}
/**检查num是否是给定列的可接受值*/
受保护的布尔校验列(int列,int num)
{
对于(int行=0;行<9;行++)
if(型号[行][列]==num)
返回false;
返回true;
}
/**检查num是否是行和列周围框的可接受值*/
受保护的布尔复选框(int行、int列、int num)
{
行=(行/3)*3;
col=(col/3)*3;
对于(int r=0;r<3;r++)
对于(int c=0;c<3;C++)
if(型号[行+r][列+c]==num)
返回false;
返回true;
}
/**浏览器调用此方法以启动小程序*/
公开作废开始()
{
//此语句将在新线程中启动方法“run”
(新线程(此)).start();
}
/**活动部分从这里开始*/
公开募捐
{
尝试
{
//让观察者看到初始位置
睡眠(100);
//开始解决左上角的难题
求解(0,0);
}
捕获(例外e)
{
}
}
/**用于为单个单元格查找有效数字的递归函数*/
公共void solve(int行,int列)引发异常
{
//如果谜题已解决,则引发异常以停止进程
如果(第8行)
抛出新异常(“找到解决方案”);
//如果单元格不为空,请继续下一个单元格
如果(型号[行][列]!=0)
下一个(行+1,列);
其他的
{
//查找空单元格的有效数字
对于(int num=1;num<10;num++)
{
if(检查行(行,数值)&&checkCol(列,数值)&&checkBox(行,列,数值))
{
模型[行][列]=num;
updateView();
//让观察者看看
睡眠(100);
//将下一个单元格上的工作委托给递归调用
下一个(行+1,列);
}
}
//找不到有效号码,请清理并返回给呼叫方
模型[行][列]=0;
updateView();
}
}
/**调用下一个单元格*/
public void next(int行,int列)引发异常
{
if(col<8)
求解(行+1,列);
其他的
求解(行+1,0);
} 
}
if(模型[0][0]。等于(“9”))

模型[0][0]是一个
int
,而不是
字符串,因此:


if(model[0][0]==9)

如果您能告诉我们您遇到了什么错误,那将对我们有很大帮助。在这样做之后,我发现了以下错误:发现了3个错误:File:/CodeForProgram/SimplifiedSudoku.java[行:100]错误:/CodeForProgram/SimplifiedSudoku.java:100:找不到符号符号:变量行位置:class SimplifiedSudoku文件:/CodeForProgram/SimplifiedSudoku.java[行:100]错误:/CodeForProgram/SimplifiedSudoku.java:100:找不到符号符号:变量列位置:类SimplifiedSudoku文件:/CodeForProgram/SimplifiedSudoku.java[line:100]错误:/CodeForProgram/SimplifiedSudoku.java:100:int无法取消引用
model[row][col]
您需要将
{…}
放在这两个循环中,以便
都在范围内。最后,模型是一个int[]。不能在int上调用setBackground()或任何类似的函数