0

私はチェスゲームを作っています。私はでペイントで作ったチェス盤を使用しています。480x480いくつかのスプライトからピースを取得し、それらのそれぞれ60x60と透明な背景を作成しました。チェス盤と駒を画面に収めることはできたのですが、位置がぐちゃぐちゃです。null レイアウトを使用しています。やった:

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

これ+25は、フレームの物が上にあり、配置が考慮されているように見えるためです。作品に関しては、例えば:

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

パラメータは xPos と yPos を設定します。境界関数については、次のことを行いました。

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

しかし、これは起こります:http://i.imgur.com/MF9Njbi.jpg

私がそれを行う場合:

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

これが起こります:http://i.imgur.com/Pm1fMSp.jpg

最初に: ポジショニングはどうなりましたか? 意図したとおりにならないのはなぜですか?2 番目: 8 番目のピースは人里離れた場所で何をしているのですか?

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(){}

}

初めてコード全体を入れて、私はそれを正しく編集したことを願っています

4

1 に答える 1

2
  • これには CardLayout を使用しないでください。
  • GridLayout を使用する JPanel を使用して、チェスの正方形の JPanel の 8x8 グリッドを保持します。
  • それを JLayeredPane に配置します。
  • 駒を適切なチェスの正方形 JPanel に追加します。
  • ピースを移動するときは、JLayeredPane のドラッグ レイヤーまで持ち上げます。

また:

  • JFrame などの最上位ウィンドウに直接描画しないでください。
  • paint メソッドをオーバーライドしないでください。
  • 代わりに、描画を行う必要がある場合paintComponent(Graphics g)は、JPanel または JComponent の をオーバーライドしてください。
  • しかし、繰り返しになりますが、小さな JPanel チェスの正方形からチェス盤を作成する場合、チェスの正方形に駒を配置し、適切に配置するのは簡単かつ自然です。

たとえば、ここで私のコードをチェックしてください。

于 2014-03-14T23:23:09.390 に答える