Reputation: 785
I'm trying to make an initial state for my game board (chess) in Java. However when i run the program the board seems to be flipped? If you look at the initial state method, this is how i want my initial board to start:
public void initialBoardState(){
gameBoard = new int [][]
{{22,23,24,25,26,24,23,22},
{21,21,21,21,21,21,21,21}, //Black pieces
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{11,11,11,11,11,11,11,11}, //White pieces
{12,13,14,15,16,14,13,12}};
}
However when i run it the board seems to be flipped like so:
gameBoard= new int[][]
{{22,21,0,0,0,0,11,12},
{23,21,0,0,0,11,13}
And so on.. Can anyone explain this? The entire class is posted here below:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
/**
* @author Cian
* This class will draw our game board
*/
class Board extends JComponent implements MouseListener{
private int gameBoard[][] = new int[8][8];
public int row = 8;
public int column = 8;
public int cell = 80;
public int rowSelected;
public int colSelected;
public int currentPlayer = 1;
int x; int y; int a; int b;
public Board(){
addMouseListener(this);
initialBoardState();
}
public void paintComponent(Graphics g){
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, 640, 640);
drawBoard(g2d);
}
public void drawBoard(Graphics2D g2d){
g2d.setColor(Color.black);
// draw the edge lines (0,0,0,640), (0,0,640,0), (0,640,640,640), (640,0,640,640)
g2d.drawLine(0, 0, 0, 639);
g2d.drawLine(0, 0, 639, 0);
g2d.drawLine(0, 639, 639, 639);
g2d.drawLine(639, 0, 639, 639);
// draw the horizontal lines using a loop from one to 7, coordiates of each line is (0, x*80, 640, x*80) also
// draw vertical lines with coordinates of (x*80, 0, x*80, 640)
for(int i = 1; i < 8; i++) {
g2d.drawLine(0, i*80, 640, i*80);
g2d.drawLine(i*80, 0, i*80, 640);
}
for ( int row = 0; row < 8; ++row ) // ++ means "increment by one"
// This inner loop counts over all the (white or black) squares in a row. It starts at 0 if row is even, or 1 if row is odd, and goes up to no more than 7 (so it will count 1-3-5-7 or 0-2-4-6
for ( int i = row % 2; i <= 7; i+= 2 ) { // += means "add to that variable
g2d.setColor(Color.BLACK);
g2d.fillRect ( getX() + cell*i, getY() + row*cell, cell, cell);
}
BufferedImage img_black = null;
BufferedImage castle_black = null;
BufferedImage castle_white = null;
BufferedImage img_white = null;
try {
img_black = ImageIO.read(new File("imgs/pawn-black.png"));
castle_black = ImageIO.read(new File("imgs/castle-black.png"));
castle_white = ImageIO.read(new File("imgs/castle-white.png"));
img_white = ImageIO.read(new File("imgs/pawn-white.png"));
} catch (IOException e) {
}
for(int a=0;a<8;a++){
for(int b=0;b<8;b++){
if(gameBoard[a][b] == 22){
g2d.drawImage(castle_black,a*80, b*80,null);
}
if(gameBoard[a][b] == 12){
g2d.drawImage(castle_white,a*80, b*80,null);
}
if(gameBoard[a][b] == 0){
g2d.drawImage(castle_white,a*80, b*80,null);
}
}
}
/*for(int i=0;i<8;i++){
for(int j=1;j<2;j++){
g2d.drawImage(img_black,i*80, j*80,null);
}
}
for(int i=0;i<8;i++){
for(int j=6;j<7;j++){
g2d.drawImage(img_white,i*80, j*80,null);
}
}*/
}
@Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent event) {
x = event.getX(); y = event.getY();
rowSelected = x / cell;
colSelected = y / cell;
if (rowSelected >= 0 && rowSelected < row && colSelected >= 0 && colSelected < column){
System.out.println("X: "+rowSelected+" | Y: "+colSelected+" | GameBoard: "+gameBoard[rowSelected][colSelected]);
}
}
@Override
public void mouseReleased(MouseEvent event) {
a = event.getX(); //a = mouse released x coordinates
b = event.getY(); //b = mouse released y coordinates
//isValidMove(x,y,a,b);
}
/*private void isValidMove(int x, int y, int a, int b){
if(gameBoard[x][y]/10 == currentPlayer){
int piece = gameBoard[x][y]%10;
if(piece == 1){
System.out.println("1");
}
}
}*/
public void initialBoardState(){
gameBoard = new int [][]
{{22,23,24,25,26,24,23,22},
{21,21,21,21,21,21,21,21}, //Black pieces
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{11,11,11,11,11,11,11,11}, //White pieces
{12,13,14,15,16,14,13,12}};
}
}
Upvotes: 0
Views: 445
Reputation: 7037
From how you describe your problem, it seems that you're just entering the x coordinate where you should enter the y coordinate and vice versa. So this is just a wild guess, but try changing similar lines:
g2d.drawImage(castle_black,a*80, b*80,null);
To this:
g2d.drawImage(castle_black,b*80, a*80,null);
Also, according to this, the method is declared as public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
, so to me it seems you're not using it well and it should actually be something like this:
g2d.drawImage(castle_black, null, b*80, a*80);
But I might be wrong as I have no experience with it.
Upvotes: 1