Reputation: 3
Im currently in the making of a java based checkers game I have plenty of debug code that proves that the array storing each piece is updating but for some reason the graphics dont change. My main class:
public class CheckersGui extends JPanel{
//private List<Piece> Pieces = new ArrayList<Piece>();
public Piece[][] pieces = new Piece[8][8];
public CheckersGui(){
pieces[0][1] = new Piece(0,100,0);
pieces[1][0] = new Piece(100,0,0);
pieces[2][1] = new Piece(200,100,0);
pieces[3][0] = new Piece(300,0,0);
pieces[4][1] = new Piece(400,100,0);
pieces[5][0] = new Piece(500,0,0);
pieces[6][1] = new Piece(600,100,0);
pieces[7][0] = new Piece(700,0,0);
pieces[0][7] = new Piece(0,700,1);
pieces[1][6] = new Piece(100,600,1);
pieces[2][7] = new Piece(200,700,1);
pieces[3][6] = new Piece(300,600,1);
pieces[4][7] = new Piece(400,700,1);
pieces[5][6] = new Piece(500,600,1);
pieces[6][7] = new Piece(600,700,1);
pieces[7][6] = new Piece(700,600,1);
// create application frame and set visible
//
JFrame f = new JFrame();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.setResizable(true);
f.setSize(820, 850);
// add mouse listeners to enable drag and drop
//
MyMouseListener listener = new MyMouseListener(this.pieces,
this);
this.addMouseListener(listener);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.clearRect(0, 0, 800, 800);
System.out.println("PAINT COMPONENT");
// for (Piece p[] : pieces) {
// for (Piece s : p) {
// System.out.print(s);
// }
// System.out.println();
// }
BufferedImage board = null, crown=null;
try { board = ImageIO.read(new File("src/bobreedhere/Checkers/assets/board3.png"));
crown = ImageIO.read(new File("src/bobreedhere/Checkers/assets/crown.png"));}
catch (IOException e){}
g.drawImage(board, 0,0,null);
for (Piece p[] : pieces) {
for (Piece s : p) {
System.out.print(s);
if(s!=null){
if (s.getColor() == 0)
g.setColor(Color.BLACK);
else
g.setColor(Color.WHITE);
g.fillOval(s.getX(), s.getY(), 100, 100);
if (s.isKinged())
g.drawImage(crown, s.getX(), s.getY(), null);
}
}
System.out.println();
}
}
public static void main(String args[]){
new CheckersGui();
}
}
and heres my mouse events class. I honestly have no idea whats wrong with this so i figure the more info the better
public class MyMouseListener implements MouseListener{
Piece[][] pieces;
private CheckersGui checkersGui;
private boolean pieceSelected;
private Piece selectedPiece;
public MyMouseListener(Piece[][] pieces, CheckersGui chessGui) {
super();
this.pieces = pieces;
this.checkersGui = chessGui;
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("CLICK");
System.out.println(pieces[e.getPoint().x/100][e.getPoint().y/100]);
//System.out.println(e.getPoint().x/100+" : "+e.getPoint().y/100);
if(!pieceSelected){
//System.out.println("NADA");
if(pieces[e.getPoint().x/100][e.getPoint().y/100]!=null){
selectedPiece = pieces[e.getPoint().x/100][e.getPoint().y/100];
pieceSelected = true;
}
}else{
//System.out.println("EYO");
if(pieces[e.getPoint().x/100][e.getPoint().y/100]==null){
pieces[e.getPoint().x/100][e.getPoint().y/100]=selectedPiece;
pieces[selectedPiece.getX()/100][selectedPiece.getY()/100]=null;
pieceSelected=false;
}
}
System.out.println(checkersGui.pieces[e.getPoint().x/100][e.getPoint().y/100]);
checkersGui.repaint();
checkersGui.paintComponent(checkersGui.getGraphics());
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
I just dont understand why the pieces array is changing and the paintComponent method is running, but the pieces dont move at all.
example the board before and after I move a piece
Upvotes: 0
Views: 131
Reputation: 10613
Your problem is because when you paint the pieces, you use the x
and y
values stored inside them. However, you never update those when you move the piece; you just put it in the new spot in the array while it still contains its old location.
To fix this, you should either update those values when you move it or (the less likely to break way), figure out the correct position to paint in from the location in the array, not from a value stored in the piece.
Upvotes: 1