드라이버 클래스 사용하여 바둑판 :배열과 객체 지향 프로그래밍
//Objective: make a 10X10 Grid appear
//it should be a red and white or black and white
//checkerboard.
public Checkers()
{
con.setLayout(new FlowLayout());
for (int xDimension = 0; xDimension<theGrid.length; xDimension++)
{
for (int yDimension = 0; yDimension< theGrid[xDimension].length; yDimension++)
{
theGrid[xDimension][yDimension] = new Block(xDimension,yDimension);
}
}
//pass in the row and column numbers to the constructor
//these will define where the Blocks appear.
t.start();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics gr)
{
Image i=createImage(getSize().width, getSize().height);
Graphics2D g2 = (Graphics2D)i.getGraphics();
for (int x = 0; x < theGrid.length; x++)
{
for (int y = 0; y < theGrid[x].length; y++)
{
theGrid[x][y].draw(g2);
}
}
g2.dispose();
gr.drawImage(i, 0, 0, this);
}
}
블록 클래스 :
public class Block
{
private int x, y, width, height, rowNum, colNum;
private Color color;
public Block(int xDimension, int yDimension)
{
Random ra = new Random();
xDimension = rowNum;
yDimension = colNum;
//finish
public void draw(Graphics2D gt)
{
// put your code here
if (rowNum % 2 == 0)
{
if (colNum % 2 == 0)
{
gt.fillRect(x, y, 20, 20);
gt.setColor(Color.RED);
}
else if (colNum % 2 == 1)
{
gt.fillRect(x, y, 20, 20);
gt.setColor(Color.WHITE);
}
}
else if (rowNum % 2 == 1)
{
if (colNum % 2 == 0)
{
gt.fillRect(x, y, 20, 20);
gt.setColor(Color.WHITE);
}
else if (colNum % 2 == 1)
{
gt.fillRect(x, y, 20, 20);
gt.setColor(Color.RED);
}
}
//Red/Black/white color.
gt.drawString("R:"+rowNum+", C:"+colNum, x+3, y +3);
}
public void getLocation()
{
}
}
프로그래밍을 위해, 우리는 코드 선생님이 만든을 사용하여 바둑판을 만들고 있어요을하지만 우리는 기입해야 나머지는 //를 통해 지침을 남겼습니다. 이 assigment의 요지는 바둑판을 프로그램하는 것입니다 (색상이 번갈아 가며 다른 모든 블록은 빨간색입니다). 블록 내에서 어떤 행과 열이 있는지 알 수 있습니다. 단순히 프로그램을 가져 오는 데 어려움이 있습니다. 나타나서 내 문제를 정확하게 지적 할 수 없다. 미리 감사드립니다!
나는 내 코드를 망치고 있었고 나는 그것을 간과했다고 생각한다. 그 점을 지적 해 주셔서 감사합니다! – user3528001