Question

I'm making a chess game, and I'm using a chessboard that I made with paint at 480x480, and got the pieces from some sprite and made each one of them 60x60 and transparent background. I managed to put the chessboard and the pieces on screen, but the positions are messy. I'm using null layout. I did:

chessBoardLabel.setBounds(0, 0+25, 480, 480);

the +25 is because of the Frame's thing that is on top and looks like it is considered in positioning. as for the piece, for example:

for (int i = 0; i <= 7; i++)
 whitePawnArray[i] = new whitePawnPiece(i*60,420+25); 

the parameters set the xPos and yPos. For the bounds function, I did:

whitePawnLabel.setBounds(this.xPos, this.yPos, this.xPos+60, this.yPos+60);

But this happens: http://i.imgur.com/MF9Njbi.jpg

If I do that:

for (int i = 0; i <= 7; i++)
whitePawnArray[i] = new whitePawnPiece(i*40,280+15);

this happens: http://i.imgur.com/Pm1fMSp.jpg

First: what happened to the positioning? Why doesn't it follow what I intended it to be? Second: what is the 8th piece doing in the middle of nowhere?

package chess.game;
import java.util.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class chessMain extends JFrame{
    public static void main (String arguments[]){

    //Create White
    whitePiece white_piece = new whitePiece();

    whitePawnPiece[] whitePawnArray = new whitePawnPiece[8];
    for (int i = 0; i <= 7; i++) whitePawnArray[i] = new whitePawnPiece(i*40,280+15);

    /*whiteTowerPiece[] whiteTowerArray = new whiteTowerPiece[2];
    whiteTowerArray[0] = new whiteTowerPiece(0,420);
    whiteTowerArray[1] = new whiteTowerPiece(420,420);

    whiteHorsePiece[] whiteHorseArray = new whiteHorsePiece[2];
    whiteBishopPiece[] whiteBishopArray = new whiteBishopPiece[2];
    whiteKingPiece whiteKing = new whiteKingPiece();
    whiteQueenPiece whiteQueen = new whiteQueenPiece();*/

    //Create Black

    JFrame frame = new JFrame();
    JPanel panel;

    //Initialize
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Chess");
    frame.setSize (640,640);
    frame.setResizable(false);
    frame.setLayout(null);
    panel = new JPanel();
    panel.setLayout(null);
    frame.getContentPane().add(panel);

    //draw chessBoard
    ImageIcon chessBoardIcon = new           ImageIcon(frame.getClass().getResource("/chess/art/Chess_Art/chessBoard.png"));
    JLabel chessBoardLabel = new JLabel(chessBoardIcon);
    panel.add(chessBoardLabel);
    chessBoardLabel.setBounds(0, 0+25, 480, 480);
    frame.setComponentZOrder(chessBoardLabel, 1);
    frame.setComponentZOrder(panel, 2);
    //draw Pawn
    for (int i = 0; i<=7; i++){
        panel.add(whitePawnArray[i].whitePawnLabel);
        whitePawnArray[i].draw();
        frame.setComponentZOrder(whitePawnArray[i].whitePawnLabel, 0);
    }

    frame.setVisible(true);

}
}

public class whitePawnPiece extends whitePiece{
    JLabel whitePawnLabel;
    ImageIcon whitePawnIcon;
    public whitePawnPiece(int x, int y){
        whitePawnIcon = new ImageIcon(getClass().getResource("/chess/art/Chess_Art/white/whitePawnPiece.png"));
        whitePawnLabel = new JLabel (whitePawnIcon);
        //whitePawnLabel.setOpaque(true);
        this.xPos = x;
        this.yPos = y;
        //this.draw();
    }

    @Override
    public void move(int newX, int newY){
        this.xPos = (newX/60)*60;                     //calcular nova pos
        this.yPos = (newY/60)*60;
        this.draw();
    }
    /*public void possibleMoves(){
        selectorMark.drawNew(this.xPos, this.yPos);
        selectorMark.drawNew(this.xPos - 60, this.yPos - 60);
        if (this.yPos == 420)  selectorMark.drawNew(this.xPos - 120, this.yPos - 120);
    }*/

    @Override
    public void draw(){
        //whitePawnIcon.paintIcon(null, chessGUI2.getGraphics(), xPos, xPos);
        whitePawnLabel.setBounds(this.xPos, this.yPos, this.xPos+60, this.yPos+60);     //x, y, width, height
    }

}

public class whitePiece{
    int xPos, yPos;

    public void move(){}
    public void draw(){}

}

First time putting whole code hope I edited it right hehe

Was it helpful?

Solution

  • Don't use CardLayout for this.
  • I'd use a JPanel that uses GridLayout to hold an 8x8 grid of chess square JPanels.
  • I'd place that in a JLayeredPane.
  • I'd add my pieces to the appropriate chess square JPanel.
  • When moving a piece, I'd lift it up to the drag layer of the JLayeredPane.

Also:

  • Don't draw directly on to a top-level window such as a JFrame.
  • Don't override the paint method.
  • Instead, if you must do drawing, override the paintComponent(Graphics g) of a JPanel or JComponent.
  • But again, if you create your chess board out of small JPanel chess squares, it is easy and natural to place pieces on the chess square and have it placed well.

For example, please check out my code here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top