[Solved] Displaying multiple images without creating new variables [closed]


There are two basic approaches, the both kind of follow the same basic concept…

Generate a list of images and paint them…

You Could…

Create a custom component which is singularly responsible for painting a single card. The benefit of this is that it’s generally easy to manage and uses what is already available.

The draw back is, you’re going to need take charge the the layout management (a little).

You would start with a LayredPane, which will allow you to push cards up and down (virtually) as well as allow them to be placed where you want them.

Personally, I would create a single MouseListener/MouseMotionListener and register it to each of the “Card Panels”, this will allow to easily control the cards with relation to the LayeredPane (moving them to the top when they are clicked for example)

You Could…

The other choice would be to load each of the card images and place them into List. Then basically, you would then simply paint them with in the paintComponent method by simply iterating through the list.

The problem is, you suddenly become responsible for the entire management of the z-order and checking what is clicked.

Customising the painting of the images (such as adding borders) becomes a little cumbersome, as you need to do this for each image and decide when. This would be easier if they were a custom component

Feedback

  • Beware of the over use of static variables, this can quickly lead you into problem areas with trying to do determine what is actually being referenced
  • Don’t call paint from paintComponent. paint actually calls paintComponent itself, which could produce a stack overflow errors
  • Don’t call any method from within a paintXxx method that might call repaint, because of the way painting works, this will simply cause the EDT to simply keep painting your component, which will eventually consume your CPU…

Take a closer look at Performing Custom Painting and Painting in AWT and Swing for more details

2

solved Displaying multiple images without creating new variables [closed]