197 lines
4.8 KiB
Java
197 lines
4.8 KiB
Java
package windowInterface;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Graphics;
|
|
import java.awt.Image;
|
|
import java.awt.event.MouseAdapter;
|
|
import java.awt.event.MouseEvent;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.swing.JPanel;
|
|
|
|
import backend.Game;
|
|
import backend.Piece;
|
|
import backend.PieceType;
|
|
|
|
public class JPanelChessBoard extends JPanel {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
private Game myGame;
|
|
private MyInterface interfaceGlobal;
|
|
private BufferedImage spriteSheet;
|
|
private int PIECE_WIDTH = 16; //in spritesheet
|
|
private int PIECE_HEIGHT = 16; //in spritesheet
|
|
private int MARGIN = 6;
|
|
|
|
private boolean pieceSelectorMode;
|
|
private boolean selectedPieceIsWhite;
|
|
private PieceType selectedPieceType;
|
|
private boolean pieceAdderMode;
|
|
|
|
public JPanelChessBoard(MyInterface itf) {
|
|
super();
|
|
myGame = null;
|
|
interfaceGlobal = itf;
|
|
selectedPieceIsWhite = true;
|
|
selectedPieceType = PieceType.Pawn;
|
|
pieceSelectorMode = false;
|
|
try {
|
|
spriteSheet = ImageIO.read(new File("pieces.png"));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
pieceSelectorMode = false;
|
|
pieceAdderMode = false;
|
|
addMouseListener(new MouseAdapter() {
|
|
public void mousePressed(MouseEvent me) {
|
|
// System.out.println(me);
|
|
if(pieceSelectorMode) {
|
|
int x = Math.round(me.getX()/cellWidth());
|
|
selectedPieceType = PieceType.values()[5-x];
|
|
selectedPieceIsWhite = (me.getY() > cellHeight());
|
|
pieceSelectorMode = false;
|
|
} else {
|
|
if(myGame == null) {
|
|
interfaceGlobal.instantiateSimu();
|
|
}
|
|
int x = (me.getX()*myGame.getWidth())/getWidth();
|
|
int y = (me.getY()*myGame.getHeight())/getHeight();
|
|
if(pieceAdderMode) {
|
|
//TODO
|
|
myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y);
|
|
pieceAdderMode = false;
|
|
} else {
|
|
myGame.clickCoords(x,y);
|
|
}
|
|
}
|
|
repaint();
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
public void setGame(Game simu) {
|
|
myGame = simu;
|
|
}
|
|
|
|
@Override
|
|
protected void paintComponent(Graphics g) {
|
|
super.paintComponent(g);
|
|
this.setBackground(Color.darkGray);
|
|
if(pieceSelectorMode) {
|
|
g.drawImage(
|
|
spriteSheet,
|
|
0,
|
|
0,
|
|
Math.round(5*cellWidth()),
|
|
Math.round(2*cellHeight()),
|
|
null
|
|
);
|
|
return;
|
|
}
|
|
if (myGame != null) {
|
|
// Draw Interface from state of simulator
|
|
float cellWidth = cellWidth();
|
|
float cellHeight = cellHeight();
|
|
|
|
g.setColor(Color.pink);
|
|
for(int x=0; x<myGame.getWidth();x++) {
|
|
for (int y=0; y<myGame.getHeight(); y++) {
|
|
boolean isSelect = myGame.isSelected(x,y);
|
|
boolean isHighlight = myGame.isHighlighted(x,y);
|
|
if(isSelect) {
|
|
g.setColor(Color.red);
|
|
}
|
|
if(isHighlight) {
|
|
g.setColor(Color.blue);
|
|
}
|
|
if((x+y)%2==1 || isSelect || isHighlight) {
|
|
g.fillRect(
|
|
Math.round(x*cellWidth),
|
|
Math.round(y*cellHeight),
|
|
Math.round(cellWidth),
|
|
Math.round(cellHeight)
|
|
);
|
|
}
|
|
if(isHighlight || isSelect) {
|
|
g.setColor(Color.pink);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
g.setColor(Color.gray);
|
|
for(int x=0; x<myGame.getWidth();x++) {
|
|
int graphX = Math.round(x*cellWidth);
|
|
g.drawLine(graphX, 0, graphX, this.getHeight());
|
|
}
|
|
for (int y=0; y<myGame.getHeight(); y++) {
|
|
int graphY = Math.round(y*cellHeight);
|
|
g.drawLine(0, graphY, this.getWidth(), graphY);
|
|
}
|
|
|
|
for (Piece piece : myGame.getPieces()) {
|
|
drawPiece(g,piece);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void drawPiece(Graphics g, Piece piece) {
|
|
g.drawImage(
|
|
getChessPieceImageFromType(piece.getType(), piece.isWhite()),
|
|
MARGIN+(xCoordFromGame(piece.getX())),
|
|
MARGIN+(yCoordFromGame(piece.getY())),
|
|
null
|
|
);
|
|
}
|
|
|
|
|
|
private Image getChessPieceImageFromType(PieceType type, boolean isWhite) {
|
|
int x = spriteSheetPositionOfPieceType(type)*PIECE_WIDTH;
|
|
int y = PIECE_HEIGHT * (isWhite?1:0);
|
|
Image subImage = spriteSheet.getSubimage(x, y, PIECE_WIDTH, PIECE_HEIGHT);
|
|
return subImage.getScaledInstance(
|
|
Math.round(cellWidth())-2*MARGIN,
|
|
Math.round(cellHeight())-2*MARGIN, 0
|
|
);
|
|
}
|
|
|
|
private int spriteSheetPositionOfPieceType(PieceType type) {
|
|
return 5-type.ordinal();
|
|
}
|
|
|
|
private float cellWidth() {
|
|
return (float) this.getWidth()/ (float)myGame.getWidth();
|
|
}
|
|
private float cellHeight() {
|
|
return (float)this.getHeight()/ (float)myGame.getHeight();
|
|
}
|
|
private int xCoordFromGame(int x) {
|
|
return Math.round(x*cellWidth());
|
|
}
|
|
private int yCoordFromGame(int y) {
|
|
return Math.round(y*cellHeight());
|
|
}
|
|
|
|
public void togglePieceSelector() {
|
|
pieceSelectorMode = ! pieceSelectorMode;
|
|
}
|
|
|
|
public void toggleAdderMode() {
|
|
pieceAdderMode = ! pieceAdderMode;
|
|
}
|
|
|
|
public boolean isPieceSelectorMode() {
|
|
return pieceSelectorMode;
|
|
}
|
|
|
|
|
|
public boolean isPieceAdderMode() {
|
|
return pieceAdderMode;
|
|
}
|
|
|
|
}
|