am having a problem with the following code for chess ,it doesnt place the pieces or darts on the board to be visible,so please help me what is the problem with it?
am having problem with the following chess code,it doesnt place the darts or pieces to be visible on the board(chess field),can u please check and send me details in my email nothandomgazi@ymail.com
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newchessgame;
/**
*
* @author Precious Magazini
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class NewChessGame extends JFrame implements MouseListener, MouseMotionListener {
JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
public NewChessGame(){
Dimension boardSize = new Dimension(600, 600);
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
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( boardSize );
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
Color Green = new Color(119,170,119);
Color Cream = new Color(250,235,215);
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 ? Green : Cream );
else
square.setBackground( i % 2 == 0 ? Cream : Green );
}
/** Pieces for the board */
// Image String array - TOP ROWS
String[] ImageArray_TOP = new String[16];
ImageArray_TOP[0] = "src//Pieces//top//wQueen.jpg";
ImageArray_TOP[1] = "src//pieces//top//1bknight.gif";
ImageArray_TOP[2] = "src//pieces//top//1bbishop.gif";
ImageArray_TOP[3] = "src//pieces//top//1bqueen.gif";
ImageArray_TOP[4] = "src//pieces//top//1bking.gif";
ImageArray_TOP[5] = "src//pieces//top//1bbishop.gif";
ImageArray_TOP[6] = "src//pieces//top//1bknight.gif";
ImageArray_TOP[7] = "src//pieces//top//1brook.gif";
ImageArray_TOP[8] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[9] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[10] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[11] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[12] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[13] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[14] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[15] = "src//pieces//top//1bpawn.gif";
// Image String Array - BOTTOM ROWS
String[] ImageArray_BOTTOM = new String[16];
ImageArray_BOTTOM[0] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[1] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[2] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[3] = "src//pieces///bottom///rpawn.gif";
ImageArray_BOTTOM[4] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[5] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[6] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[7] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[8] = "src//pieces///bottm//rrook.gif";
ImageArray_BOTTOM[9] = "src//pieces//bottom//rknight.gif";
ImageArray_BOTTOM[10] = "src//pieces//bottom//rbishop.gif";
ImageArray_BOTTOM[11] = "src//pieces//bottom//rqueen.gif";
ImageArray_BOTTOM[12] = "src//pieces//bottom//rking.gif";
ImageArray_BOTTOM[13] = "src//pieces//bottom//rbishop.gif";
ImageArray_BOTTOM[14] = "src//pieces//bottom//rknight.gif";
ImageArray_BOTTOM[15] = "src//pieces//bottom//rrook.gif";
// Add pieces to the board (TOP)
for( int i = 0; i < 16; i++) {
JLabel piece = new JLabel( new ImageIcon(ImageArray_TOP[i]) );
JPanel panel = (JPanel)chessBoard.getComponent(i);
panel.add(piece);
}
// Add pieces to the board (BOTTOM)
int inc = 48;
for( int j = 0; j < 16; j++, inc++) {
JLabel piece = new JLabel( new ImageIcon(ImageArray_BOTTOM[j]) );
JPanel panel = (JPanel)chessBoard.getComponent(inc);
panel.add(piece);
}
}
public void mousePressed(MouseEvent e){
chessPiece = null;
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
int X = e.getX();
int Y = e.getY();
System.out.println(X + Y);
Pieces.workoutloc(X, Y);
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);
}
//Move the chess piece around
public void mouseDragged(MouseEvent me) {
if (chessPiece == null) return;
chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
}
//Drop the chess piece back onto the chess board
public void mouseReleased(MouseEvent e) {
if(chessPiece == null) return;
chessPiece.setVisible(false);
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JLabel){
Container parent = c.getParent();
parent.remove(0);
parent.add( chessPiece );
}
else {
Container parent = (Container)c;
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 NewChessGame();
JMenuBar MenuBar = new JMenuBar();
JMenu File = new JMenu("File" );
JMenu Help = new JMenu( "Help" );
MenuBar.add( File );
MenuBar.add( Help );
JMenuItem newGame = new JMenuItem( "New Game" );
JMenuItem exit = new JMenuItem( "Exit" );
File.add( newGame );
File.add( exit );
JMenuItem HelpInfo = new JMenuItem( "How to play chess" );
JMenuItem About = new JMenuItem( "About" );
Help.add( HelpInfo );
Help.add( About );
// ActionListeners for items
// EXIT PROGRAM
ActionListener ExitGame = new ActionListener() {
public void actionPerformed( ActionEvent actionEvent ) {
System.exit(0);
}
};
// NEW GAME
ActionListener newGameAction = new ActionListener() {
public void actionPerformed( ActionEvent actionEvent ) {
// SOMETHING
}
};
// HOW TO PLAY
ActionListener HowToPlay = new ActionListener() {
public void actionPerformed( ActionEvent actionEvent ) {
String url = "http://www.chess.com/learn-how-to-play-chess.html";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
} catch (IOException ex) {
System.err.println(); }
}
};
// ADD LISTENERS
exit.addActionListener( ExitGame );
newGame.addActionListener( newGameAction );
HelpInfo.addActionListener( HowToPlay );
frame.setJMenuBar( MenuBar );
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
frame.setSize(600, 645);
}
}
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package newchessgame; /** * * @author Precious Magazini */ import java.io.*; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class NewChessGame extends JFrame implements MouseListener, MouseMotionListener { JLayeredPane layeredPane; JPanel chessBoard; JLabel chessPiece; int xAdjustment; int yAdjustment; public NewChessGame(){ Dimension boardSize = new Dimension(600, 600); // Use a Layered Pane for this this application layeredPane = new JLayeredPane(); getContentPane().add(layeredPane); layeredPane.setPreferredSize(boardSize); 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( boardSize ); chessBoard.setBounds(0, 0, boardSize.width, boardSize.height); Color Green = new Color(119,170,119); Color Cream = new Color(250,235,215); 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 ? Green : Cream ); else square.setBackground( i % 2 == 0 ? Cream : Green ); } /** Pieces for the board */ // Image String array - TOP ROWS String[] ImageArray_TOP = new String[16]; ImageArray_TOP[0] = "src//Pieces//top//wQueen.jpg"; ImageArray_TOP[1] = "src//pieces//top//1bknight.gif"; ImageArray_TOP[2] = "src//pieces//top//1bbishop.gif"; ImageArray_TOP[3] = "src//pieces//top//1bqueen.gif"; ImageArray_TOP[4] = "src//pieces//top//1bking.gif"; ImageArray_TOP[5] = "src//pieces//top//1bbishop.gif"; ImageArray_TOP[6] = "src//pieces//top//1bknight.gif"; ImageArray_TOP[7] = "src//pieces//top//1brook.gif"; ImageArray_TOP[8] = "src//pieces//top//1bpawn.gif"; ImageArray_TOP[9] = "src//pieces//top//1bpawn.gif"; ImageArray_TOP[10] = "src//pieces//top//1bpawn.gif"; ImageArray_TOP[11] = "src//pieces//top//1bpawn.gif"; ImageArray_TOP[12] = "src//pieces//top//1bpawn.gif"; ImageArray_TOP[13] = "src//pieces//top//1bpawn.gif"; ImageArray_TOP[14] = "src//pieces//top//1bpawn.gif"; ImageArray_TOP[15] = "src//pieces//top//1bpawn.gif"; // Image String Array - BOTTOM ROWS String[] ImageArray_BOTTOM = new String[16]; ImageArray_BOTTOM[0] = "src//pieces//bottom//rpawn.gif"; ImageArray_BOTTOM[1] = "src//pieces//bottom//rpawn.gif"; ImageArray_BOTTOM[2] = "src//pieces//bottom//rpawn.gif"; ImageArray_BOTTOM[3] = "src//pieces///bottom///rpawn.gif"; ImageArray_BOTTOM[4] = "src//pieces//bottom//rpawn.gif"; ImageArray_BOTTOM[5] = "src//pieces//bottom//rpawn.gif"; ImageArray_BOTTOM[6] = "src//pieces//bottom//rpawn.gif"; ImageArray_BOTTOM[7] = "src//pieces//bottom//rpawn.gif"; ImageArray_BOTTOM[8] = "src//pieces///bottm//rrook.gif"; ImageArray_BOTTOM[9] = "src//pieces//bottom//rknight.gif"; ImageArray_BOTTOM[10] = "src//pieces//bottom//rbishop.gif"; ImageArray_BOTTOM[11] = "src//pieces//bottom//rqueen.gif"; ImageArray_BOTTOM[12] = "src//pieces//bottom//rking.gif"; ImageArray_BOTTOM[13] = "src//pieces//bottom//rbishop.gif"; ImageArray_BOTTOM[14] = "src//pieces//bottom//rknight.gif"; ImageArray_BOTTOM[15] = "src//pieces//bottom//rrook.gif"; // Add pieces to the board (TOP) for( int i = 0; i < 16; i++) { JLabel piece = new JLabel( new ImageIcon(ImageArray_TOP[i]) ); JPanel panel = (JPanel)chessBoard.getComponent(i); panel.add(piece); } // Add pieces to the board (BOTTOM) int inc = 48; for( int j = 0; j < 16; j++, inc++) { JLabel piece = new JLabel( new ImageIcon(ImageArray_BOTTOM[j]) ); JPanel panel = (JPanel)chessBoard.getComponent(inc); panel.add(piece); } } public void mousePressed(MouseEvent e){ chessPiece = null; Component c = chessBoard.findComponentAt(e.getX(), e.getY()); int X = e.getX(); int Y = e.getY(); System.out.println(X + Y); Pieces.workoutloc(X, Y); 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); } //Move the chess piece around public void mouseDragged(MouseEvent me) { if (chessPiece == null) return; chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment); } //Drop the chess piece back onto the chess board public void mouseReleased(MouseEvent e) { if(chessPiece == null) return; chessPiece.setVisible(false); Component c = chessBoard.findComponentAt(e.getX(), e.getY()); if (c instanceof JLabel){ Container parent = c.getParent(); parent.remove(0); parent.add( chessPiece ); } else { Container parent = (Container)c; 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 NewChessGame(); JMenuBar MenuBar = new JMenuBar(); JMenu File = new JMenu("File" ); JMenu Help = new JMenu( "Help" ); MenuBar.add( File ); MenuBar.add( Help ); JMenuItem newGame = new JMenuItem( "New Game" ); JMenuItem exit = new JMenuItem( "Exit" ); File.add( newGame ); File.add( exit ); JMenuItem HelpInfo = new JMenuItem( "How to play chess" ); JMenuItem About = new JMenuItem( "About" ); Help.add( HelpInfo ); Help.add( About ); // ActionListeners for items // EXIT PROGRAM ActionListener ExitGame = new ActionListener() { public void actionPerformed( ActionEvent actionEvent ) { System.exit(0); } }; // NEW GAME ActionListener newGameAction = new ActionListener() { public void actionPerformed( ActionEvent actionEvent ) { // SOMETHING } }; // HOW TO PLAY ActionListener HowToPlay = new ActionListener() { public void actionPerformed( ActionEvent actionEvent ) { String url = "http://www.chess.com/learn-how-to-play-chess.html"; try { java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); } catch (IOException ex) { System.err.println(); } } }; // ADD LISTENERS exit.addActionListener( ExitGame ); newGame.addActionListener( newGameAction ); HelpInfo.addActionListener( HowToPlay ); frame.setJMenuBar( MenuBar ); frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE ); frame.pack(); frame.setResizable(false); frame.setLocationRelativeTo( null ); frame.setVisible(true); frame.setSize(600, 645); } }
am having problem with the following chess code,it doesnt place the darts or pieces to be visible on the board(chess field),can u please check and send me details in my email nothandomgazi@ymail.com
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newchessgame;
/**
*
* @author Precious Magazini
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class NewChessGame extends JFrame implements MouseListener, MouseMotionListener {
JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
public NewChessGame(){
Dimension boardSize = new Dimension(600, 600);
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
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( boardSize );
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
Color Green = new Color(119,170,119);
Color Cream = new Color(250,235,215);
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 ? Green : Cream );
else
square.setBackground( i % 2 == 0 ? Cream : Green );
}
/** Pieces for the board */
// Image String array - TOP ROWS
String[] ImageArray_TOP = new String[16];
ImageArray_TOP[0] = "src//Pieces//top//wQueen.jpg";
ImageArray_TOP[1] = "src//pieces//top//1bknight.gif";
ImageArray_TOP[2] = "src//pieces//top//1bbishop.gif";
ImageArray_TOP[3] = "src//pieces//top//1bqueen.gif";
ImageArray_TOP[4] = "src//pieces//top//1bking.gif";
ImageArray_TOP[5] = "src//pieces//top//1bbishop.gif";
ImageArray_TOP[6] = "src//pieces//top//1bknight.gif";
ImageArray_TOP[7] = "src//pieces//top//1brook.gif";
ImageArray_TOP[8] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[9] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[10] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[11] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[12] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[13] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[14] = "src//pieces//top//1bpawn.gif";
ImageArray_TOP[15] = "src//pieces//top//1bpawn.gif";
// Image String Array - BOTTOM ROWS
String[] ImageArray_BOTTOM = new String[16];
ImageArray_BOTTOM[0] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[1] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[2] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[3] = "src//pieces///bottom///rpawn.gif";
ImageArray_BOTTOM[4] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[5] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[6] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[7] = "src//pieces//bottom//rpawn.gif";
ImageArray_BOTTOM[8] = "src//pieces///bottm//rrook.gif";
ImageArray_BOTTOM[9] = "src//pieces//bottom//rknight.gif";
ImageArray_BOTTOM[10] = "src//pieces//bottom//rbishop.gif";
ImageArray_BOTTOM[11] = "src//pieces//bottom//rqueen.gif";
ImageArray_BOTTOM[12] = "src//pieces//bottom//rking.gif";
ImageArray_BOTTOM[13] = "src//pieces//bottom//rbishop.gif";
ImageArray_BOTTOM[14] = "src//pieces//bottom//rknight.gif";
ImageArray_BOTTOM[15] = "src//pieces//bottom//rrook.gif";
// Add pieces to the board (TOP)
for( int i = 0; i < 16; i++) {
JLabel piece = new JLabel( new ImageIcon(ImageArray_TOP[i]) );
JPanel panel = (JPanel)chessBoard.getComponent(i);
panel.add(piece);
}
// Add pieces to the board (BOTTOM)
int inc = 48;
for( int j = 0; j < 16; j++, inc++) {
JLabel piece = new JLabel( new ImageIcon(ImageArray_BOTTOM[j]) );
JPanel panel = (JPanel)chessBoard.getComponent(inc);
panel.add(piece);
}
}
public void mousePressed(MouseEvent e){
chessPiece = null;
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
int X = e.getX();
int Y = e.getY();
System.out.println(X + Y);
Pieces.workoutloc(X, Y);
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);
}
//Move the chess piece around
public void mouseDragged(MouseEvent me) {
if (chessPiece == null) return;
chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
}
//Drop the chess piece back onto the chess board
public void mouseReleased(MouseEvent e) {
if(chessPiece == null) return;
chessPiece.setVisible(false);
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JLabel){
Container parent = c.getParent();
parent.remove(0);
parent.add( chessPiece );
}
else {
Container parent = (Container)c;
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 NewChessGame();
JMenuBar MenuBar = new JMenuBar();
JMenu File = new JMenu("File" );
JMenu Help = new JMenu( "Help" );
MenuBar.add( File );
MenuBar.add( Help );
JMenuItem newGame = new JMenuItem( "New Game" );
JMenuItem exit = new JMenuItem( "Exit" );
File.add( newGame );
File.add( exit );
JMenuItem HelpInfo = new JMenuItem( "How to play chess" );
JMenuItem About = new JMenuItem( "About" );
Help.add( HelpInfo );
Help.add( About );
// ActionListeners for items
// EXIT PROGRAM
ActionListener ExitGame = new ActionListener() {
public void actionPerformed( ActionEvent actionEvent ) {
System.exit(0);
}
};
// NEW GAME
ActionListener newGameAction = new ActionListener() {
public void actionPerformed( ActionEvent actionEvent ) {
// SOMETHING
}
};
// HOW TO PLAY
ActionListener HowToPlay = new ActionListener() {
public void actionPerformed( ActionEvent actionEvent ) {
String url = "http://www.chess.com/learn-how-to-play-chess.html";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
} catch (IOException ex) {
System.err.println(); }
}
};
// ADD LISTENERS
exit.addActionListener( ExitGame );
newGame.addActionListener( newGameAction );
HelpInfo.addActionListener( HowToPlay );
frame.setJMenuBar( MenuBar );
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
frame.setSize(600, 645);
}
}