Search code examples
javaimageicon

java imageicon bring to front


I have a list of cards and they have a imageicon, i have all my cards painted on the deck, and i can move them with mousePressed and mouseDragged, now i want to bring a card to front when i click on it, a card that is behind other cards, when i click it moves to the front of all cards. how do i do this?

here is my mousepressed code :

int x = e.getX();   // Save the x coord of the click
int y = e.getY();   // Save the y coord of the click

//... Find card image this is in.  Check from top down.
_currentCard = null;  // Assume not in any image.
for (int crd=_deck.length-1; crd>=0; crd--) {
    Card testCard = _deck[crd];
    if (testCard.contains(x, y)) {
        //... Found, remember this card for dragging.
        _dragFromX = x - testCard.getX();  // how far from left
        _dragFromY = x - testCard.getY();  // how far from top
         _currentCard = testCard; // Remember what we're dragging.

        break;        // Stop when we find the first match.
    }
}

Solution

  • Use an ArrayList and remove the selected card from the list and add it back at the top. Then paint the cards in the order of the list.