• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variable to store Swing Object?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create a human on human chess program using java swing. I have 64 jPanels back to back (alternating between grey and white background colors), with a jLabel inside each one that I am using to display icons for the pieces. Right now I'm trying to make it so that you can move a piece from one panel to another by clicking the panels in sucession. I also am using an icon array called "board" to store the positions of all the pieces. Right now, I have the following code for the mouseclicked event for each jPanel. So far the program sucesfully duplicates the icon of the first clicked jPanel to the second (and makes the corresponding change to the board array). However, I am having trouble removing the icon from the first clicked jPanel so that the piece actually 'moves', rather than just duplicating. I know I somehow need to store which panel is originally selected in some kind of variable, and then use that variable to set the icon of the original jPanel to null when the second panel is clicked. However, short of using an integer for the selected panel (1-64), and then a really long switch statement (switch (int), case 1: jPanel1.setIcon(null), case 2: jPanel2.setIcon(null)), etc.), I don't know what kind of variable to use. Is there an easier way to do this (than just using the switch statement)? Like some kind of object variable that I can store a swing object inside and then use later to command whatever object is stored inside? Anyone know how to do what I'm talking about? Help would be much appreciated.

private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {

if (pieceSelected == false)
{
pieceSelection = board[0][0];
pieceSelected = true;
} else //if a piece was selected when this panel was clicked
{
//change this panel's icon to the icon of the first panel
jLabel1.setIcon(pieceSelection);
board[0][0] = pieceSelection;

//change the icon of the first square clicked to null
//the following part is the part i'm having trouble with
//board[last x coordinate][last y coordinate] = null;
//last jPanel.setIcon(null)

pieceSelected = false;
}
}
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Louis,

yes, there are better ways to do it. I'll show you the gui that I use for my chessboard. Note however, there are "many roads leading to Rome",
so don't think my way is the only possible way.
Here goes...

first of all, I only use one JPanel for the chessboard; let's call it jp_Chessoard. And I use a GridLayout(8,8) as the LayoutManager.
In the grid, I will put JLabels, albeit it special ones. Here is my ChessSquare (in Dutch: Bordveld):
}

And then I do the following routine:



And lastly, I have an array of integers, 8 by 8, that represents the chess position; each piece having a certain fixed value.
And from this array I decide what icon to put in my Bordveld labels. Don't forget to put the jp_Chessborad to a JFrame.

Well, I hope that with the code given you have some idea of how to get a chessboard on your screen, with some fairly short code.

Greetings,
Piet


 
I'm so happy! And I wish to make this tiny ad happy too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic