java如何将对象从另一个类传递到paintComponent方法?
我的任务是制作游戏“洪水”如果你需要理解的话,你可以在这里玩这个游戏,但我认为这不是真的必要:http://unixpapa.com/floodit/
我已经完成了它的实际游戏部分,但现在我需要为它制作一个图形界面。我有三门课:
董事会。java,它使棋盘具有随机的int值,并包含其他几种使游戏正常运行的方法:
import java.awt.Color;
import java.util.Random;
/**The board class for the Flood-It game. This class implements a NxN board filled with numColors colors.
* The class implements several methods to allow the playing of the game.
*/
class Board {
//you will probably need to create some field variables
private int size;
private int numColors;
private int[][] board;
private int numOfMoves;
/**Constructs a new sizeXsize board filled where each element on the board is a random number between 0
* and numColors. Also initializes the number of moves to zero.
* @param size -- the size of the board
* @param numColors -- the number of possible entries on the board
*/
public Board(int size,int numColors) {
//TODO finish this constructor
this.size = size;
this.numColors = numColors;
numOfMoves = 0;
board = new int[size][size];
Random rand = new Random();
int randomNum = 0;
for (int count = 0; count < size; count++) {
for (int counter = 0; counter < size; counter++) {
randomNum = rand.nextInt(this.numColors);
board[count][counter] = randomNum;
}
}
}
/**Updates the board to fill (from the top left corner) with a specified color.
* Filling stops when any other color is hit besides the one in the top left corner.
* Play the game at http://www.lemoda.net/javascript/flood-it/ or http://unixpapa.com/floodit/?sz=14&nc=4
* to get a better understanding of what this method should do.
* You will probably also want to take a look at the algorithm described
* at http://en.wikipedia.org/wiki/Flood_fill which describes what this method should do.
* I recommend the Stack-based recursive implementation. It is a recursive algorithm for
* flooding the board. It is one of the easier ones to implement.
* You are free to have this method call other methods. I would recommend creating a private method that
* this method calls and have that private method be the recursive method.
* A recursive method is one that calls itself.
* @param color -- the new color to flood the board with.
*/
public void move(int replacementColor) {
int targetColor = board[0][0];
recursiveMove(0,0,targetColor,replacementColor);
numOfMoves++;
}
private void recursiveMove(int xCoord, int yCoord, int targetColor, int replacementColor) {
if (targetColor == replacementColor) {
return;
}
if (board[xCoord][yCoord] != targetColor) {
return;
}
board[xCoord][yCoord] = replacementColor;
if (yCoord != size-1) {
recursiveMove(xCoord,yCoord+1,targetColor,replacementColor);
}
if (yCoord != 0) {
recursiveMove(xCoord,yCoord-1,targetColor,replacementColor);
}
if (xCoord != 0) {
recursiveMove(xCoord-1,yCoord,targetColor,replacementColor);
}
if (xCoord != size-1) {
recursiveMove(xCoord+1,yCoord,targetColor,replacementColor);
}
}
/**returns true if the board is not completely filled with a single color.
* Otherwise it returns false.
* @return true if board is all one color
*/
public boolean finished() {
//TODO finish this method
for (int count = 0; count < size; count++) {
for (int counter = 0; counter < size; counter++) {
if (board[count][counter] != board[0][0]) {
return false;
}
}
}
return true;
}
/**returns how many times the move() method has been called.
* @return the number of times the move() method has been called.
*/
public int numMoves() {
//TODO finish this method
return numOfMoves;
}
/**Returns a string representation of the board. Use tabs between elements of the board.
* And have every row of the board be separated by a newline character.
* Example:
* "1\t0\t3\t\n2\t0\t2\t\n1\t0\t1\t\n"
* @return a String representation of the board
*/
public String toString() {
//TODO finish this method
String boardString = "";
for (int count = 0; count < board.length; count++) {
for (int counter = 0; counter < board.length; counter++) {
boardString += board[count][counter];
boardString += "\t";
}
boardString += "\n";
}
return boardString;
}
}
淹没它。java,其中包含用于加载图形界面的JFrame行,以及用于实际运行游戏的代码(由于我被卡住,它还没有完全完成):
import java.util.Scanner;
import javax.swing.JFrame;
/**This class is the main method for the Flood-It game as found on many web sites
* ( such as http://www.lemoda.net/javascript/flood-it/ or
http://unixpapa.com/floodit/?sz=14&nc=4 ).
* It prompts the user for the size of the board
* and the number of colors. The user is prompted for the next color until the board is flooded.
* After the game is over it prints how many turns the user took and then asks if they want to play again.
*/
class FloodIt {
private static final int FRAMESIZE = 1000;
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setSize(FRAMESIZE,FRAMESIZE);
frame.setTitle("Brennan's Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicalBoard component = new GraphicalBoard();
frame.add(component);
frame.setVisible(true);
String again="";
int size = 20;
int numColors = 7;
do {
Board board=new Board(size,numColors);
while(!board.finished()) {
//I will change the print statements below into graphical input boxes later
System.out.print("****************\n"+board+"\n****************\n");
System.out.print("What color do you choose? ");
int color=Integer.parseInt(scan.nextLine());
board.move(color);
}
System.out.println("Nice job, you finished in "+board.numMoves());
System.out.print("Would you like to play again (Y/N)? ");
again=scan.nextLine();
} while (again.equalsIgnoreCase("Y"));
scan.close();
}
}
和图形板。java,它应该从板上获取2d数组的值。java,并在图形界面中显示电路板。2d数组中的每个数字都对应于颜色数组中的一种颜色:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
public class GraphicalBoard extends JComponent {
private int xSize = 50;
private int ySize = 50;
public void paintComponent(Graphics g, int size, Board board) {
String colors[] = {"BLUE","GREEN","YELLOW","RED","BLACK","ORANGE","PINK"};
Graphics2D g2 = (Graphics2D) g;
int xCoord = 0;
int yCoord = 0;
int colorNum = 0;
String colorOfSquare = "";
for (int count = 0; count < size; count++) {
for (int counter = 0; counter < size; counter++) {
colorNum = board[count][counter];
colorOfSquare = colors[colorNum];
g2.setColor(Color.colorOfSquare);
Rectangle square = new Rectangle(xCoord,yCoord,xSize,ySize);
xCoord += 50;
}
yCoord += 50;
}
}
}
两个问题:
- 在图形板中。java,在“colorNum=board[count][counter];”行上,我得到了一个错误:“表达式的类型必须是数组类型,但它已解析为Board。”李>
我似乎遇到了一个问题,将已经初始化的电路板从电路板中取出。将java类转换为GraphicalBoard。java类
- 在图形板中。java,在“g2.setColor(Color.colorOfSquare);”行上,我得到一个错误:“colorOfSquare无法解析,或者它不是一个字段。”李>
我知道这个问题,应该是“g2.setColor(Color.BLACK);”,但是我要让用户输入颜色,所以它需要是一个变量,我希望有一些更干净的东西,而不仅仅是每个颜色的if语句
有什么建议吗?谢谢
# 1 楼答案
两个答案:
paintComponent
仅接收图形对象。请参阅此link获取简短教程。如果需要访问此方法中的其他对象,请将其设置为变量GraphicalBoard
,并在构造期间将其传递给om李>1.5您需要访问董事会的董事会,因为这是您正在使用的。所以在类
Board
中添加一个getBoard(int i, int j)
。类似于以下内容(我还添加了一个getSize()
方法):colorOfSquare
已定义为颜色。出现错误的原因是Color
类没有这样的常量。你应该直接传递颜色李>试试这个:
# 2 楼答案
您的
Board
类包含一个成员变量int[][] board
,但其作用域是私有的。当您调用以下命令时:colorNum=电路板[计数][计数器]
这是错误的,因为这里的
board
变量是Board
类的对象。它本身不是两天数组,但它将int[][] board
封装在其中。因此,您需要在Board
中提供一个getter方法来公开其board
成员变量,如下所示:然后在
paintComponent
方法中,您可以通过以下方式访问它:board.getBoard()[count][counter]
在
colorOfSquare
变量中似乎已经有用户输入的颜色。但是Graphics2D
的setColor
方法只接受java类型的变量。awt。颜色因为有颜色的字符串表示,所以可以得到相应的java。awt。使用上述反射的颜色值here。以下几点应该对你有用:# 3 楼答案
一般来说
代码中的其他问题:
例如,在下面的代码中,我使用了一个模型类,这个类包含int[][]board变量,这里称为BoardModel,我给它一个SwingPropertyChangeSupport对象
此对象将接受侦听器,并允许我通知侦听器模型的更改
然后,当模型更改时,我通过调用支持对象的
firePropertyChange(...)
方法通知所有侦听器:然后在控件中,我可以添加通知视图更改的侦听器:
一个可行的例子可以如下所示: