If i do the code like this, it only works one time, i cant do other move and i know the error is all in the mousePressed function, but i can't solve it: The problem is specifically here:
public void mousePressed(MouseEvent me) {
if(foi==false) {
posxmouse = me.getX()/100;
posymouse = me.getY()/100;
foi = true;
}
if(foi==true) {
posx1mouse = me.getX()/100;
posy1mouse = me.getY()/100;
System.out.println("posx: " + me.getX()/100);
System.out.println("posy: " + me.getY()/100);
board = moves.MajorMovePawn(board, posymouse, posxmouse, posy1mouse, posx1mouse);
}
But this is the fully code, and i hope you guys have the solution for a so simple problem like this. I'm brazilian, the word "foi" means something like "done", and i used it to catch the two coordinates of the mouse and move one piece and after exclude the position where the piece was originally.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
/**
*
* @author Pedro Maxx
*/
public class Game extends JPanel implements MouseListener {
public Moves moves = new Moves();
public String[][] initialboard = {
{ "TB", "HB", "BB", "KB", "QB", "BB", "HB", "TB" },
{ "PB", "PB", "PB", "PB", "PB", "PB", "PB", "PB" },
{ "V", "V", "V", "V", "V", "V", "V", "V" },
{ "V", "V", "V", "V", "V", "V", "V", "V" },
{ "V", "V", "V", "V", "V", "V", "V", "V" },
{ "V", "V", "V", "V", "V", "V", "V", "V" },
{ "PW", "PW", "PW", "PW", "PW", "PW", "PW", "PW" },
{ "TW", "HW", "BW", "KW", "QW", "BW", "HW", "TW" },
};
public boolean foi, move = false;
public static boolean vezW = true, vezB = false, done;
public boolean obst = false;
public int posx, posy, posx1, posy1;
public String[][] board = new String[8][8];
public ImageIcon pawnB = new ImageIcon("ChessJava//PawnBlack.png"),
pawnW = new ImageIcon("ChessJava//PawnWhite.png"), towerB = new ImageIcon("ChessJava//TowerBlack.png"),
towerW = new ImageIcon("ChessJava//TowerWhite.png"), horseB = new ImageIcon("ChessJava//HorseBlack.png"),
horseW = new ImageIcon("ChessJava//HorseWhite.png"), bishopB = new ImageIcon("ChessJava//BishopBlack.png"),
bishopW = new ImageIcon("ChessJava//BishopWhite.png"), queenB = new ImageIcon("ChessJava//QueenBlack.png"),
queenW = new ImageIcon("ChessJava//QueenWhite.png"), kingB = new ImageIcon("ChessJava//KingBlack.png"),
kingw = new ImageIcon("ChessJava//KingWhite.png");
public Image imgPawnB = pawnB.getImage(), imgPawnW = pawnW.getImage(), imgTowerB = towerB.getImage(),
imgTowerW = towerW.getImage(), imgHorseB = horseB.getImage(), imgHorseW = horseW.getImage(),
imgBishopB = bishopB.getImage(), imgBishopW = bishopW.getImage(), imgQueenB = queenB.getImage(),
imgQueenW = queenW.getImage(), imgKingB = kingB.getImage(), imgKingW = kingw.getImage();
public int movesPawn2 = 2, movesPawn1 = 1, posxmouse = 0, posymouse = 0, posx1mouse = 0, posy1mouse = 0;;
public Game() {
setFocusable(true);
setPreferredSize(new Dimension(800, 800));
addMouseListener(this);
setInicio();
}
public void setInicio() {
for (int conti = 0; conti < 8; conti++) {
for (int contij = 0; contij < 8; contij++) {
board[conti][contij] = initialboard[conti][contij];
}
}
}
public void paintComponent(Graphics g) {
Graphics2D grafico = (Graphics2D) g;
super.paintComponent(g);
//Cria o tabuleiro:
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if ((i + j) % 2 == 0) {
g.setColor(Color.white);
} else {
g.setColor(Color.GREEN);
}
grafico.fillRect(j * 100, i * 100, 100, 100);
}
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j].equals("PB")) {
grafico.drawImage(imgPawnB, (j * 100), (i * 100 + 10), null);
}
if (board[i][j].equals("PW")) {
grafico.drawImage(imgPawnW, (j * 100), (i * 100 + 10), null);
}
if (board[i][j].equals("BB")) {
grafico.drawImage(imgBishopB, (j * 100), (i * 100 + 10), null);
}
if (board[i][j].equals("BW")) {
grafico.drawImage(imgBishopW, (j * 100), (i * 100 + 10), null);
}
if (board[i][j].equals("TB")) {
grafico.drawImage(imgTowerB, (j * 100), (i * 100 + 10), null);
}
if (board[i][j].equals("TW")) {
grafico.drawImage(imgTowerW, (j * 100), (i * 100 + 10), null);
}
if (board[i][j].equals("HB")) {
grafico.drawImage(imgHorseB, (j * 100), (i * 100 + 10), null);
}
if (board[i][j].equals("HW")) {
grafico.drawImage(imgHorseW, (j * 100), (i * 100 + 10), null);
}
if (board[i][j].equals("QB")) {
grafico.drawImage(imgQueenB, (j * 100), (i * 100 + 10), null);
}
if (board[i][j].equals("QW")) {
grafico.drawImage(imgQueenW, (j * 100), (i * 100 + 10), null);
}
if (board[i][j].equals("KB")) {
grafico.drawImage(imgKingB, (j * 100), (i * 100 + 10), null);
}
if (board[i][j].equals("KW")) {
grafico.drawImage(imgKingW, (j * 100), (i * 100 + 10), null);
}
}
}
repaint();
}
@Override
public void mousePressed(MouseEvent me) {
if (foi == false) {
posxmouse = me.getX() / 100;
posymouse = me.getY() / 100;
foi = true;
}
if (foi == true) {
posx1mouse = me.getX() / 100;
posy1mouse = me.getY() / 100;
System.out.println("posx: " + me.getX() / 100);
System.out.println("posy: " + me.getY() / 100);
board = moves.MajorMovePawn(board, posymouse, posxmouse, posy1mouse, posx1mouse);
}
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseClicked(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
public String testPiece(int posypiece, int posxpiece) {
String piece = "";
// Testar se é um peão branco
if (board[posypiece][posxpiece].equals("PW")) {
piece = "PW";
System.out.println("É um peão branco!");
}
// Testar se é um peão preto:
if (board[posypiece][posxpiece].equals("PB")) {
piece = "PB";
System.out.println("É um peão preto!");
}
return piece;
}
public boolean movePawnWhite(int posypawn, int posxpawn, int posypawn1, int posxpawn1) {
boolean pode = true;
if (testPiece(posypawn, posxpawn).equals("PW")) {
if (!board[posypawn - 1][posxpawn].equals("V")) {
pode = false;
}
if (!board[posypawn - 2][posxpawn].equals("V")) {
pode = false;
}
}
return pode;
}
}
Honestly, i think there's no issue with the Moves class:
public class Moves {
public int boardaux[][] = new int[8][8];
public String[][] MajorMovePawn(String board[][], int posy, int posx, int posy1, int posx1) {
String[][] boardFinal = board;
if(boardFinal[posy][posx].equals("PW")) {
System.out.println(board[posy][posx]);
if(board[posy1][posx1].equals("V")) {
boardFinal[posy1][posx1] = "PW";
boardFinal[posy][posx] = "V";
}
}
return boardFinal;
}
}