2 Replies - 270 Views - Last Post: 02 March 2013 - 09:13 PM Rate Topic: -----

#1 Precious Magazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-February 13

chess code

Posted 02 March 2013 - 02:24 AM

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?
/*
 * 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);
}
}

This post has been edited by modi123_1: 02 March 2013 - 02:22 AM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: chess code

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: chess code

Posted 02 March 2013 - 04:36 AM

Are you getting any error messages? If so, post them.

What is "Pieces" at line 104?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: chess code

Posted 02 March 2013 - 09:13 PM

Directories are separated by a single / not 2

src//pieces//top//1bbishop.gif"
should be
src/pieces/top/1bbishop.gif"

True both on Windows or Unix

This post has been edited by pbl: 02 March 2013 - 09:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1