473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Chess program

how to make a appropriate move for each pawns? I knew that I should used some HashMap or classes for every pawn,but I don't know how to implement in code...

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener
  8. {
  9.     JLayeredPane layeredPane;
  10.     JPanel chessBoard;
  11.     JLabel chessPiece;
  12.     int xAdjustment;
  13.     int yAdjustment;
  14.      private JFileChooser chooser;
  15.      Component prev;
  16.  
  17.     public ChessBoard()
  18.     {
  19.  
  20.  
  21.         setTitle("Chessboard by Radek");
  22.  
  23.         chooser = new JFileChooser();
  24.         chooser.setCurrentDirectory(new File("."));
  25.  
  26.         JMenuBar menuBar = new JMenuBar();
  27.         setJMenuBar(menuBar);
  28.  
  29.         JMenu menu = new JMenu("File");
  30.         menuBar.add(menu);
  31.  
  32.         JMenu menu1 = new JMenu("Edit");
  33.         menuBar.add(menu1);
  34.  
  35.  
  36.         JMenuItem openItem = new JMenuItem("Open");
  37.         menu.add(openItem);
  38.         openItem.addActionListener(new ActionListener()
  39.         {
  40.             public void actionPerformed(ActionEvent evt)
  41.             {
  42.                 int r = chooser.showOpenDialog(null);
  43.                 if(r == JFileChooser.APPROVE_OPTION)
  44.                 {
  45.                     String name = chooser.getSelectedFile().getPath();
  46.                 }
  47.             }
  48.         });
  49.  
  50.         JMenuItem saveItem = new JMenuItem("Save");
  51.         menu.add(saveItem);
  52.  
  53.         JMenuItem exitItem = new JMenuItem("Exit");
  54.         menu.add(exitItem);
  55.         exitItem.addActionListener(new ActionListener()
  56.         {
  57.             public void actionPerformed(ActionEvent event){
  58.                 System.exit(0);
  59.             }
  60.         });
  61.  
  62.         JMenuItem zoomItem = new JMenuItem("size +");
  63.         menu1.add(zoomItem);
  64.         zoomItem.addActionListener(new ActionListener(){
  65.             public void actionPerformed(ActionEvent evt){
  66.                 setSize (300, 300);
  67.             }
  68.         });
  69.  
  70.         JMenuItem zoomOutItem = new JMenuItem("size -");
  71.         menu1.add(zoomOutItem);
  72.         zoomOutItem.addActionListener(new ActionListener(){
  73.             public void actionPerformed(ActionEvent evt){
  74.                 setSize (630, 670);
  75.             }
  76.         });
  77.  
  78.         Dimension Size = new Dimension(600, 600);
  79.  
  80.         //  Use a Layered Pane for this application
  81.  
  82.         layeredPane = new JLayeredPane();
  83.         getContentPane().add(layeredPane);
  84.         layeredPane.setPreferredSize( Size );
  85.         layeredPane.addMouseListener( this );
  86.         layeredPane.addMouseMotionListener( this );
  87.  
  88.         //  Add a chess board to the Layered Pane
  89.  
  90.         chessBoard = new JPanel();
  91.         layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
  92.         chessBoard.setLayout( new GridLayout(8, 8) );
  93.         chessBoard.setPreferredSize( Size );
  94.         chessBoard.setBounds(2, 2, Size.width, Size.height);
  95.  
  96.  
  97.  
  98.         for (int i = 0; i < 64; i++)
  99.         {
  100.             JPanel square = new JPanel( new BorderLayout() );
  101.             chessBoard.add( square );
  102.  
  103.             int row = (i / 8) % 2;
  104.             if (row == 0)
  105.                 square.setBackground( i % 2 == 0 ? Color.black : Color.white );
  106.             else
  107.                 square.setBackground( i % 2 == 0 ? Color.white : Color.black );
  108.         }
  109.  
  110.         // Add a few pieces to the board but I don't know is it ok? maybe it should be in some loop?
  111.  
  112.         JLabel piece = new JLabel( new ImageIcon("pw.png") );
  113.         JPanel panel = (JPanel)chessBoard.getComponent( 8 );
  114.         panel.add( piece );
  115.         piece = new JLabel( new ImageIcon("pw.png") );
  116.         panel = (JPanel)chessBoard.getComponent( 9 );
  117.         panel.add( piece );
  118.         piece = new JLabel( new ImageIcon("pw.png") );
  119.         panel = (JPanel)chessBoard.getComponent( 10 );
  120.         panel.add( piece );
  121.         piece = new JLabel( new ImageIcon("pw.png") );
  122.         panel = (JPanel)chessBoard.getComponent( 11 );
  123.         panel.add( piece );
  124.         piece = new JLabel( new ImageIcon("pw.png") );
  125.         panel = (JPanel)chessBoard.getComponent( 12 );
  126.         panel.add( piece );
  127.         piece = new JLabel( new ImageIcon("pw.png") );
  128.         panel = (JPanel)chessBoard.getComponent( 13 );
  129.         panel.add( piece );
  130.         piece = new JLabel( new ImageIcon("pw.png") );
  131.         panel = (JPanel)chessBoard.getComponent( 14 );
  132.         panel.add( piece );
  133.         piece = new JLabel( new ImageIcon("pw.png") );
  134.         panel = (JPanel)chessBoard.getComponent( 15 );
  135.         panel.add( piece );
  136.         piece = new JLabel( new ImageIcon("ww.png") );
  137.         panel = (JPanel)chessBoard.getComponent( 0 );
  138.         panel.add( piece );
  139.         piece = new JLabel( new ImageIcon("ww.png") );
  140.         panel = (JPanel)chessBoard.getComponent( 7 );
  141.         panel.add( piece );
  142.         piece = new JLabel( new ImageIcon("hw.png") );
  143.         panel = (JPanel)chessBoard.getComponent( 1 );
  144.         panel.add( piece );
  145.         piece = new JLabel( new ImageIcon("hw.png") );
  146.         panel = (JPanel)chessBoard.getComponent( 6 );
  147.         panel.add( piece );
  148.         piece = new JLabel( new ImageIcon("bw.png") );
  149.         panel = (JPanel)chessBoard.getComponent( 2 );
  150.         panel.add( piece );
  151.         piece = new JLabel( new ImageIcon("bw.png") );
  152.         panel = (JPanel)chessBoard.getComponent( 5 );
  153.         panel.add( piece );
  154.         piece = new JLabel( new ImageIcon("qw.png") );
  155.         panel = (JPanel)chessBoard.getComponent( 3 );
  156.         panel.add( piece );
  157.         piece = new JLabel( new ImageIcon("kgw.png") );
  158.         panel = (JPanel)chessBoard.getComponent( 4 );
  159.         panel.add( piece );
  160.  
  161.         //black
  162.  
  163.         piece = new JLabel( new ImageIcon("pc.png") );
  164.         panel = (JPanel)chessBoard.getComponent( 48 );
  165.         panel.add( piece );
  166.         piece = new JLabel( new ImageIcon("pc.png") );
  167.         panel = (JPanel)chessBoard.getComponent( 49 );
  168.         panel.add( piece );
  169.         piece = new JLabel( new ImageIcon("pc.png") );
  170.         panel = (JPanel)chessBoard.getComponent( 50 );
  171.         panel.add( piece );
  172.         piece = new JLabel( new ImageIcon("pc.png") );
  173.         panel = (JPanel)chessBoard.getComponent( 51 );
  174.         panel.add( piece );
  175.         piece = new JLabel( new ImageIcon("pc.png") );
  176.         panel = (JPanel)chessBoard.getComponent( 52 );
  177.         panel.add( piece );
  178.         piece = new JLabel( new ImageIcon("pc.png") );
  179.         panel = (JPanel)chessBoard.getComponent( 53 );
  180.         panel.add( piece );
  181.         piece = new JLabel( new ImageIcon("pc.png") );
  182.         panel = (JPanel)chessBoard.getComponent( 54 );
  183.         panel.add( piece );
  184.         piece = new JLabel( new ImageIcon("pc.png") );
  185.         panel = (JPanel)chessBoard.getComponent( 55 );
  186.         panel.add( piece );
  187.         piece = new JLabel( new ImageIcon("wc.png") );
  188.         panel = (JPanel)chessBoard.getComponent( 56 );
  189.         panel.add( piece );
  190.         piece = new JLabel( new ImageIcon("wc.png") );
  191.         panel = (JPanel)chessBoard.getComponent( 63 );
  192.         panel.add( piece );
  193.         piece = new JLabel( new ImageIcon("hc.png") );
  194.         panel = (JPanel)chessBoard.getComponent( 57 );
  195.         panel.add( piece );
  196.         piece = new JLabel( new ImageIcon("hc.png") );
  197.         panel = (JPanel)chessBoard.getComponent( 62 );
  198.         panel.add( piece );
  199.         piece = new JLabel( new ImageIcon("bc.png") );
  200.         panel = (JPanel)chessBoard.getComponent( 58 );
  201.         panel.add( piece );
  202.         piece = new JLabel( new ImageIcon("bc.png") );
  203.         panel = (JPanel)chessBoard.getComponent( 61 );
  204.         panel.add( piece );
  205.         piece = new JLabel( new ImageIcon("qc.png") );
  206.         panel = (JPanel)chessBoard.getComponent( 59 );
  207.         panel.add( piece );
  208.         piece = new JLabel( new ImageIcon("kgc.png") );
  209.         panel = (JPanel)chessBoard.getComponent( 60 );
  210.         panel.add( piece );
  211.  
  212.  
  213.     }
  214.  
  215.  
  216.     public void mousePressed(MouseEvent e)
  217.     {
  218.         chessPiece = null;
  219.         Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
  220.          prev = c.getParent();
  221.         if (c instanceof JPanel) return;
  222.  
  223.         Point parentLocation = c.getParent().getLocation();
  224.         xAdjustment = parentLocation.x - e.getX();
  225.         yAdjustment = parentLocation.y - e.getY();
  226.         chessPiece = (JLabel)c;
  227.         chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
  228.         chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
  229.         layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
  230.     }
  231.  
  232.  
  233.     public void mouseDragged(MouseEvent me)
  234.     {
  235.         if (chessPiece == null) return;
  236.  
  237.         chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
  238.      }
  239.  
  240.  
  241.     public void mouseReleased(MouseEvent e)
  242.     {
  243.  
  244. try
  245.     {
  246.         if (chessPiece == null) return;
  247.  
  248.         chessPiece.setVisible(false);
  249.         Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
  250.  
  251.         if (c instanceof JLabel)
  252.         {
  253.             Container parent = (Container)prev;
  254.             parent.add( chessPiece );
  255.         }
  256.         else
  257.         {
  258.             Container parent = (Container)c;
  259.             parent.add( chessPiece );
  260.         }
  261.  
  262.         chessPiece.setVisible(true);
  263.  
  264.     }catch(NullPointerException exception)
  265.     {
  266.         Container parent = (Container)prev;
  267.         parent.add( chessPiece );
  268.         chessPiece.setVisible(true);
  269.     }
  270.     }
  271.  
  272.     public void mouseClicked(MouseEvent e) {}
  273.     public void mouseMoved(MouseEvent e) {}
  274.     public void mouseEntered(MouseEvent e) {}
  275.     public void mouseExited(MouseEvent e) {}
  276.  
  277.  
  278.  
  279.     public static void main(String[] args)
  280.     {
  281.         JFrame frame = new ChessBoard();
  282.         frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
  283.         frame.pack();
  284.         frame.setResizable( false );
  285.         frame.setLocationRelativeTo( null );
  286.         frame.setVisible(true);
  287.      }
  288. }
Aug 29 '07 #1
7 3273
Nepomuk
3,112 Expert 2GB
how to make a appropriate move for each pawns? I knew that I should used some HashMap or classes for every pawn,but I don't know how to implement in code...

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. ...
  2.         // Add a few pieces to the board but I don't know is it ok? maybe it should be in some loop?
  3.  
  4.         JLabel piece = new JLabel( new ImageIcon("pw.png") );
  5.         JPanel panel = (JPanel)chessBoard.getComponent( 8 );
  6.         panel.add( piece );
  7. ...
  8.     }
  9. ...
  10.  
First of all: It is ok to add the pieces in that way, but you can certainly save code by adding any pieces which have the same Image (if there are more than 2 with the same Image) in a loop.

OK, next Question. Indeed, a HashMap would be a good idea. But first of all, I would create an interface for all figures. Something like this:
Expand|Select|Wrap|Line Numbers
  1. public interface Figure {
  2.  
  3.     // Every figure has to be moved - but not to an illegal Field
  4.     public void move(int x, int y) throws IllegalFieldException;
  5.  
  6.     // You have to be able to find out, where a figure is.
  7.     public int xPosition();
  8.     public int yPosition();
  9.  
  10.     // Of course, you have to know, if a figure is still on the field
  11.     public boolean captured();
  12.  
  13.     // And you have to be able to capture figures
  14.     public void capture();
  15. }
  16. }
  17.  
From this, you implement the various figures. When done, you can add them to your Field:
Expand|Select|Wrap|Line Numbers
  1. HashMap[] figure = new Figure[16];
  2. for(int i=0;i<8;i++)
  3. {
  4.     Figures.add("Pawn" + i;new Pawn(i,1));
  5. }
  6. // Add the other figures
  7. ...
  8.  
And continue from that.
The rules about how you can move a figure (e.g. a Pawn) should be programmed in the "move" method.
Aug 29 '07 #2
In this program The GUI must enable:

* drag'n'drop of pieces,
* entering moves with the keyboard,
* displaying information concerning the current move,
* highlighting the active player.
* forbid illegal moves,
* remember game history (each move),
* enable storing/loading a game to/from a file,

maybe somebode can do that whit this code...
I will be very gratefull.
Aug 29 '07 #3
sicarie
4,677 Expert Mod 4TB
Those look like things you can do if you read nepomuk's post and implement the suggestions. Or are you stuck on one of them?
Aug 29 '07 #4
Nepomuk
3,112 Expert 2GB
* forbid illegal moves,
The "IllegalFieldException" (you can call it "IllegalMoveException" of course) can take care of most of that. You just have to catch it and react.
* remember game history (each move),
You could create a container class called Move, which saves a Move (from where, to where, which Figure) and a Vector of Moves. The Vector can then save the History of all Moves.

However, there might be a better solution.
* enable storing/loading a game to/from a file,
Read this article about reading and writing files.
Aug 29 '07 #5
Nepomuk
3,112 Expert 2GB
The "IllegalFieldException" (you can call it "IllegalMoveException" of course) can take care of most of that. You just have to catch it and react.
Actually, you could also implement a function called public boolean legalMove(int x, int y) in the interface to test, if a Move is legal. That might save some work.
Aug 29 '07 #6
I stuck in add pawns, I can't see them on the board. probably I should add this by JPanel but in that way it doesn't work...


Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.io.*;
  5. import java.util.*;
  6. import java.util.HashMap;
  7.  
  8. public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener
  9. {
  10.     JLayeredPane layeredPane;
  11.     JPanel chessBoard;
  12.     JLabel chessPiece;
  13.     int xAdjustment;
  14.     int yAdjustment;
  15.      private JFileChooser chooser;
  16.      Component prev;
  17.  
  18.     private static HashMap WhiteFigures = new HashMap();
  19.     private static HashMap BlackFigures = new HashMap();
  20.  
  21.  
  22.     public ChessBoard()
  23.     {
  24.  
  25.  
  26.         setTitle("Chessboard by Radek");
  27.  
  28.         chooser = new JFileChooser();
  29.         chooser.setCurrentDirectory(new File("."));
  30.  
  31.         JMenuBar menuBar = new JMenuBar();
  32.         setJMenuBar(menuBar);
  33.  
  34.         JMenu menu = new JMenu("File");
  35.         menuBar.add(menu);
  36.  
  37.         JMenu menu1 = new JMenu("Edit");
  38.         menuBar.add(menu1);
  39.  
  40.  
  41.         JMenuItem openItem = new JMenuItem("Open");
  42.         menu.add(openItem);
  43.         openItem.addActionListener(new ActionListener()
  44.         {
  45.             public void actionPerformed(ActionEvent evt)
  46.             {
  47.                 int r = chooser.showOpenDialog(null);
  48.                 if(r == JFileChooser.APPROVE_OPTION)
  49.                 {
  50.                     String name = chooser.getSelectedFile().getPath();
  51.                 }
  52.             }
  53.         });
  54.  
  55.         JMenuItem saveItem = new JMenuItem("Save");
  56.         menu.add(saveItem);
  57.  
  58.         JMenuItem exitItem = new JMenuItem("Exit");
  59.         menu.add(exitItem);
  60.         exitItem.addActionListener(new ActionListener()
  61.         {
  62.             public void actionPerformed(ActionEvent event){
  63.                 System.exit(0);
  64.             }
  65.         });
  66.  
  67.         JMenuItem zoomItem = new JMenuItem("size +");
  68.         menu1.add(zoomItem);
  69.         zoomItem.addActionListener(new ActionListener(){
  70.             public void actionPerformed(ActionEvent evt){
  71.                 setSize (300, 300);
  72.             }
  73.         });
  74.  
  75.         JMenuItem zoomOutItem = new JMenuItem("size -");
  76.         menu1.add(zoomOutItem);
  77.         zoomOutItem.addActionListener(new ActionListener(){
  78.             public void actionPerformed(ActionEvent evt){
  79.                 setSize (630, 670);
  80.             }
  81.         });
  82.  
  83.         Dimension Size = new Dimension(600, 600);
  84.  
  85.         //  Use a Layered Pane for this application
  86.  
  87.         layeredPane = new JLayeredPane();
  88.         getContentPane().add(layeredPane);
  89.         layeredPane.setPreferredSize( Size );
  90.         layeredPane.addMouseListener( this );
  91.         layeredPane.addMouseMotionListener( this );
  92.  
  93.         //  Add a chess board to the Layered Pane
  94.  
  95.         chessBoard = new JPanel();
  96.         layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
  97.         chessBoard.setLayout( new GridLayout(8, 8) );
  98.         chessBoard.setPreferredSize( Size );
  99.         chessBoard.setBounds(2, 2, Size.width, Size.height);
  100.  
  101.  
  102.  
  103.         for (int i = 0; i < 64; i++)
  104.         {
  105.             JPanel square = new JPanel( new BorderLayout() );
  106.             chessBoard.add( square );
  107.  
  108.             int row = (i / 8) % 2;
  109.             if (row == 0)
  110.                 square.setBackground( i % 2 == 0 ? Color.black : Color.white );
  111.             else
  112.                 square.setBackground( i % 2 == 0 ? Color.white : Color.black );
  113.         }
  114.  
  115.         Figure[] whiteFigure = new Figure[16];
  116.  
  117.  
  118.         // Add Pawns
  119.         for(int i=0;i<8;i++)
  120.         {
  121.             whiteFigure[i] = new Pawn(1,i);
  122.             WhiteFigures.put("pw.png" + i, whiteFigure[i]);
  123.         }
  124.         //add king
  125.         whiteFigure[12] = new King(0,4);
  126.         WhiteFigures.put("kgw.png", whiteFigure[12]);
  127.  
  128.     /*    JLabel piece = new JLabel( new ImageIcon("pw.png") );
  129.         JPanel panel = (JPanel)chessBoard.getComponent( 8 );
  130.         panel.add( piece );
  131.         piece = new JLabel( new ImageIcon("pw.png") );
  132.         panel = (JPanel)chessBoard.getComponent( 9 );
  133.         panel.add( piece );
  134.         piece = new JLabel( new ImageIcon("pw.png") );
  135.         panel = (JPanel)chessBoard.getComponent( 10 );
  136.         panel.add( piece );
  137.         piece = new JLabel( new ImageIcon("pw.png") );
  138.         panel = (JPanel)chessBoard.getComponent( 11 );
  139.         panel.add( piece );
  140.         piece = new JLabel( new ImageIcon("pw.png") );
  141.         panel = (JPanel)chessBoard.getComponent( 12 );
  142.         panel.add( piece );
  143.         piece = new JLabel( new ImageIcon("pw.png") );
  144.         panel = (JPanel)chessBoard.getComponent( 13 );
  145.         panel.add( piece );
  146.         piece = new JLabel( new ImageIcon("pw.png") );
  147.         panel = (JPanel)chessBoard.getComponent( 14 );
  148.         panel.add( piece );
  149.         piece = new JLabel( new ImageIcon("pw.png") );
  150.         panel = (JPanel)chessBoard.getComponent( 15 );
  151.         panel.add( piece );
  152.         piece = new JLabel( new ImageIcon("ww.png") );
  153.         panel = (JPanel)chessBoard.getComponent( 0 );
  154.         panel.add( piece );
  155.         piece = new JLabel( new ImageIcon("ww.png") );
  156.         panel = (JPanel)chessBoard.getComponent( 7 );
  157.         panel.add( piece );
  158.         piece = new JLabel( new ImageIcon("hw.png") );
  159.         panel = (JPanel)chessBoard.getComponent( 1 );
  160.         panel.add( piece );
  161.         piece = new JLabel( new ImageIcon("hw.png") );
  162.         panel = (JPanel)chessBoard.getComponent( 6 );
  163.         panel.add( piece );
  164.         piece = new JLabel( new ImageIcon("bw.png") );
  165.         panel = (JPanel)chessBoard.getComponent( 2 );
  166.         panel.add( piece );
  167.         piece = new JLabel( new ImageIcon("bw.png") );
  168.         panel = (JPanel)chessBoard.getComponent( 5 );
  169.         panel.add( piece );
  170.         piece = new JLabel( new ImageIcon("qw.png") );
  171.         panel = (JPanel)chessBoard.getComponent( 3 );
  172.         panel.add( piece );
  173.         piece = new JLabel( new ImageIcon("kgw.png") );
  174.         panel = (JPanel)chessBoard.getComponent( 4 );
  175.         panel.add( piece );
  176.  
  177.         //black
  178.  
  179.         piece = new JLabel( new ImageIcon("pc.png") );
  180.         panel = (JPanel)chessBoard.getComponent( 48 );
  181.         panel.add( piece );
  182.         piece = new JLabel( new ImageIcon("pc.png") );
  183.         panel = (JPanel)chessBoard.getComponent( 49 );
  184.         panel.add( piece );
  185.         piece = new JLabel( new ImageIcon("pc.png") );
  186.         panel = (JPanel)chessBoard.getComponent( 50 );
  187.         panel.add( piece );
  188.         piece = new JLabel( new ImageIcon("pc.png") );
  189.         panel = (JPanel)chessBoard.getComponent( 51 );
  190.         panel.add( piece );
  191.         piece = new JLabel( new ImageIcon("pc.png") );
  192.         panel = (JPanel)chessBoard.getComponent( 52 );
  193.         panel.add( piece );
  194.         piece = new JLabel( new ImageIcon("pc.png") );
  195.         panel = (JPanel)chessBoard.getComponent( 53 );
  196.         panel.add( piece );
  197.         piece = new JLabel( new ImageIcon("pc.png") );
  198.         panel = (JPanel)chessBoard.getComponent( 54 );
  199.         panel.add( piece );
  200.         piece = new JLabel( new ImageIcon("pc.png") );
  201.         panel = (JPanel)chessBoard.getComponent( 55 );
  202.         panel.add( piece );
  203.         piece = new JLabel( new ImageIcon("wc.png") );
  204.         panel = (JPanel)chessBoard.getComponent( 56 );
  205.         panel.add( piece );
  206.         piece = new JLabel( new ImageIcon("wc.png") );
  207.         panel = (JPanel)chessBoard.getComponent( 63 );
  208.         panel.add( piece );
  209.         piece = new JLabel( new ImageIcon("hc.png") );
  210.         panel = (JPanel)chessBoard.getComponent( 57 );
  211.         panel.add( piece );
  212.         piece = new JLabel( new ImageIcon("hc.png") );
  213.         panel = (JPanel)chessBoard.getComponent( 62 );
  214.         panel.add( piece );
  215.         piece = new JLabel( new ImageIcon("bc.png") );
  216.         panel = (JPanel)chessBoard.getComponent( 58 );
  217.         panel.add( piece );
  218.         piece = new JLabel( new ImageIcon("bc.png") );
  219.         panel = (JPanel)chessBoard.getComponent( 61 );
  220.         panel.add( piece );
  221.         piece = new JLabel( new ImageIcon("qc.png") );
  222.         panel = (JPanel)chessBoard.getComponent( 59 );
  223.         panel.add( piece );
  224.         piece = new JLabel( new ImageIcon("kgc.png") );
  225.         panel = (JPanel)chessBoard.getComponent( 60 );
  226.         panel.add( piece );
  227. */
  228.  
  229.     }
  230.  
  231.  
  232.     public void mousePressed(MouseEvent e)
  233.     {
  234.         chessPiece = null;
  235.         Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
  236.          prev = c.getParent();
  237.         if (c instanceof JPanel) return;
  238.  
  239.         Point parentLocation = c.getParent().getLocation();
  240.         xAdjustment = parentLocation.x - e.getX();
  241.         yAdjustment = parentLocation.y - e.getY();
  242.         chessPiece = (JLabel)c;
  243.         chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
  244.         chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
  245.         layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
  246.     }
  247.  
  248.  
  249.     public void mouseDragged(MouseEvent me)
  250.     {
  251.         if (chessPiece == null) return;
  252.  
  253.         chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
  254.      }
  255.  
  256.  
  257.     public void mouseReleased(MouseEvent e)
  258.     {
  259.  
  260. try
  261.     {
  262.         if (chessPiece == null) return;
  263.  
  264.         chessPiece.setVisible(false);
  265.         Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
  266.  
  267.         if (c instanceof JLabel)
  268.         {
  269.    Container parent = (Container)prev;
  270.    parent.add( chessPiece );
  271.         }
  272.         else
  273.         {
  274.             Container parent = (Container)c;
  275.             parent.add( chessPiece );
  276.         }
  277.  
  278.         chessPiece.setVisible(true);
  279.  
  280.     }catch(NullPointerException exception)
  281.     {
  282.         Container parent = (Container)prev;
  283.         parent.add( chessPiece );
  284.         chessPiece.setVisible(true);
  285.     }
  286.     }
  287.  
  288.     public void mouseClicked(MouseEvent e) {}
  289.     public void mouseMoved(MouseEvent e) {}
  290.     public void mouseEntered(MouseEvent e) {}
  291.     public void mouseExited(MouseEvent e) {}
  292.  
  293.  
  294.  
  295.     public static void main(String[] args)
  296.     {
  297.         JFrame frame = new ChessBoard();
  298.         frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
  299.         frame.pack();
  300.         frame.setResizable( false );
  301.         frame.setLocationRelativeTo( null );
  302.         frame.setVisible(true);
  303.  
  304.  
  305.  
  306.  
  307.         }
  308.      }
  309.  
  310.  
Aug 31 '07 #7
Nepomuk
3,112 Expert 2GB
I stuck in add pawns, I can't see them on the board. probably I should add this by JPanel but in that way it doesn't work...

Expand|Select|Wrap|Line Numbers
  1.  
  2.         Figure[] whiteFigure = new Figure[16];
  3.  
  4.         // Add Pawns
  5.         for(int i=0;i<8;i++)
  6.         {
  7.             whiteFigure[i] = new Pawn(1,i);
  8.             WhiteFigures.put("pw.png" + i, whiteFigure[i]);
  9.         }
  10.         //add king
  11.         whiteFigure[12] = new King(0,4);
  12.         WhiteFigures.put("kgw.png", whiteFigure[12]);
  13.  
  14.     /*    JLabel piece = new JLabel( new ImageIcon("pw.png") );
  15.         JPanel panel = (JPanel)chessBoard.getComponent( 8 );
  16.         panel.add( piece );
  17.         ...
  18.  
  19.         //black
  20.  
  21.         piece = new JLabel( new ImageIcon("pc.png") );
  22.         panel = (JPanel)chessBoard.getComponent( 48 );
  23.         panel.add( piece );
  24.         ...
  25. */
  26.     }
  27.  
  28. ...
  29.  
  30.     public static void main(String[] args)
  31.     {
  32.         JFrame frame = new ChessBoard();
  33.         frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
  34.         frame.pack();
  35.         frame.setResizable( false );
  36.         frame.setLocationRelativeTo( null );
  37.         frame.setVisible(true);
  38.         }
  39. }
  40.  
It doesn't surprise me, that you can't see the Figures - you haven't at any point connected the Pictures with them!
The Line
Expand|Select|Wrap|Line Numbers
  1. WhiteFigures.put("pw.png" + i, whiteFigure[i]);
only means, that you can call your (virtual) Figure by using
Expand|Select|Wrap|Line Numbers
  1. WhiteFigures.get("pw.png1"); // If i == 1
Anyway, you do create a chessboard, but you use the same JLabel piece again and again. (OK, that's commented out.) I would suggest, you use an Array of JLabels instead.
Expand|Select|Wrap|Line Numbers
  1. JLabel [][] piece = new JLabel[8][8]; // This will give you an 8x8 Array of JLabels
Also you can (if you choose to do so) add a picture (or two, if you have the Fields Background in the same picture) to your Figures. Of course, you'd have to give them a way of setting the images and a getImage method, which returns the correct Image. (You could either have them calculate, if they are on a black or a white field, or you calculate that somewhere else and tell the Figure, which picture you want.)

Two last tips (for now):
1. If you use [code=java] instead of [code] in this Forum, it will be formated even better.
2. Please post all classes. (Figures.java, Pawn.java and King.java plus any other classes that you may have) It will make life so much easier for anyone who wants to help you.
Aug 31 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: David K | last post by:
Hi all I am tasked to write a chess program as a leisure exercise for an educational institution. Is there anyone out there that can assist and maybe show some additional pointers. Thanks
5
by: derian | last post by:
Do you guys know if there is a program that can generate chess moves? I recently wrote a chess game but its only 2 player... I deceided it was too difficult to come up with the logic for the AI...
11
by: Gregc. | last post by:
G'day I am writing a chess program. Here is my code: #include <stdio.h> #include <stdlib.h> bool isInCheck (int krow, int kcol, int qrow, int qcol) { double check;
1
by: Varun Hiremath | last post by:
Hello, I have written a chess client using python which is a graphic interface to play chess. It is at present a two player version, players move their peices by clicking two squares on the board...
13
by: asif929 | last post by:
I have been trying to create this chess program from many hours and still couldn't figure out how to complete it. After consume all these efforts i come to this google groups for the first time for...
63
by: biyubi | last post by:
Hi, a year ago I won the 2005 Best Game categoryof the International Obfuscated C Code Contestwith a chess program. http://www.ioccc.org/whowon2005.html...
10
by: sam_cit | last post by:
Hi Everyone, I'm working on developing a chess game and i decided to use c++ for its object oriented approach. I have a bass class unit and is inherited to distinct number of units (like king,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.