[英]Drawing a two colored checker board with nested for loops where each square being their own object (Java)
I am attempting to draw a checkerboard pattern in java using nested for loops, but I am having trouble doing it with two different colors. 我正在尝试使用嵌套的for循环在Java中绘制棋盘图案,但是使用两种不同的颜色却很难。 I know this question has been asked before, but it hasn't been asked with two different colors on the board that are not just using a background color. 我知道这个问题以前曾被问过,但并没有用板上两种不仅仅使用背景色的颜色来问过。 I plan on using the individual squares as an array to hold checker positions, so I do need each individual square made. 我计划使用单个正方形作为数组来保存检查位置,因此我确实需要制作每个单独的正方形。 Would it be better to drop the ice of a nested for loop to create each square, or should i stick with that shortcut? 丢弃嵌套的for循环来创建每个正方形会更好吗,还是我应该坚持使用这种快捷方式? And if I were to stick with it, how would the nested loop be formatted (one for each color)? 如果我坚持使用它,嵌套循环将如何格式化(每种颜色一个)?
When creating checker tiles, I would pass in an int
for the x coordinate, and y coordinate such as: 创建棋盘格时,我将为x坐标和y坐标传递一个int
,例如:
import java.awt.Color;
import java.awt.Graphics;
public class CheckerTile {
public static final int WIDTH = 100; //width of each tile
public static final int HEIGHT = 100; //height of each tile, most likely same as width so its a square
public static int currentId = 0; //variable to reference unique id for each tile
private int id; //current id of tile
private int x; //x coordinate
private int y; //y coordinate
private int width; //width of tile
private int height; //height of tile
//Default constructor to take x and y coordinate
public CheckerTile( int x, int y ) {
this.id = currentId++;
this.x = x;
this.y = y;
width = WIDTH;
height = HEIGHT;
}
public int getId()
{
return id;
}
//draws the tile on the panel.
public void draw(Graphics g)
{
//if the checkerTile's id is divisible by 2, draw it red, otherwise draw it black.
g.setColor( id % 2 == 0 ? Color.RED : Color.black);
g.fillRect(x, y, width, height);
}
}
That way we have a way to draw the tile on the board. 这样,我们就可以在板上绘制瓷砖。 Now, when creating each object, we increment a currentId
variable so that we can color each one individually using the modulus operator later. 现在,在创建每个对象时,我们增加一个currentId
变量,以便稍后可以使用模数运算符分别为每个对象着色。
I am assuming you are using Swing so I decided to add a draw(Graphics g)
method so when repainting in java it would use that Graphics
object. 我假设您正在使用Swing,所以我决定添加一个draw(Graphics g)
方法,以便在Java中进行重新绘制时将使用该Graphics
对象。 If you are using a different library, then you will have to do some research in to how to draw it on the board. 如果您使用的是其他库,则必须进行一些研究以将其绘制在板上。
Now in your JPanel
, it would look something like this: 现在在您的JPanel
,它看起来像这样:
//Creates the JPanel, which needs to be added to JFrame object in main
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CheckerBoard extends JPanel {
CheckerTile[][] checkerTiles; //2-dimension array of checkerTiles
public CheckerBoard() {
super();
this.setSize(800,800);
checkerTiles = new CheckerTile[9][9];
//This creates the checkerTiles.
for(int i = 0; i < 9; i++)
{
for( int j = 0; j < 9; j++)
{
checkerTiles[i][j] = new CheckerTile( j * CheckerTile.WIDTH, i * CheckerTile.HEIGHT );
}
}
this.setVisible(true);
//Repaint right away to show results.
repaint();
}
//We need to override this to paint the tiles on the board.
@Override
public void paintComponent(Graphics g)
{
for(int i = 0; i < checkerTiles.length; i++)
{
for(int j = 0; j < checkerTiles[i].length; j++)
{
//call the draw method on each tile.
checkerTiles[i][j].draw(g);
}
}
}
//A demo of adding the panel to a frame and showing the tiles.
public static void main(String[] args)
{
//Create the JFrame and add the CheckerBoard we made to it.
JFrame frame = new JFrame();
frame.setSize(800,800);
frame.setLayout(new BorderLayout());
frame.add(new CheckerBoard(), BorderLayout.CENTER);
frame.setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.