home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
inprise
/
JSAMPLES.Z
/
Chessboard.java
< prev
next >
Wrap
Text File
|
1998-05-08
|
35KB
|
1,061 lines
/*
* Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
*
* This SOURCE CODE FILE, which has been provided by Borland as part
* of a Borland product for use ONLY by licensed users of the product,
* includes CONFIDENTIAL and PROPRIETARY information of Borland.
*
* USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
* OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
* THE PRODUCT.
*
* IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
* COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
* OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
* OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
* OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
* OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
* CODE FILE.
*/
//
// Chessboard.java
//
package borland.samples.apps.chess.client.board;
import java.awt.event.*;
import java.net.URL;
import java.awt.*;
import java.awt.image.*;
import java.util.ResourceBundle;
public class Chessboard extends Canvas implements MouseMotionListener, MouseListener, Runnable
{
ResourceBundle res = ResourceBundle.getBundle("borland.samples.apps.chess.client.board.Res");
boolean animate = true;
boolean badTimeToStopThread = false;
boolean mouseActive = false;
boolean blackOnTop = true;
boolean blackGoesOnTop = false;
boolean retryImage = true;
boolean isLive ; //Chessboard enabled state
boolean imageLoadError = false;
boolean init = false ;
boolean initfailed = false;
private Color darkSquareColor;
private Color lightSquareColor;
ActionListener listener;
Image iBoard;
Image iBackground;
Image promoteWhite;
Image promoteBlack;
Image iAnimationBackground;
Image iPiece[] ;
Image iPiecemask[];
FilteredImageSource masksource[];
int offset[] ;
int endrank;
int startrank;
int endfile;
int startfile;
Thread timer = null;
Boardsquares squareColor;
Boardsquares prevpos;
Boardsquares pieceColor;
Boardsquares updateArray;
public static final int LIGHTSQUARE = 0;
public static final int DARKSQUARE = 16;
public static final int BLACKPIECE = 8;
public static final int WHITEPIECE = 0;
public static final int PAWN = 1;
public static final int KNIGHT = 2;
public static final int BISHOP = 3;
public static final int ROOK = 4;
public static final int QUEEN = 5;
public static final int KING = 6;
static final String [] file = {"a","b","c","d","e","f","g","h"};
String initstring = res.getString("Loading_Chessboard");
String message = "Initialization failed";
int xoffset;
int yoffset;
MediaTracker tracker;
int p;
int xincrement;
int yincrement;
int xOldMouse;
int yOldMouse;
int pointbx = 0;
int pointby = 0;
int finalPiece;
MoveTuple tuple;
Font boldfont;
long currentTime = 0;
private URL boardURL = null;
private String boardPath = "" ;
public Chessboard () {
try {
addMouseMotionListener(this);
addMouseListener(this);
darkSquareColor = Color.blue;
lightSquareColor = Color.lightGray;
boldfont = new Font("Dialog",Font.BOLD,10) ;
squareColor = new Boardsquares();
prevpos = new Boardsquares();
pieceColor = new Boardsquares();
updateArray = new Boardsquares();
int i;
int j;
offset = new int[8];
offset[1] = 35;
iPiece = new Image[32];
masksource = new FilteredImageSource[16] ;
iPiecemask = new Image[16];
for(j=0;j<8;j=j+2) {
for(i=0;i<8;i=i+2) {
squareColor.assign(j+1,i+1,DARKSQUARE);
squareColor.assign(j+1,i,LIGHTSQUARE);
squareColor.assign(j,i,DARKSQUARE);
squareColor.assign(j,i+1,LIGHTSQUARE);
}
}
for(j=0;j<8;j++) {
prevpos.assign(j,1,PAWN);
prevpos.assign(j,6,PAWN);
for(i=0;i<2;i++) {
pieceColor.assign(j,i,WHITEPIECE);
pieceColor.assign(j,i+6,BLACKPIECE);
}
}
prevpos.assign(0,0,ROOK);
prevpos.assign(1,0,KNIGHT);
prevpos.assign(2,0,BISHOP);
prevpos.assign(3,0,QUEEN);
prevpos.assign(4,0,KING);
prevpos.assign(5,0,BISHOP);
prevpos.assign(6,0,KNIGHT);
prevpos.assign(7,0,ROOK);
prevpos.assign(0,7,ROOK);
prevpos.assign(1,7,KNIGHT);
prevpos.assign(2,7,BISHOP);
prevpos.assign(3,7,QUEEN);
prevpos.assign(4,7,KING);
prevpos.assign(5,7,BISHOP);
prevpos.assign(6,7,KNIGHT);
prevpos.assign(7,7,ROOK);
}
catch (Exception e) {
message = e.toString() ;
e.printStackTrace();
initfailed = true;
}
}
public Boardsquares getPiecePosition() {
return prevpos;
}
public Boardsquares getColorPosition() {
return pieceColor;
}
// java.awt.Component method
public Dimension getPreferredSize() {
if (offset[1] > 12) {
System.err.println("Board preferred Size =(" + offset[1] + "*8)+12");
return new Dimension((offset[1] * 8) + 12, (offset[1] * 8) + 12);
}
else
return new Dimension(292, 292);
}
public void setImageName(String boardPath) {
this.boardPath = boardPath;
try {
setImageURL(new URL(boardPath));
}
catch (Exception e) {
System.err.println("setImagePath " + e);
}
}
public String getImageName() {
return boardPath;
}
public void setImageURL(URL boardURL) {
this.boardURL = boardURL;
setImage(getToolkit().getImage(boardURL));
}
public URL getImageURL() {
return boardURL;
}
public void addNotify() {
//we have been added to a living, breathing window! if our image has already been set
//now we can actually take that image and chop it up into little pieces to make our pieces
super.addNotify();
if (retryImage) {
retryImage = false;
setImage(iBoard);
}
}
public Image getImage() {
return iBoard;
}
public synchronized void setImage(Image iPieces){
try{
if (iPieces == null) {
System.err.println("setImage - image is null");
return;
}
//need a peer to manipulate images, so remember what to do and wait patiently for addNotify() to happen
if (retryImage) {
iBoard = iPieces;
return;
}
//init = false;
System.err.println("Chessboard.setImage...");
tracker = new MediaTracker(this);
tracker.addImage(iPieces,0);
tracker.waitForAll();
//System.out.println("done waiting for image...");
offset[0] = 0;
int squareSize = iPieces.getWidth(this) / 6;
//System.out.println("Square width=" + offset[1]);
if (squareSize < 10) {
System.out.println("This image is too small.");
return;
}
offset[1] = squareSize;
int i;
for (i=2;i < 8;i++) {
offset[i] = offset[1] * i;
}
initstring = CVS.LOADING_PIECES_;
ImageFilter tfilter = new Transparent();
MediaTracker trackr ;
trackr = new MediaTracker(this);
FilteredImageSource mask = new FilteredImageSource(iPieces.getSource(),
tfilter);
iPieces = createImage(mask);
trackr.addImage(iPieces,0);
try {
initstring = CVS.LOADING_BLACK_PIECE;
trackr.waitForID(0);
initstring = CVS.LOADING_WHITE_PIECE;
//System.out.println("waitForAll did not crash... " );
}
catch (Exception e){
System.out.println("waitForAll failed ");
e.printStackTrace();
}
for (i=0;i<32;i++) {
iPiece[i] = createImage(offset[1],offset[1]);
}
for (i=0;i<16;i++) {
iPiecemask[i] = createImage(offset[1],offset[1]);
}
//System.out.println("create maskimages ");
Graphics gPiece;
//pieces
//pawns
CropImageFilter crop = new CropImageFilter(offset[5],offset[0],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[PAWN+WHITEPIECE] = createImage(mask);
trackr.addImage(iPiecemask[PAWN+WHITEPIECE],1);
crop = new CropImageFilter(offset[5],offset[1],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[PAWN+BLACKPIECE] = createImage(mask);
trackr.addImage(iPiecemask[PAWN+BLACKPIECE],1);
//knights
crop = new CropImageFilter(offset[1],offset[0],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[KNIGHT+WHITEPIECE] = createImage(mask);
trackr.addImage(iPiecemask[KNIGHT+WHITEPIECE],1);
crop = new CropImageFilter(offset[1],offset[1],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[KNIGHT+BLACKPIECE] = createImage(mask);
trackr.addImage(iPiecemask[KNIGHT+BLACKPIECE],1);
//bishops
crop = new CropImageFilter(offset[2],offset[0],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[BISHOP+WHITEPIECE] = createImage(mask);
trackr.addImage(iPiecemask[BISHOP+WHITEPIECE],1);
crop = new CropImageFilter(offset[2],offset[1],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[BISHOP+BLACKPIECE] = createImage(mask);
trackr.addImage(iPiecemask[BISHOP+BLACKPIECE],1);
//rooks
crop = new CropImageFilter(offset[0],offset[0],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[ROOK+WHITEPIECE] = createImage(mask);
trackr.addImage(iPiecemask[ROOK+WHITEPIECE],1);
crop = new CropImageFilter(offset[0],offset[1],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[ROOK+BLACKPIECE] = createImage(mask);
trackr.addImage(iPiecemask[ROOK+BLACKPIECE],1);
//queens
crop = new CropImageFilter(offset[3],offset[0],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[QUEEN+WHITEPIECE] = createImage(mask);
trackr.addImage(iPiecemask[QUEEN+WHITEPIECE],1);
crop = new CropImageFilter(offset[3],offset[1],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[QUEEN+BLACKPIECE] = createImage(mask);
trackr.addImage(iPiecemask[QUEEN+BLACKPIECE],1);
//kings
crop = new CropImageFilter(offset[4],offset[0],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[KING+LIGHTSQUARE+WHITEPIECE] = createImage(mask);
trackr.addImage(iPiecemask[KING+WHITEPIECE],1);
crop = new CropImageFilter(offset[4],offset[1],offset[1],offset[1]);
mask = new FilteredImageSource(iPieces.getSource(),crop);
iPiecemask[KING+LIGHTSQUARE+BLACKPIECE] = createImage(mask);
trackr.addImage(iPiecemask[KING+BLACKPIECE],1);
int ImageWidth = offset[1]*8;
int ImageHeight = ImageWidth;
iBoard = createImage(ImageWidth + 12,ImageWidth + 12);
iBackground = createImage(offset[1],offset[1]);
iAnimationBackground = createImage(offset[1],offset[1]);
initstring = CVS.LOADING_MASKS_;
try {
initstring = CVS.LOADING_BLACK_PIECE;
trackr.waitForID(1);
initstring = CVS.LOADING_WHITE_PIECE;
//System.out.println("waitForAll did not crash " );
}
catch (Exception e){
System.out.println("waitForAll failed ");
e.printStackTrace();
}
repaint();
setLightSquareColor(lightSquareColor);
setDarkSquareColor(darkSquareColor);
promoteBlack = createImage(offset[1]*4,offset[1]);
promoteWhite = createImage(offset[1]*4,offset[1]);
if (promoteBlack == null) {
System.err.println("Chessboard.SetImage promoteBlack=null " + offset[1]);
return;
}
System.err.println("Chessboard.SetImage build initial position");
Graphics g = promoteBlack.getGraphics();
g.drawImage(iPiece[ROOK+LIGHTSQUARE+BLACKPIECE],0,0,this);
g.translate(offset[1],0);
g.drawImage(iPiece[KNIGHT+DARKSQUARE+BLACKPIECE],0,0,this);
g.translate(offset[1],0);
g.drawImage(iPiece[BISHOP+LIGHTSQUARE+BLACKPIECE],0,0,this);
g.translate(offset[1],0);
g.drawImage(iPiece[QUEEN+DARKSQUARE+BLACKPIECE],0,0,this);
g.dispose();
g = promoteWhite.getGraphics();
g.drawImage(iPiece[ROOK+DARKSQUARE+WHITEPIECE],0,0,this);
g.translate(offset[1],0);
g.drawImage(iPiece[KNIGHT+LIGHTSQUARE+WHITEPIECE],0,0,this);
g.translate(offset[1],0);
g.drawImage(iPiece[BISHOP+DARKSQUARE+WHITEPIECE],0,0,this);
g.translate(offset[1],0);
g.drawImage(iPiece[QUEEN+LIGHTSQUARE+WHITEPIECE],0,0,this);
g.dispose();
setSize(getPreferredSize());
init = true;
setPosition(prevpos,pieceColor);
drawBorder();
//System.err.println("Chessboard.SetImage last repaint");
initstring = CVS.CREATING_INITIAL;
repaint();
//System.err.println("Chessboard.SetImage return from repaint");
}
catch (Exception e) {
e.printStackTrace();
System.err.println("Chessboard.init " + e.toString());
repaint(0);
}
//System.err.println("Chessboard.init end");
}
public void setLightSquareColor(Color color) {
lightSquareColor = color;
if (iBoard != null) {
Graphics square = iPiece[0].getGraphics();
fillSquare(square,lightSquareColor);
square.dispose();
square = iPiece[8].getGraphics();
fillSquare(square,lightSquareColor);
square.dispose();
for (int i=1; i< 7;i++) {
for (int j=0; j <= BLACKPIECE;j=j+BLACKPIECE) {
// System.out.println("draw piece " + i + ","+ j);
Graphics g = iPiece[i+j+LIGHTSQUARE].getGraphics();
g.drawImage(iPiece[LIGHTSQUARE],0,0,this);
g.drawImage(iPiecemask[i+j],0,0,this);
g.dispose();
}
}
if (init)
repaint();
}
}
public Color getLightSquareColor() {
return lightSquareColor;
}
private void fillSquare(Graphics square,Color color) {
square.setColor(color);
square.fillRect(0,0,offset[1],offset[1]);
}
public void setDarkSquareColor(Color color) {
darkSquareColor = color;
if (iBoard != null) {
Graphics square = iPiece[16].getGraphics();
fillSquare(square,darkSquareColor);
square.dispose();
square = iPiece[24].getGraphics();
fillSquare(square,darkSquareColor);
square.dispose();
for (int i=1; i< 7;i++) {
for (int j=0; j <= BLACKPIECE;j=j+BLACKPIECE) {
// System.out.println("draw piece " + i + ","+ j);
Graphics g = iPiece[i+j+DARKSQUARE].getGraphics();
g.drawImage(iPiece[16],0,0,this);
g.drawImage(iPiecemask[i+j],0,0,this);
g.dispose();
}
}
if (init)
repaint();
}
}
public Color getDarkSquareColor() {
return darkSquareColor;
}
void drawBorder() {
if (iBoard == null)
return;
Graphics gBoard = iBoard.getGraphics();
if (gBoard == null)
return;
blackGoesOnTop = blackOnTop;
gBoard.setColor(getBackground());
int imagewidth = offset[1] * 8;
gBoard.fillRect(imagewidth,0,12,imagewidth + 12);
gBoard.fillRect(0,imagewidth,imagewidth,12);
gBoard.setColor(Color.black );
gBoard.setFont(boldfont);
int xoffset = (offset[1] - 8) / 2;
int yoffset = (offset[1] + 12) / 2;
if (blackOnTop) {
for (int i=0;i<8;i++) {
gBoard.drawString(String.valueOf(8-i),imagewidth + 2,(i * offset[1]) + yoffset);
}
for (int i=0;i<8;i++) {
gBoard.drawString(file[i],(i * offset[1]) + xoffset,imagewidth + 10);
}
}
else {
for (int i=0;i<8;i++) {
gBoard.drawString(String.valueOf(1+i),imagewidth + 2,(i * offset[1]) + yoffset);
}
for (int i=0;i<8;i++) {
gBoard.drawString(file[7-i],(i * offset[1]) + xoffset,imagewidth + 10);
}
}
gBoard.dispose();
}
public void setPosition(){
setPosition(prevpos,pieceColor);
}
public void setPosition(Boardsquares piecearray,Boardsquares colorarray) {
if (init) {
int rank = 0;
int file = 0;
int xoff = offset[1];
int yoff = offset[1];
int i = 0;
int newxoff;
int newyoff;
int squarecount = 0;
int pointax=0;
int pointay = 0;
xincrement = 0;
yincrement =0;
updateArray.init(prevpos);
updateArray.add(pieceColor);
updateArray.subtract(piecearray);
updateArray.subtract(colorarray);
for (rank = 0 ; rank < 8 ; rank++) {
for (file = 0 ; file < 8 ; file++) {
if (updateArray.value(file,rank) != 0) {
if (squarecount < 2) {
if (squarecount == 0) {
pointax = file;
pointay = rank;
}
else {
pointbx = file;
pointby = rank;
}
}
squarecount++;
}
}
}
boolean doAnimation = false;
if (squarecount == 2 && timer == null) {
if (piecearray.value(pointax,pointay) > 0 &&
prevpos.value(pointbx,pointby) > 0 ) {
file = pointax;
rank = pointay;
pointay = pointby;
pointax = pointbx;
pointbx = file;
pointby = rank;
}
file = pointax;
rank = pointay;
if (blackOnTop) {
xincrement = 2 *(pointbx - pointax); //left is 0, right is 7
yincrement = 2 *(pointay - pointby); //top is 7 bottom is 0
newxoff = file*xoff;
newyoff = yoff*7 - (rank*yoff);
}
else {
xincrement = 2 *(pointax - pointbx); //left is 7, right is 0
yincrement = 2 *(pointby - pointay); //top is 0 bottom is 7
newxoff = xoff*7 -(file*xoff);
newyoff = rank*yoff;
}
//set initial background to new square contents
synchronized (this) {
p = squareColor.value(file,rank) +
piecearray.value(file,rank) +
colorarray.value(file,rank);
Graphics gAnimationBackground = iAnimationBackground.getGraphics();
gAnimationBackground.drawImage(iPiece[p],0,0,this);
gAnimationBackground.dispose();
p = pieceColor.value(file,rank) +
prevpos.value(file,rank);
finalPiece = piecearray.value(pointbx,pointby) +
colorarray.value(pointbx,pointby) ;
xoffset = newxoff;
yoffset = newyoff;
if (colorarray.value(pointbx,pointby) == pieceColor.value(file,rank) && piecearray.value(pointbx,pointby) > 0) {
timer = new Thread(this);
timer.start();
doAnimation = true;
}
}
}
if (!doAnimation) {
synchronized (this) {
Graphics gBoard = iBoard.getGraphics();
xoffset = 0;
yoffset = 0;
for (rank = 0 ; rank < 8 ; rank++) {
for (file = 0 ; file < 8 ; file++) {
if (squarecount == 0 || updateArray.value(file,rank) != 0 || timer != null) {
if (blackOnTop) {
newxoff = file*xoff;
newyoff = yoff*7 - (rank*yoff);
}
else {
newxoff = xoff*7 -(file*xoff);
newyoff = rank*yoff;
}
i = squareColor.value(file,rank) +
colorarray.value(file,rank) +
piecearray.value(file,rank);
gBoard.translate(newxoff - xoffset,newyoff-yoffset);
gBoard.drawImage(iPiece[i],0,0,this);
xoffset = newxoff;
yoffset = newyoff;
}
}
}
gBoard.dispose();
}
if (timer != null) {
synchronized(timer) {
if (badTimeToStopThread)
System.out.println("How can it stop now?");
animate = false;
timer.notify();
timer = null;
}
}
repaint();
}
}
prevpos.init(piecearray);
pieceColor.init(colorarray);
}
public void stop() {
if (timer != null)
timer.stop();
System.out.println("Chessboard.stop was called");
timer = null;
}
//java.awt.Runnable method
public void run() {
int i;
int myy;
int myx;
animate = true;
Graphics gBoard = iBoard.getGraphics();
Graphics gAnimationBackground = iAnimationBackground.getGraphics();
try {
if (isLive = isEnabled())
setEnabled(false); //so stray mouse clicks don't screw us over
int absx = xincrement;
int absy = yincrement;
if (absx < 0)
absx = -absx;
if (absy < 0)
absy = -absy;
absx = absx + absy;
int timedelay = 10;
if (absx > 2)
timedelay = 15;
myx = xoffset;
myy = yoffset;
gBoard.translate(xoffset,yoffset);
int endingXOffset = xoffset + offset[1]*xincrement/2;
int endingYOffset = yoffset + offset[1]*yincrement/2;
boolean skip = false;
int loopcount = offset[1] /2;
if (p > finalPiece)
p = finalPiece; //promoted pieces moving backwards move as pawns
int endingpiece = finalPiece + squareColor.value(pointbx,pointby) ;
int piece = p;
for (i=0;i<loopcount;i++) {
if (iPiecemask[p] == null)
System.out.println("Chessboard.run iPiecemask[" + p + "] null ");
int translatex;
int translatey;
synchronized (this) {
if (animate) {
badTimeToStopThread = true;
translatex = myx - xoffset ;
translatey = myy - yoffset ;
if (translatex + translatey != 0) {
gBoard.translate(translatex,translatey);
xoffset = xoffset + translatex;
yoffset = yoffset + translatey;
}
myx = xoffset + xincrement;
myy = yoffset + yincrement;
gBoard.drawImage(iAnimationBackground,0,0,this);
gAnimationBackground.drawImage(iBoard,-1*myx,-1*myy,this);
translatex = myx-xoffset;
translatey = myy-yoffset;
gBoard.translate(translatex,translatey);
xoffset = myx;
yoffset = myy;
gBoard.drawImage(iPiecemask[piece],0,0,this);
badTimeToStopThread = false;
}
else
break;
}
if ( skip == false) {
repaint();
try {Thread.sleep(timedelay);}
catch (InterruptedException e){}
catch (Exception e){System.err.println("Chessbord.run "+ e.toString());}
}
if (timedelay == 10)
if (skip == false)
skip = true;
else
skip = false;
}
if (animate) {
synchronized (this) {
int translatex = myx - xoffset ;
int translatey = myy - yoffset ;
if (translatex + translatey != 0) {
xoffset = xoffset + translatex;
yoffset = yoffset + translatey;
gBoard.translate(translatex,translatey);
}
gBoard.drawImage(iAnimationBackground,0,0,this);
gBoard.translate(endingXOffset - xoffset,endingYOffset - yoffset);
xoffset = endingXOffset;
yoffset = endingYOffset;
gBoard.drawImage(iPiece[endingpiece],0,0,this);
}
}
if (isLive)
setEnabled(true);
}
catch(Exception x ) {System.err.println("Chessboard.run "+ x.toString());}
catch(ThreadDeath x) {
if (badTimeToStopThread)
System.out.println("You picked the wrong time to give up running");
else
System.err.println("ThreadDeath");
if (isLive)
setEnabled(true);
throw x;
}
gBoard.dispose();
gAnimationBackground.dispose();
xoffset = 0;
yoffset = 0;
repaint();
timer = null;
}
//java.awt.Component method
public void update(Graphics g) {
paint(g);
}
//java.awt.Component method
public void paint(Graphics g) {
//System.err.println("Chessboard.paint " + init + " initfailed=" + initfailed);
if (init == true && initfailed == false) {
if (blackOnTop ^ blackGoesOnTop) {
drawBorder();
}
g.drawImage(iBoard,0,0,this);
}
else {
System.err.println("Chessboard.paint " + init + " initfailed=" + initfailed + " " + boardURL);
g.setColor(getBackground());
g.fillRect(0,0,280,280);
g.setColor(Color.blue );
g.drawString(initstring,10,135);
//The designers first call to setImage never seems to work
//if (retryImage && init == false && initfailed == false && boardURL != null)
// setImageURL(boardURL);
}
}
// java.awt.MouseMotionListener method
public void mouseDragged(MouseEvent mev) {
if (mouseActive == true) {
currentTime = System.currentTimeMillis();
int squareRadius = offset[1]/2; //fudge factor so image is centered on the cursor
int x = mev.getX() - squareRadius;
int y = mev.getY() - squareRadius;
//System.out.println("mouse dragged");
synchronized (this) {
Graphics gBoard = iBoard.getGraphics();
gBoard.translate(xOldMouse,yOldMouse);
gBoard.drawImage(iBackground,0,0,this);
gBoard.translate(x-xOldMouse,y-yOldMouse);
xOldMouse = x;
yOldMouse = y;
Graphics gBackground = iBackground.getGraphics();
gBackground.drawImage(iBoard,-1*(xOldMouse),-1*(yOldMouse),this);
gBoard.drawImage(iPiecemask[p],0,0,this);
gBoard.dispose();
gBackground.dispose();
}
repaint();
}
return ;
}
// java.awt.MouseMotionListener method
public void mouseMoved(MouseEvent e){}
// java.awt.MouseListener method
public void mouseClicked(MouseEvent e){}
// java.awt.MouseListener method
public void mouseExited(MouseEvent e){}
// java.awt.MouseListener method
public void mouseEntered(MouseEvent e){}
// java.awt.MouseListener method
public void mousePressed(MouseEvent mev) {
int x = mev.getX();
int y = mev.getY();
int squareSize = offset[1] ;
//System.out.println("mouse pressed");
int mousex = (x / squareSize) * squareSize;
int mousey = (y / squareSize) * squareSize;
if (x/squareSize > 7 || y /squareSize > 7) {
//System.out.println("click out of bounds" + squareSize + "," + x + "," + y);
return ;
}
synchronized (this) {
xoffset = mousex ;
yoffset = mousey;
xOldMouse = xoffset;
yOldMouse = yoffset;
if (blackOnTop) {
startfile = x/squareSize;
startrank = 7 - (y/squareSize);
}
else {
startfile = 7 - (x/squareSize);
startrank = y/squareSize;
}
p = prevpos.value(startfile,startrank) ;
if (p > 0) {
p = p + pieceColor.value(startfile,startrank);
Graphics gBackground = iBackground.getGraphics();
gBackground.drawImage(iPiece[squareColor.value(startfile,startrank)],0,0,this);
gBackground.dispose();
mouseActive = true;
}
}
return ;
}
// java.awt.MouseListener method
public void mouseReleased(MouseEvent mev){
if (!mouseActive)
return ;
//System.out.println("mouse released");
int x = mev.getX();
int y = mev.getY();
int piecevalue =0;
int piececolor =0;
synchronized (this) {
Graphics gBoard = iBoard.getGraphics();
if (mouseActive) {
gBoard.translate(xOldMouse,yOldMouse);
gBoard.drawImage(iBackground,0,0,this);
}
else {
xOldMouse = 0;
yOldMouse = 0;
}
int squareSize = offset[1] ;
int mousex = (x / squareSize) * squareSize;
int mousey = (y / squareSize) * squareSize;
if (blackOnTop) {
endfile = x/squareSize;
endrank = 7 - (y/squareSize);
}
else {
endfile = 7 - (x/squareSize);
endrank = y/squareSize;
}
if (startrank != endrank || startfile != endfile) {
if (endrank < 0 || endrank > 7 || endfile < 0 ||
endfile > 7) {
endrank = startrank;
endfile = startfile;
if (blackOnTop) {
mousex = startfile * squareSize;
mousey = (7 - startrank) * squareSize;
}
else {
mousex = (7 - startfile) * squareSize;
mousey = startrank * squareSize;
}
}
if (startrank < 0 || startrank > 7)
System.out.println("Startrank =" + startrank + "??");
if (startfile < 0 || startfile > 7)
System.out.println("Startfile =" + startfile + "??");
piecevalue = prevpos.value(startfile,startrank);
piececolor = pieceColor.value(startfile,startrank);
prevpos.assign(startfile,startrank,0);
pieceColor.assign(startfile,startrank,0);
prevpos.assign(endfile,endrank,piecevalue);
pieceColor.assign(endfile,endrank,piececolor);
}
gBoard.translate(mousex - xOldMouse,mousey - yOldMouse);
int w = squareColor.value(endfile,endrank) + p;
gBoard.drawImage(iPiece[w],0,0,this);
gBoard.dispose();
}
repaint();
if (startrank != endrank || startfile != endfile) {
broadcast(piecevalue,piececolor);
}
mouseActive = false;
return ;
}
void broadcast(int piecevalue,int piececolor) {
boolean showPromotionDialog = false;
PromotionDialog dDialog = null;
try {
if (piecevalue == PAWN &&
(endrank == 0 || endrank == 7)) {
if (piececolor == WHITEPIECE && startrank ==6) {
showPromotionDialog = true;
dDialog = new PromotionDialog(getFrame(this),promoteWhite,this);
}
else
if (piececolor == BLACKPIECE && startrank ==1) {
showPromotionDialog = true;
dDialog = new PromotionDialog(getFrame(this),promoteBlack,this);
}
}
if (showPromotionDialog){
//System.out.println("Show promotion dialog");
dDialog.pack();
dDialog.setLocation(360,300);
dDialog.setVisible(true);
//System.out.println(" promotion dialog Shown");
}
else {
tuple = new MoveTuple();
tuple.pieceValue = prevpos.value(endfile,endrank);
tuple.newpieceValue = tuple.pieceValue;
tuple.pieceColor = pieceColor.value(endfile,endrank);
tuple.fromFile = startfile;
tuple.fromRank = startrank;
tuple.toFile = endfile;
tuple.toRank = endrank;
handleActionEvent(new ActionEvent(this,1,null));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Frame getFrame(Component theComponent) {
Component currParent = theComponent;
Frame theFrame = null;
while (currParent != null) {
if (currParent instanceof Frame){
theFrame = (Frame) currParent;
break;
}
currParent = currParent.getParent();
}
return theFrame;
}
public synchronized void addActionListener(ActionListener l) {
if (listener == null)
listener = l;
}
public synchronized void removeActionListener(ActionListener l) {
listener = null;
}
public ActionListener getActionListener() {
return listener;
}
protected void handleActionEvent(ActionEvent ae) {
if (getActionListener() != null) {
getActionListener().actionPerformed(ae);
}
}
public MoveTuple getTuple() {
return tuple;
}
void promote(int piecesub) {
tuple = new MoveTuple();
switch (piecesub) {
case 3:
tuple.newpieceValue = QUEEN;
break;
case 1:
tuple.newpieceValue = KNIGHT;
break;
case 0:
tuple.newpieceValue = ROOK;
break;
case 2:
tuple.newpieceValue = BISHOP;
break;
}
tuple.pieceValue = prevpos.value(endfile,endrank);
tuple.pieceColor = pieceColor.value(endfile,endrank);
tuple.fromFile = startfile;
tuple.fromRank = startrank;
tuple.toFile = endfile;
tuple.toRank = endrank;
handleActionEvent(new ActionEvent(this,1,null));
}
public boolean isBlackOnTop() {
return blackOnTop;
}
public boolean switchOrientation() {
blackOnTop = !blackOnTop;
int squareSize = offset[1];
int rank,file,newxoff,newyoff,i;
int xoffset = 0;
int yoffset = 0;
Graphics gBoard = iBoard.getGraphics();
for (rank = 0 ; rank < 8 ; rank++) {
for (file = 0 ; file < 8 ; file++) {
if (blackOnTop) {
newxoff = file*squareSize;
newyoff = squareSize*7 - (rank*squareSize);
}
else {
newxoff = squareSize*7 -(file*squareSize);
newyoff = rank*squareSize;
}
i = squareColor.value(file,rank) +
pieceColor.value(file,rank) +
prevpos.value(file,rank);
synchronized (this) {
gBoard.translate(newxoff - xoffset ,newyoff-yoffset );
gBoard.drawImage(iPiece[i], 0, 0, this);
xoffset = newxoff;
yoffset = newyoff;
}
}
}
gBoard.dispose();
repaint();
return blackOnTop;
}
}
class Transparent extends RGBImageFilter
{
int backgroundPixel = 0xFF00FF00;
public Transparent() {
canFilterIndexColorModel = false;
}
public int filterRGB(int x, int y,int rgb) {
try{
if (x == 0 && y ==0)
backgroundPixel = rgb;
if (backgroundPixel == rgb)
rgb = rgb & 0x00FFFFFF ;
}
catch(Exception e) {
e.printStackTrace();
//System.out.println("filterRGB " + e.toString());
}
return rgb;
}
}