Added menu bar
This commit is contained in:
parent
a0c6c7605e
commit
824f26ac67
|
@ -6,19 +6,30 @@ import java.awt.*;
|
|||
*/
|
||||
public class Chess {
|
||||
|
||||
static JFrame window;
|
||||
|
||||
static Chessboard chessboard;
|
||||
|
||||
static JMenuBar menuBar;
|
||||
|
||||
static final String DEFAULT_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
|
||||
/**
|
||||
* Create the main window and chessboard with pieces
|
||||
* @param args command-line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
JFrame window = new JFrame();
|
||||
setLookAndFeel();
|
||||
|
||||
window = new JFrame();
|
||||
window.setTitle("Chess");
|
||||
window.setSize(800, 600);
|
||||
window.setMinimumSize(new Dimension(800, 600));
|
||||
window.setBackground(Color.GRAY);
|
||||
|
||||
Chessboard chessboard = Chessboard.fromFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
||||
window.setJMenuBar(createMenuBar());
|
||||
|
||||
chessboard = Chessboard.fromFEN(DEFAULT_FEN);
|
||||
|
||||
window.add(chessboard);
|
||||
window.pack();
|
||||
|
@ -29,4 +40,64 @@ public class Chess {
|
|||
|
||||
}
|
||||
|
||||
public static void setLookAndFeel() {
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (UnsupportedLookAndFeelException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static JMenuBar createMenuBar() {
|
||||
menuBar = new JMenuBar();
|
||||
|
||||
JMenu menu = new JMenu("Game");
|
||||
|
||||
JMenuItem startNewGame = new JMenuItem("Start new game");
|
||||
startNewGame.addActionListener(l -> newGame(Chessboard.fromFEN(DEFAULT_FEN)));
|
||||
menu.add(startNewGame);
|
||||
|
||||
JMenuItem newGameFromFEN = new JMenuItem("Load from FEN");
|
||||
newGameFromFEN.addActionListener(l -> {
|
||||
String fen = JOptionPane.showInputDialog("Please input FEN: ");
|
||||
if(fen == null) return;
|
||||
Chessboard c = Chessboard.fromFEN(fen);
|
||||
if(c != null) newGame(c);
|
||||
else JOptionPane.showMessageDialog(window, "Invalid FEN!");
|
||||
});
|
||||
menu.add(newGameFromFEN);
|
||||
|
||||
JMenuItem toggleBlindMode = new JMenuItem("Toggle blind mode");
|
||||
toggleBlindMode.addActionListener(l -> {
|
||||
chessboard.blindMode = !chessboard.blindMode;
|
||||
chessboard.repaint();
|
||||
});
|
||||
menu.add(toggleBlindMode);
|
||||
|
||||
menuBar.add(menu);
|
||||
|
||||
return menuBar;
|
||||
}
|
||||
|
||||
static void repaintWindow() {
|
||||
// Hack to force repaint of the chessboard (repaint is not working)
|
||||
int winWidth = window.getWidth();
|
||||
int winHeight = window.getHeight();
|
||||
window.resize(winWidth+1, winHeight+1);
|
||||
window.resize(winWidth, winHeight);
|
||||
}
|
||||
|
||||
static void newGame(Chessboard newChessboard) {
|
||||
window.remove(chessboard);
|
||||
chessboard = newChessboard;
|
||||
window.add(chessboard);
|
||||
repaintWindow();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -83,12 +83,13 @@ public class ChessboardMouseAdapter extends MouseAdapter {
|
|||
Chessboard c = (Chessboard) me.getSource();
|
||||
IPiece piece = c.getSelectedPiece();
|
||||
if (piece != null) {
|
||||
int yOffset = Chess.menuBar.getHeight();
|
||||
double totalScale = c.pieceScale * c.boardScale;
|
||||
c.getRootPane().repaint(piece.getRepaintRectangle(piece.getOverrideX(), piece.getOverrideY(), totalScale));
|
||||
c.getRootPane().repaint(piece.getRepaintRectangle(piece.getOverrideX(), piece.getOverrideY() + yOffset, totalScale));
|
||||
double pieceX = me.getX() - 50 * totalScale;
|
||||
double pieceY = me.getY() - 50 * totalScale;
|
||||
piece.setOverride(pieceX, pieceY);
|
||||
c.getRootPane().repaint(piece.getRepaintRectangle(pieceX, pieceY, totalScale));
|
||||
c.getRootPane().repaint(piece.getRepaintRectangle(pieceX, pieceY + yOffset, totalScale));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue