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]
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]
Comment