現在、「解決」ボタン、テキスト フィールド、および JLabel を含むフレームが表示されています。テキスト フィールドに数値を入力して「解決」を押すと、NQueens モデルを調べて、解決策がいくつあるかを計算し、グリッド レイアウトを使用して画面に 1 つの解決策を表示することになっています。しかし、解決をクリックしても、実際には何も起こりません。ビューの repaint() および revalidate() メソッドを呼び出してみましたが、機能していないようです。
Solve ボタンはリフレクションを使用してコントローラーに移動し、何をすべきかを確認します。誰が何が起こっているのか知っていますか?それはMVCスタイルです
コントローラ クラス:
package model;
public class Controller {
private View myView;
private NQueensModel myModel;
private int int1, possibilities;
public Controller()
{
myView = new View(this);
}
public void solve()
{
int1 = myView.getEntryInt();
myView.doViewGrid();
myModel = new NQueensModel(int1);
myModel.solvePuzzle();
possibilities = myModel.getPossibilities();
myView.addButtons();
myView.setPossibilitiesLabel(possibilities);
myView.revalidate();
myView.repaint();
}
public boolean[][] getMyBoard()
{
return myModel.getBoard();
}
/*public static void main(String[] args)
{
Controller controller = new Controller();
}*/
}
クラスを見る
package model;
import java.awt.*;
import javax.swing.*;
import model.ButtonListener;
import model.Controller;
import java.lang.reflect.Method;
@SuppressWarnings("serial")
public class View extends JFrame {
private static final int FRAME_WIDTH = 600;
private static final int FRAME_HEIGHT = 600;
private static final int FRAME_X_ORIGIN = 250;
private static final int FRAME_Y_ORIGIN = 250;
private JButton solveButton;
JPanel gridPanel;
private JTextField text;
private String entryNum;
private int entryInt;
private ButtonListener mySolveListener;
private boolean[][] myBoard;
private JButton buttons, emptyButtons;
private JLabel possibilitiesLabel;
public static void main(String[] args){
}
private static Controller myController;
public View(Controller controller)
{
JPanel gridPanel;
gridPanel = new JPanel();
gridPanel.setBounds(100,100,400,400);
gridPanel.setLayout(new GridLayout(4,4));
this.add(gridPanel);
this.setTitle("NQueens");
myController = controller;
this.setLayout(null);
this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
// this.associateListeners();
Container contentPane;
// this.add(contentPane);
this.setTitle("NQueens");
// this.addWindowListener(new AWindowListener());
this.setResizable(false);
this.setBackground(Color.WHITE);
solveButton = new JButton("Solve");
solveButton.setBounds(500,500,80,80);
this.add(solveButton);
text = new JTextField();
text.setBounds(400,500,80,80);
this.add(text);
possibilitiesLabel = new JLabel("Possibilities: ");
possibilitiesLabel.setBounds(240,500,80,80);
this.add(possibilitiesLabel);
this.setVisible(true);
}
public void setPossibilitiesLabel(int x)
{
possibilitiesLabel.setText("Possiblities: " + x);
}
public void doInt(){
entryNum = text.getText();
entryInt = Integer.parseInt(entryNum);
}
public int getEntryInt(){
return entryInt;
}
public void addButtons()
{
myBoard = myController.getMyBoard();
for (int i = 0; i < this.getEntryInt(); i++)
{
for(int j = 0 ; j < this.getEntryInt(); j++)
{
if(myBoard[i][j]==true)
{
buttons = new JButton("Q");
gridPanel.add(buttons);
this.setVisible(true);
}
else
{
emptyButtons = new JButton(" ");
gridPanel.add(emptyButtons);
this.setVisible(true);
}
}
}
}
public void doViewGrid(){
gridPanel.setLayout(new GridLayout(this.getEntryInt(),this.getEntryInt()));
}
/**
* Associates each component's listener with the controller
* and the correct method to invoke when triggered.
*
* <pre>
* pre: the controller class has be instantiated
* post: all listeners have been associated to the controller
* and the method it must invoke
* </pre>
*/
public void associateListeners()
{
String error;
Class<? extends Controller> controllerClass;
Method solveMethod,
decrementMethod;
Class<?>[] classArgs;
Integer[] args;
controllerClass = myController.getClass();
error = null;
solveMethod = null;
decrementMethod = null;
classArgs = new Class[1];
args = new Integer[1];
// Set argument types for method invokations
try
{
classArgs[0] = Class.forName("java.lang.Integer");
}
catch(ClassNotFoundException exception)
{
error = exception.toString();
System.out.println(error);
}
// Associate method names with actual methods to be invoked
try
{
solveMethod = controllerClass.getMethod("solve",classArgs);
decrementMethod = controllerClass.getMethod("decrement",classArgs);
}
catch(NoSuchMethodException exception)
{
error = exception.toString();
System.out.println(error);
}
catch(SecurityException exception)
{
error = exception.toString();
System.out.println(error);
}
// Set up listeners with actual argument values passed in
// to methods
args[0] = new Integer(200);
mySolveListener
= new ButtonListener(myController, solveMethod, args);
// myDecrementListener
// = new ButtonListener(myController, decrementMethod, args);
}
}
モデル
package model;
public class NQueensModel
{
private int myNumsQueen;
public int myPossibilities=0;
private boolean[][] myBoard;
// private static NQueensModel myModel = new NQueensModel(5);
public static void main (String[] args) {
// System.out.println(myModel.solvePuzzle());
// System.out.println(myModel.myPossibilities);
// System.out.println(myModel.doIt(myModel.myPossibilities)); //you want this
// System.out.println(myPossibilities);
}
public NQueensModel(int nQueens)
{
myNumsQueen = nQueens;
myPossibilities=0;
myBoard = new boolean[myNumsQueen][myNumsQueen];
}
public boolean solvePuzzle()
{
// return this.doIt(myModel.myPossibilities);
return solvePuzzle(0);
}
private boolean solvePuzzle(int ncolumn)
{
if(ncolumn>myNumsQueen-1)
{
myPossibilities++;
// return true;
}
int i;
for( i =0; i<myNumsQueen;i++)
{
if(this.isSafeMove(i, ncolumn)==true)
{
this.placeQueen(i,ncolumn);
if(this.solvePuzzle(ncolumn+1)==true)
{
return true;
}
this.removeQueen(i, ncolumn);
}
}
// else if(myPossibilities>0)
// {
// return true;
// }
return false;
}
private boolean doIt(int county)
{
if(county>0)
{
return true;
}
else
{
return false;
}
}
// private int solveCount(int count, int col)
// {
// for(int j = 0 ; j<col; j++){
// if(myModel.solvePuzzle(j)==true){
// count++;
// }
// else if(myModel.solvePuzzle(j)==false){
// return count;
// }
// else
// {
// for(int i =0 ; i<col;i++){
// count = solveCount(count, col+1);
// }
// return count;
// }
// return count;
// }
// return count;
//
// }
private boolean isSafeMove(int row, int col)
{
if(row <0 || row>=myNumsQueen || col<0 || col>=myNumsQueen)
{
return false;
}
else if(this.checkLowerDiag(row, col)==true ||this.checkUpperDiag(row, col)==true ||this.checkLeft(row,col)==true)
{
return false;
}
else
{
return true;
}
}
private boolean checkUpperDiag(int row, int col)
{
if(row==0)
{
return false;
}
else
{
for(int i=row, j = col; i>=0 && j>=0; i--, j--)
{
if(myBoard[i][j]==true)
{
return true;
}
}
return false;
}
}
private boolean checkLowerDiag(int row, int col)
{
if(col==0 )
{
return false;
}
if(row==myNumsQueen-1){
return false;
}
else
{
for(int i = row, j = col; i<myNumsQueen && j>=0; i++, j--)
{
// System.out.println("error" + i + " " + j );
if(j>=myNumsQueen)
{
return false;
}
else if(myBoard[i][j]==true)
{
return true;
}
}
return false;
}
}
private boolean checkLeft(int row, int col)
{
if(col==0)
{
return false;
}
else
{
for(int i = col; i>=0; i--)
{
if(i>=myNumsQueen)
{
return false;
}
else if(myBoard[row][i]==true)
{
return true;
}
}
return false;
}
}
private boolean placeQueen(int row, int col)
{
if(col>=myNumsQueen)
{
return false;
}
myBoard[row][col] = true;
return true;
}
private boolean removeQueen(int row, int col)
{
myBoard[row][col] = false;
return false;
}
public int getPossibilities(){
return this.myPossibilities;
}
public boolean[][] getBoard()
{
return myBoard;
}
// public String toString()
// {
//
// }
}