Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

moving the pictures

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi again,

Ive got a picture of a knight on my board, but I'm struggling to get it
moving.
I'm new to all this graphics stuff, so its difficult for me at the moment.
Basically I know how to print pictures on a frame, I've got a board printed,
as well as a black knight.
But before, I was using letters(as Strings) for the pieces, and using
tokens to get them to move (by using mouse, or by commands in the
program ,such as when the computer wants to make move).
Im trying to adapt this tokens method for images, but as I said, I dont
really have a clue what Im doing.

Heres a piece of my code so far, let me know if u can help.
Thanks

class ChessBoard extends JPanel
{
Font font;
static double[] XPOSITION=new double[8];
static double[] YPOSITION=new double[8];
Dimension tokenSize;
Token[][] tokens;
Token pics;
final int PAD=0;


public ChessBoard()
{
font = new Font("chess alpha", Font.PLAIN, 36);
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
//Image blackKnight = new ImageIcon("bnb.gif").getImage();
//g.drawImage(blackKnight, 50, 50, this);

Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

//w,h is usable width/height of frame,
double w = getWidth();
double h = getHeight();

//xInc,yInc is usable width/height of squares
double xInc = (w - 2*PAD) / 8;
double yInc = (h - 2*PAD) / 8;

//x1,y1 is top left corner, x2,y2 is bottom right corner
double x1 = PAD, y1 = PAD, x2 = w - PAD, y2 = h - PAD;


// grid lines - horizontal and vertical
for(int j=0;j<=8;j++)
{
g2.draw(new Line2D.Double(x1, y1, x2, y1));
y1 += yInc;
}
y1 = PAD;
for(int j=0;j<=8;j++)
{
g2.draw(new Line2D.Double(x1, y1, x1, y2));
x1 += xInc;
}

// tokens
g2.setFont(font);
if(tokens == null) initTokens(g2, xInc, yInc);
//for(int j = 0; j < tokens.length; j++)
for(int j=0;j<8;j++) for(int i=0;i<8;i++) tokens[j][i].draw(g2);
}

public Token[][] getTokens()
{
return tokens;
}



private void initTokens(Graphics2D g2, double xInc, double yInc)
{Image blackKnight = new ImageIcon("bnb.gif").getImage();
String[][] key = { // white black
{ "R", "N", "B", "Q", "K", "B", "N", "R" }, // pawn o p
{ "p", "p", "p", "p", "p", "p", "p", "p" }, // rook r R
{ " ", " ", " ", " ", " ", " ", " ", " " }, // knight n N
{ " ", " ", " ", " ", " ", " ", " ", " " }, // bishop b B
{ " ", " ", " ", " ", " ", " ", " ", " " }, // queen q Q
{ " ", " ", " ", " ", " ", " ", " ", " " }, // king k K
{ "o", "o", "o", "o", "o", "o", "o", "o" },
{ "r", "n", "b", "q", "k", "b", "n", "r" } };

tokens = new Token[8][8];

FontRenderContext frc = g2.getFontRenderContext();
Rectangle2D r = font.getStringBounds("L", frc);
Dimension d = new Dimension((int)r.getWidth(), (int)r.getHeight());

double x = PAD + (xInc - r.getWidth())/2;
double y = PAD + (yInc + r.getHeight())/2;


for(int row=0;row<8;row++)
{
for(int col=0;col<8;col++)
{
XPOSITION[col]=x;
YPOSITION[row]=y;
tokens[row][col] = new Token(key[row][col], x, y, d);
x += xInc;
}
x = PAD + (xInc - r.getWidth())/2;
y += yInc;
}
pics=new Token(blackKnight,150,150,d);

}

//public void reset()
//{
// tokens = null;
// repaint();
//}
}








class TokenMover extends MouseInputAdapter
{
ChessBoard chessBoard;
Token selectedToken;
Point2D.Double offset;
boolean dragging;
public TokenMover(ChessBoard cb)
{
chessBoard = cb;
offset = new Point2D.Double();
dragging = false;
}

public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();

//System.out.println("x="+p.x+" y="+p.y);
Token[][] tokens = chessBoard.getTokens();
Token pics=chessBoard.pics;

//for(int j=0;j<tokens.length;j++)
for(int j=0;j<8;j++) for(int i=0;i<8;i++)
if(tokens[j][i].contains(p))
{
selectedToken = tokens[j][i];
offset.x = p.x - tokens[j][i].x;
offset.y = p.y - tokens[j][i].y;
dragging = true;
break;
}

if(pics.contains(p))
{ selectedToken = pics;
offset.x = p.x - pics.x;
offset.y = p.y - pics.y;
dragging = true;

}

//if(e.getClickCount() == 2) chessBoard.reset();
}

public void mouseReleased(MouseEvent e)
{
Point p = e.getPoint();
dragging = false;
System.out.println("x="+p.x+" y="+p.y);

}


public void mouseDragged(MouseEvent e)
{
if(dragging)
{
double x = e.getX() - offset.x;
double y = e.getY() - offset.y;
selectedToken.setLocation(x, y);
chessBoard.repaint();
}
}
}




class Token
{ Image image;
String value;
double x, y;
Rectangle r;
public Token(String s, double x, double y, Dimension size)
{
value = s;
this.x = x;
this.y = y;
r = new Rectangle(size);
}


//I JUST MADE THIS BIT UP, AND IT DOESNT WORK,
//public Token(Image image, double x, double y, Dimension size)
//{
// this.image=image;
// this.x = x;
// this.y = y;
// r = new Rectangle(size);
//}


public void draw(Graphics2D g2)
{
g2.drawString(value, (float)x, (float)y);
}


public boolean contains(Point p)
{
r.setFrame(x, y - r.height, r.width, r.height);
if(r.contains(p))
return true;
return false;
}

public void setLocation(double x, double y)
{
this.x = x;
this.y = y;
}
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like you've got the general ideas correct, but there's too much code, I think, for anybody to trace through every bit and tell you what's what. You'll need to help us out a little with some more information.

What doesn't work? What's wrong with the commented-out constructor? What happens if you try to run this code?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic