我目前正在制作基于Java的跳棋游戏我有很多调试代码可以证明存储每个部分的数组正在更新,但由于某种原因图形不会改变。我的主要课程:
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();
}
}
这是我的鼠标事件类。老实说,我不知道这有什么问题,所以我认为信息越多越好
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
}
}
我只是不明白为什么碎片数组在变化并且 paintComponent 方法正在运行,但碎片根本不移动。
例如我移动一块之前和之后的板子
您的问题是,当您绘制碎片时,您使用存储在其中的x
和y
值。但是,当您移动作品时,您永远不会更新它们;您只需将其放在数组中的新位置,而它仍然包含其旧位置。
要解决此问题,您应该在移动它时更新这些值,或者(不太可能中断),从数组中的位置找出正确的绘制位置,而不是从存储在块中的值。