8 Replies - 1231 Views - Last Post: 09 December 2012 - 09:50 PM Rate Topic: -----

#1 nvrBungeeJumped  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-December 12

Chess with AI to actually make a move; moving JLabel to another JPanel

Posted 08 December 2012 - 08:20 PM

So I'm working on a chess game. I have a layered pane which I add JPanels to for the tiles and I use a JLabel to hold the images (pieces).

When it comes time for the AI to implement a move. What is the best way to go about doing that? Is here a method to move a JLabel to another JPanel or would it be easier to delete the JLabel and create a new one on the other Panel?

Here is my code for creating one piece.
JLabel piece = new JLabel(new ImageIcon("king.png"));
JPanel panel = (JPanel)board.getComponent(40);
panel.add(piece);



Is This A Good Question/Topic? 0
  • +

Replies To: Chess with AI to actually make a move; moving JLabel to another JPanel

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Chess with AI to actually make a move; moving JLabel to another JPanel

Posted 08 December 2012 - 08:28 PM

You would not actually need to move the JLabels, just simply change the icons :) Seems a lot simpler to change the icons of the labels instead of changing the layout

And are you using the JPanel for the black/white background?? Wouldn't it be easier having a JLabel with a black/white background set and then an icon for the chess piece with a transparent background :) ?
Was This Post Helpful? 1
  • +
  • -

#3 nvrBungeeJumped  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-December 12

Re: Chess with AI to actually make a move; moving JLabel to another JPanel

Posted 08 December 2012 - 08:31 PM

wouldn't that mean putting every chess piece on every tile and just toggling through the one you wanted visible on that tile?
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Chess with AI to actually make a move; moving JLabel to another JPanel

Posted 08 December 2012 - 08:43 PM

View PostnvrBungeeJumped, on 09 December 2012 - 03:31 AM, said:

wouldn't that mean putting every chess piece on every tile and just toggling through the one you wanted visible on that tile?


No I would not do it that way. A JLabel can only have one icon

JLabel has a method called setIcon(Icon icon), which you can use to change the icon of the label

Lets try and take an example where a piece moves from a2 to a3
ChessPiece piece = ...; //The piece we want to move
JLabel a2 = ...; //The JLabel moved from
JLabel a3 = ...; //The JLabel moved to

a2.setIcon(null); //Remove the icon from the label moved from. No icon will be displayed
a3.setIcon(piece.getIcon()); //Paint 'piece' at this label


Was This Post Helpful? 1
  • +
  • -

#5 nvrBungeeJumped  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-December 12

Re: Chess with AI to actually make a move; moving JLabel to another JPanel

Posted 08 December 2012 - 09:15 PM

Okay that works, but it is kind of a weird way to do it.

Is there not a way to take a JLabel from a panel and move it to an other panel, or is that essentially it? Delete it and recreate it somewhere else?
Was This Post Helpful? 0
  • +
  • -

#6 nvrBungeeJumped  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-December 12

Re: Chess with AI to actually make a move; moving JLabel to another JPanel

Posted 08 December 2012 - 09:40 PM

View PostCasiOo, on 08 December 2012 - 08:43 PM, said:

View PostnvrBungeeJumped, on 09 December 2012 - 03:31 AM, said:

wouldn't that mean putting every chess piece on every tile and just toggling through the one you wanted visible on that tile?


No I would not do it that way. A JLabel can only have one icon

JLabel has a method called setIcon(Icon icon), which you can use to change the icon of the label

Lets try and take an example where a piece moves from a2 to a3
ChessPiece piece = ...; //The piece we want to move
JLabel a2 = ...; //The JLabel moved from
JLabel a3 = ...; //The JLabel moved to

a2.setIcon(null); //Remove the icon from the label moved from. No icon will be displayed
a3.setIcon(piece.getIcon()); //Paint 'piece' at this label



I'm having a tough time selecting which Panel I want to Remove the JLabel from. Like I can remove a JLabel but only from the last panel I created? Any clue on how to change the selection?
Was This Post Helpful? 0
  • +
  • -

#7 nvrBungeeJumped  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-December 12

Re: Chess with AI to actually make a move; moving JLabel to another JPanel

Posted 08 December 2012 - 10:14 PM

Basically. I create my board which is a bunch of JPanels in a JLayeredPane. Then I use
panel.remove(Piece);
but it only works on the last panel I created.

Is there anyway to change the selection/active pane. That way I can select which panel I want to remove a piece from?
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Chess with AI to actually make a move; moving JLabel to another JPanel

Posted 09 December 2012 - 07:25 AM

I see you are still trying to do it the hard way
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Chess with AI to actually make a move; moving JLabel to another JPanel

Posted 09 December 2012 - 09:50 PM

View PostnvrBungeeJumped, on 09 December 2012 - 12:15 AM, said:

Okay that works, but it is kind of a weird way to do it.

Is there not a way to take a JLabel from a panel and move it to an other panel, or is that essentially it? Delete it and recreate it somewhere else?

Don;t think that weird at all
Actually dynamically adding/removing JCompnent to a Container is not a good idea at all, it can screw up a Layout quite fast
Just have JLabel pre-added everywhere and simply change what it displays
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1