Chess program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • needhelp20
    New Member
    • Aug 2007
    • 3

    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:

    [CODE=java]import java.awt.*;
    import java.awt.event. *;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;

    public class ChessBoard extends JFrame implements MouseListener, MouseMotionList ener
    {
    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;
    private JFileChooser chooser;
    Component prev;

    public ChessBoard()
    {


    setTitle("Chess board by Radek");

    chooser = new JFileChooser();
    chooser.setCurr entDirectory(ne w File("."));

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(men uBar);

    JMenu menu = new JMenu("File");
    menuBar.add(men u);

    JMenu menu1 = new JMenu("Edit");
    menuBar.add(men u1);


    JMenuItem openItem = new JMenuItem("Open ");
    menu.add(openIt em);
    openItem.addAct ionListener(new ActionListener( )
    {
    public void actionPerformed (ActionEvent evt)
    {
    int r = chooser.showOpe nDialog(null);
    if(r == JFileChooser.AP PROVE_OPTION)
    {
    String name = chooser.getSele ctedFile().getP ath();
    }
    }
    });

    JMenuItem saveItem = new JMenuItem("Save ");
    menu.add(saveIt em);

    JMenuItem exitItem = new JMenuItem("Exit ");
    menu.add(exitIt em);
    exitItem.addAct ionListener(new ActionListener( )
    {
    public void actionPerformed (ActionEvent event){
    System.exit(0);
    }
    });

    JMenuItem zoomItem = new JMenuItem("size +");
    menu1.add(zoomI tem);
    zoomItem.addAct ionListener(new ActionListener( ){
    public void actionPerformed (ActionEvent evt){
    setSize (300, 300);
    }
    });

    JMenuItem zoomOutItem = new JMenuItem("size -");
    menu1.add(zoomO utItem);
    zoomOutItem.add ActionListener( new ActionListener( ){
    public void actionPerformed (ActionEvent evt){
    setSize (630, 670);
    }
    });

    Dimension Size = new Dimension(600, 600);

    // Use a Layered Pane for this application

    layeredPane = new JLayeredPane();
    getContentPane( ).add(layeredPa ne);
    layeredPane.set PreferredSize( Size );
    layeredPane.add MouseListener( this );
    layeredPane.add MouseMotionList ener( this );

    // Add a chess board to the Layered Pane

    chessBoard = new JPanel();
    layeredPane.add (chessBoard, JLayeredPane.DE FAULT_LAYER);
    chessBoard.setL ayout( new GridLayout(8, 8) );
    chessBoard.setP referredSize( Size );
    chessBoard.setB ounds(2, 2, Size.width, Size.height);



    for (int i = 0; i < 64; i++)
    {
    JPanel square = new JPanel( new BorderLayout() );
    chessBoard.add( square );

    int row = (i / 8) % 2;
    if (row == 0)
    square.setBackg round( i % 2 == 0 ? Color.black : Color.white );
    else
    square.setBackg round( i % 2 == 0 ? Color.white : Color.black );
    }

    // Add a few pieces to the board but I don't know is it ok? maybe it should be in some loop?

    JLabel piece = new JLabel( new ImageIcon("pw.p ng") );
    JPanel panel = (JPanel)chessBo ard.getComponen t( 8 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 9 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 10 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 11 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 12 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 13 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 14 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 15 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("ww.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 0 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("ww.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 7 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("hw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 1 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("hw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 6 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("bw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 2 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("bw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 5 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("qw.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 3 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("kgw. png") );
    panel = (JPanel)chessBo ard.getComponen t( 4 );
    panel.add( piece );

    //black

    piece = new JLabel( new ImageIcon("pc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 48 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 49 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 50 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 51 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 52 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 53 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 54 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("pc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 55 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("wc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 56 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("wc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 63 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("hc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 57 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("hc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 62 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("bc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 58 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("bc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 61 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("qc.p ng") );
    panel = (JPanel)chessBo ard.getComponen t( 59 );
    panel.add( piece );
    piece = new JLabel( new ImageIcon("kgc. png") );
    panel = (JPanel)chessBo ard.getComponen t( 60 );
    panel.add( piece );


    }


    public void mousePressed(Mo useEvent e)
    {
    chessPiece = null;
    Component c = chessBoard.find ComponentAt(e.g etX(), e.getY());
    prev = c.getParent();
    if (c instanceof JPanel) return;

    Point parentLocation = c.getParent().g etLocation();
    xAdjustment = parentLocation. x - e.getX();
    yAdjustment = parentLocation. y - e.getY();
    chessPiece = (JLabel)c;
    chessPiece.setL ocation(e.getX( ) + xAdjustment, e.getY() + yAdjustment);
    chessPiece.setS ize(chessPiece. getWidth(), chessPiece.getH eight());
    layeredPane.add (chessPiece, JLayeredPane.DR AG_LAYER);
    }


    public void mouseDragged(Mo useEvent me)
    {
    if (chessPiece == null) return;

    chessPiece.setL ocation(me.getX () + xAdjustment, me.getY() + yAdjustment);
    }


    public void mouseReleased(M ouseEvent e)
    {

    try
    {
    if (chessPiece == null) return;

    chessPiece.setV isible(false);
    Component c = chessBoard.find ComponentAt(e.g etX(), e.getY());

    if (c instanceof JLabel)
    {
    Container parent = (Container)prev ;
    parent.add( chessPiece );
    }
    else
    {
    Container parent = (Container)c;
    parent.add( chessPiece );
    }

    chessPiece.setV isible(true);

    }catch(NullPoin terException exception)
    {
    Container parent = (Container)prev ;
    parent.add( chessPiece );
    chessPiece.setV isible(true);
    }
    }

    public void mouseClicked(Mo useEvent e) {}
    public void mouseMoved(Mous eEvent e) {}
    public void mouseEntered(Mo useEvent e) {}
    public void mouseExited(Mou seEvent e) {}



    public static void main(String[] args)
    {
    JFrame frame = new ChessBoard();
    frame.setDefaul tCloseOperation ( DISPOSE_ON_CLOS E );
    frame.pack();
    frame.setResiza ble( false );
    frame.setLocati onRelativeTo( null );
    frame.setVisibl e(true);
    }
    }[/CODE]
    Last edited by Ganon11; Aug 29 '07, 12:45 PM. Reason: Please use the [CODE] tags provided
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by needhelp20
    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:

    [CODE=java]
    ...
    // Add a few pieces to the board but I don't know is it ok? maybe it should be in some loop?

    JLabel piece = new JLabel( new ImageIcon("pw.p ng") );
    JPanel panel = (JPanel)chessBo ard.getComponen t( 8 );
    panel.add( piece );
    ...
    }
    ...
    [/CODE]
    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:
    [CODE=java]
    public interface Figure {

    // Every figure has to be moved - but not to an illegal Field
    public void move(int x, int y) throws IllegalFieldExc eption;

    // You have to be able to find out, where a figure is.
    public int xPosition();
    public int yPosition();

    // Of course, you have to know, if a figure is still on the field
    public boolean captured();

    // And you have to be able to capture figures
    public void capture();
    }
    }
    [/CODE]
    From this, you implement the various figures. When done, you can add them to your Field:
    [CODE=java]
    HashMap[] figure = new Figure[16];
    for(int i=0;i<8;i++)
    {
    Figures.add("Pa wn" + i;new Pawn(i,1));
    }
    // Add the other figures
    ...
    [/CODE]And continue from that.
    The rules about how you can move a figure (e.g. a Pawn) should be programmed in the "move" method.
    Last edited by Nepomuk; Aug 29 '07, 01:46 PM. Reason: corrected a little error in the interface

    Comment

    • needhelp20
      New Member
      • Aug 2007
      • 3

      #3
      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.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        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?

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by needhelp20
          * forbid illegal moves,
          The "IllegalFieldEx ception" (you can call it "IllegalMoveExc eption" of course) can take care of most of that. You just have to catch it and react.
          Originally posted by needhelp20
          * 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.
          Originally posted by needhelp20
          * enable storing/loading a game to/from a file,
          Read this article about reading and writing files.

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by nepomuk
            The "IllegalFieldEx ception" (you can call it "IllegalMoveExc eption" 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.

            Comment

            • needhelp20
              New Member
              • Aug 2007
              • 3

              #7
              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...


              Code:
              import java.awt.*;
              import java.awt.event.*;
              import javax.swing.*;
              import java.io.*;
              import java.util.*;
              import java.util.HashMap;
               
              public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener
              {
              	JLayeredPane layeredPane;
              	JPanel chessBoard;
              	JLabel chessPiece;
              	int xAdjustment;
              	int yAdjustment;
               	private JFileChooser chooser;
               	Component prev;
               	
                  private static HashMap WhiteFigures = new HashMap();
                  private static HashMap BlackFigures = new HashMap();
               
               
              	public ChessBoard()
              	{
              		
              		
              		setTitle("Chessboard by Radek");
              		
              		chooser = new JFileChooser();
              		chooser.setCurrentDirectory(new File("."));
              
              		JMenuBar menuBar = new JMenuBar();
              		setJMenuBar(menuBar);
              				
              		JMenu menu = new JMenu("File");
              		menuBar.add(menu);
              		
              		JMenu menu1 = new JMenu("Edit");
              		menuBar.add(menu1);
              		
              
              		JMenuItem openItem = new JMenuItem("Open");
              		menu.add(openItem);
              		openItem.addActionListener(new ActionListener()
              		{
              			public void actionPerformed(ActionEvent evt)
              			{
              				int r = chooser.showOpenDialog(null);
              				if(r == JFileChooser.APPROVE_OPTION)
              				{
              					String name = chooser.getSelectedFile().getPath();
              				}
              			}
              		});
              		
              		JMenuItem saveItem = new JMenuItem("Save");
              		menu.add(saveItem);
              		
              		JMenuItem exitItem = new JMenuItem("Exit");
              		menu.add(exitItem);
              		exitItem.addActionListener(new ActionListener()
              		{
              			public void actionPerformed(ActionEvent event){
              				System.exit(0);
              			}
              		});
              		
              		JMenuItem zoomItem = new JMenuItem("size +");
              		menu1.add(zoomItem);
              		zoomItem.addActionListener(new ActionListener(){
              			public void actionPerformed(ActionEvent evt){
              				setSize (300, 300);
              			}
              		});
              		
              		JMenuItem zoomOutItem = new JMenuItem("size -");
              		menu1.add(zoomOutItem);
              		zoomOutItem.addActionListener(new ActionListener(){
              			public void actionPerformed(ActionEvent evt){
              				setSize (630, 670);
              			}
              		});
              		
              		Dimension Size = new Dimension(600, 600);
               
              		//  Use a Layered Pane for this application
               
              		layeredPane = new JLayeredPane();
              		getContentPane().add(layeredPane);
              		layeredPane.setPreferredSize( Size );
              		layeredPane.addMouseListener( this );
              		layeredPane.addMouseMotionListener( this );
               
              		//  Add a chess board to the Layered Pane
               
              		chessBoard = new JPanel();
              		layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
              		chessBoard.setLayout( new GridLayout(8, 8) );
              		chessBoard.setPreferredSize( Size );
              		chessBoard.setBounds(2, 2, Size.width, Size.height);
              		
              		
               
              		for (int i = 0; i < 64; i++)
              		{
              			JPanel square = new JPanel( new BorderLayout() );
              			chessBoard.add( square );
               
              			int row = (i / 8) % 2;
              			if (row == 0)
              				square.setBackground( i % 2 == 0 ? Color.black : Color.white );
              			else
              				square.setBackground( i % 2 == 0 ? Color.white : Color.black );
              		}
               
              		Figure[] whiteFigure = new Figure[16];
              		
              
              		// Add Pawns
                      for(int i=0;i<8;i++)
                      {
                          whiteFigure[i] = new Pawn(1,i);
                          WhiteFigures.put("pw.png" + i, whiteFigure[i]);
                      }
              		//add king
              		whiteFigure[12] = new King(0,4);
                      WhiteFigures.put("kgw.png", whiteFigure[12]);
              
              	/*	JLabel piece = new JLabel( new ImageIcon("pw.png") );
              		JPanel panel = (JPanel)chessBoard.getComponent( 8 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pw.png") );
              		panel = (JPanel)chessBoard.getComponent( 9 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pw.png") );
              		panel = (JPanel)chessBoard.getComponent( 10 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pw.png") );
              		panel = (JPanel)chessBoard.getComponent( 11 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pw.png") );
              		panel = (JPanel)chessBoard.getComponent( 12 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pw.png") );
              		panel = (JPanel)chessBoard.getComponent( 13 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pw.png") );
              		panel = (JPanel)chessBoard.getComponent( 14 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pw.png") );
              		panel = (JPanel)chessBoard.getComponent( 15 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("ww.png") );
              		panel = (JPanel)chessBoard.getComponent( 0 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("ww.png") );
              		panel = (JPanel)chessBoard.getComponent( 7 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("hw.png") );
              		panel = (JPanel)chessBoard.getComponent( 1 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("hw.png") );
              		panel = (JPanel)chessBoard.getComponent( 6 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("bw.png") );
              		panel = (JPanel)chessBoard.getComponent( 2 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("bw.png") );
              		panel = (JPanel)chessBoard.getComponent( 5 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("qw.png") );
              		panel = (JPanel)chessBoard.getComponent( 3 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("kgw.png") );
              		panel = (JPanel)chessBoard.getComponent( 4 );
              		panel.add( piece );
              		
              		//black
              		
              		piece = new JLabel( new ImageIcon("pc.png") );
              		panel = (JPanel)chessBoard.getComponent( 48 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pc.png") );
              		panel = (JPanel)chessBoard.getComponent( 49 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pc.png") );
              		panel = (JPanel)chessBoard.getComponent( 50 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pc.png") );
              		panel = (JPanel)chessBoard.getComponent( 51 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pc.png") );
              		panel = (JPanel)chessBoard.getComponent( 52 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pc.png") );
              		panel = (JPanel)chessBoard.getComponent( 53 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pc.png") );
              		panel = (JPanel)chessBoard.getComponent( 54 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("pc.png") );
              		panel = (JPanel)chessBoard.getComponent( 55 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("wc.png") );
              		panel = (JPanel)chessBoard.getComponent( 56 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("wc.png") );
              		panel = (JPanel)chessBoard.getComponent( 63 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("hc.png") );
              		panel = (JPanel)chessBoard.getComponent( 57 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("hc.png") );
              		panel = (JPanel)chessBoard.getComponent( 62 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("bc.png") );
              		panel = (JPanel)chessBoard.getComponent( 58 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("bc.png") );
              		panel = (JPanel)chessBoard.getComponent( 61 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("qc.png") );
              		panel = (JPanel)chessBoard.getComponent( 59 );
              		panel.add( piece );
              		piece = new JLabel( new ImageIcon("kgc.png") );
              		panel = (JPanel)chessBoard.getComponent( 60 );
              		panel.add( piece );
              */
              		
              	}
              	
              
              	public void mousePressed(MouseEvent e)
              	{
              		chessPiece = null;
              		Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
               		prev = c.getParent();
              		if (c instanceof JPanel) return;
               
              		Point parentLocation = c.getParent().getLocation();
              		xAdjustment = parentLocation.x - e.getX();
              		yAdjustment = parentLocation.y - e.getY();
              		chessPiece = (JLabel)c;
              		chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
              		chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
              		layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
              	}
               
               
              	public void mouseDragged(MouseEvent me)
              	{
              		if (chessPiece == null) return;
               
              		chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
              	 }
               
               
              	public void mouseReleased(MouseEvent e)
              	{
              		
              try
              	{
              		if (chessPiece == null) return;
               
              		chessPiece.setVisible(false);
              		Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
               
              		if (c instanceof JLabel)
              		{
                 Container parent = (Container)prev;
                 parent.add( chessPiece );
              		}
              		else
              		{
              			Container parent = (Container)c;
              			parent.add( chessPiece );
              		}
               
              		chessPiece.setVisible(true);
              
              	}catch(NullPointerException exception)
              	{
              		Container parent = (Container)prev;
              		parent.add( chessPiece );
              		chessPiece.setVisible(true);
              	}
              	}
               
              	public void mouseClicked(MouseEvent e) {}
              	public void mouseMoved(MouseEvent e) {}
              	public void mouseEntered(MouseEvent e) {}
              	public void mouseExited(MouseEvent e) {}
               
               
               
              	public static void main(String[] args)
              	{
              		JFrame frame = new ChessBoard();
              		frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
              		frame.pack();
              		frame.setResizable( false );
              		frame.setLocationRelativeTo( null );
              		frame.setVisible(true);
              		
              
                      
                      
                      }
              	 }

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                Originally posted by needhelp20
                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...

                Code:
                 
                		Figure[] whiteFigure = new Figure[16];
                
                		// Add Pawns
                        for(int i=0;i<8;i++)
                        {
                            whiteFigure[i] = new Pawn(1,i);
                            WhiteFigures.put("pw.png" + i, whiteFigure[i]);
                        }
                		//add king
                		whiteFigure[12] = new King(0,4);
                        WhiteFigures.put("kgw.png", whiteFigure[12]);
                
                	/*	JLabel piece = new JLabel( new ImageIcon("pw.png") );
                		JPanel panel = (JPanel)chessBoard.getComponent( 8 );
                		panel.add( piece );
                		...
                		
                		//black
                		
                		piece = new JLabel( new ImageIcon("pc.png") );
                		panel = (JPanel)chessBoard.getComponent( 48 );
                		panel.add( piece );
                		...
                */
                	}
                	
                ...
                 
                	public static void main(String[] args)
                	{
                		JFrame frame = new ChessBoard();
                		frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
                		frame.pack();
                		frame.setResizable( false );
                		frame.setLocationRelativeTo( null );
                		frame.setVisible(true);
                        }
                }
                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[CODE=java]WhiteFigures.pu t("pw.png" + i, whiteFigure[i]);[/CODE]only means, that you can call your (virtual) Figure by using[CODE=java]WhiteFigures.ge t("pw.png1"); // If i == 1[/CODE]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.[CODE=java]JLabel [][] piece = new JLabel[8][8]; // This will give you an 8x8 Array of JLabels[/CODE]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.

                Comment

                Working...