Chess Pieces not rendering correctly
I am creating a chess game and I am now adding the pieces. All the pieces have rendered individually correctly, but now that I am trying to set them in their location they are not appearing where they should. What in my code is causing the pieces to generate wrong?
public class Tile {
Graphics g;
public static HashMap<HashMap<Integer, Integer>, String> pieces = new HashMap<>();
public Tile(Graphics g) {
this.g = g;
}
public static void setPieceLoc(int x, int y, String tile) {
x = (x*64) + 20;
y = (y*64) + 20;
HashMap<Integer, Integer> tileNum = new HashMap<>();
tileNum.put(x, y);
pieces.put(tileNum, tile);
}
public void tick() {
registerPieces();
}
private static void registerPieces() {
//Pawns
for(int x = 0; x < 8; x++) {
setPieceLoc(x, 1, "pawn");
setPieceLoc(x, 6, "pawn");
}
for(int y = 0; y < 1; y++) {
int cy = y*7;
setPieceLoc(0, cy, "rook");
setPieceLoc(1, cy, "knight");
setPieceLoc(2, cy, "bishop");
setPieceLoc(5, cy, "bishop");
setPieceLoc(6, cy, "knight");
setPieceLoc(7, cy, "rook");
}
setPieceLoc(3, 0, "queen");
setPieceLoc(4, 0, "king");
setPieceLoc(3, 0, "queen");
setPieceLoc(4, 7, "king");
}
public void render(Graphics g) {
for(HashMap<Integer, Integer> tile : pieces.keySet()) {
int x = 0;
int y = 0;
for(Integer xy : tile.keySet()) {
x = xy;
y = tile.get(xy);
}
String piecesName = pieces.get(tile);
g.drawImage(ChessImages.getPieceImage(piecesName), x, y, null);
}
}
}
Here is what it is generating look like:
My guess is your pieces are black, and your board is black so you can't see them.
Unrelated, but you are using a HashMap to store coordinates of your pieces. This is completely unnecessary and I would recommend you use a Point.
链接地址: http://www.djcxy.com/p/84628.html下一篇: 棋子不能正确渲染