Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Chess Board

Started by CodeTitan Dec 18, 2004 at 9:43 AM 1 replies 1.5k views
Original Post
CodeTitan
CodeTitan
I am trying to model a chessboard in a Swing application. This board will eventually have chesspieces on it. I tried making the application have a GridLayout, and then I put JLabels into the different sections of the grid. That doesn't work, because I need an 8 by 8 coordinate system. I want to be able to say: Move piece on (2, 2) to (2, 3) etc. This doesn't work with a GridLayout. How, then, would I model the chessboard with Swing? I can't seem to find any sort of Swing coordinate system-friendly Layout.
garazdawi
garazdawi
just create a overload the paint method in a JPanel object and use that new object to draw the chess board. Like so:

public class MyChessBoard extends JPanel {

public void paint(Graphics g){
// do graphics stuff with g
}
}

This class can be used just like any normal JPanel as it inherits all properties of it.
garazdawi - 'I put the laughter back in slaughter'
Nomad010
Nomad010
I am making a chess game as well. You could just use a null layout.

Example:

import javax.swing.*;import java.awt.*;class Chessboard extends JFrame{ int screenWidth = 0; int screenHeight = 0; Color black = Color.black; Color white = Color.white;  Container field;  public Chessboard() {  super("Chessboard");    screenWidth = getToolkit().getScreenSize().width;  screenHeight = getToolkit().getScreenSize().height;  setJMenuBar(new ChessMenu());  field = getContentPane(); //What we draw to  setSize(600,600);  setLocation((screenWidth - 600)/2, (screenHeight - 600)/2);  setVisible(true); }  public static void main(String args[]) {  new Chessboard(); }


and

import javax.swing.*;import java.awt.*;import java.awt.event.*;class ChessMenu extends JMenuBar{ JMenu fileMenu; JMenu newGame;  JMenu gameMenu; JMenu difficulty; JMenu help;   JMenuItem openGame; JMenuItem openReplay; JMenuItem saveGame;  JMenuItem exit;  JMenuItem PvP;  JMenuItem PvC; JMenuItem CvC;  JMenuItem undoMove; JMenuItem redoMove; JMenuItem resign;  JMenuItem easyMode; JMenuItem moderateMode; JMenuItem hardMode; JMenuItem about;    public ChessMenu() {  fileMenu = new JMenu("File");  newGame = new JMenu("New Game");  gameMenu = new JMenu("Game");  difficulty = new JMenu("Difficulty");  help = new JMenu("Help");    PvP = new JMenuItem("Player vs. Player");  PvP.addActionListener(new ActionListener()  {   public void actionPerformed(ActionEvent e)   {    //Start a new game with two human players   }  });    PvC = new JMenuItem("Player vs. Computer");  PvC.addActionListener(new ActionListener()  {   public void actionPer  }  CvC = new JMenuItem("Computer vs. Computer");  openGame = new JMenuItem("Open Game...");  saveGame = new JMenuItem("Save Game");  openReplay = new JMenuItem("Open Replay...");  exit = new JMenuItem("Exit");  undoMove = new JMenuItem("Undo Move");  redoMove = new JMenuItem("Redo Move");  easyMode = new JMenuItem("Easy Mode");  moderateMode = new JMenuItem("Moderate Mode");  hardMode = new JMenuItem("Hard Mode");  resign = new JMenuItem("Resign");  about = new JMenuItem("About");     fileMenu.add(newGame);  fileMenu.add(openGame);  fileMenu.addSeparator();  fileMenu.add(saveGame);  fileMenu.addSeparator();  fileMenu.add(openReplay);  fileMenu.addSeparator();  fileMenu.add(exit);    newGame.add(PvP);  newGame.add(PvC);  newGame.add(CvC);  gameMenu.add(undoMove);  gameMenu.add(redoMove);  gameMenu.addSeparator();  gameMenu.add(difficulty);  gameMenu.addSeparator();  gameMenu.add(resign);    difficulty.add(easyMode);  difficulty.add(moderateMode);  difficulty.add(hardMode);    help.add(about);    add(fileMenu);  add(gameMenu);  add(help); } }


That's just a snippet of what I have done. The null parameter allows you enter in coord-based positions and have them work. However there are a couple of other problems that must be worked around.(Like having to set the container invisible and visible again just to refresh it.)
"..."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.