0

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

Lazer
  • 3
  • 1
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jun 05 '16 at 03:22
  • `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);` 1) Use the panel as an `ImageObserver`. I.E. `g.drawImage(board, 0,0,this);` 2) Don't load the images during a paint method! They should be read when the instance is being constructed. .. – Andrew Thompson Jun 05 '16 at 03:25
  • .. 3) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. 4) Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` – Andrew Thompson Jun 05 '16 at 03:26
  • 1
    5) `checkersGui.paintComponent(checkersGui.getGraphics());` should be `checkersGui.repaint();` 6) See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). (Similar GUI, radically different approach). – Andrew Thompson Jun 05 '16 at 03:27
  • Either you don't need the array set up as a grid or the pieces don't need to maintain an x/y coordinate, as you're trying to use both in different ways and they aren't working – MadProgrammer Jun 05 '16 at 03:46

1 Answers1

1

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.

resueman
  • 10,572
  • 10
  • 31
  • 45