I want to be able to display two BufferedImages on a component which then will be able to be moved around the component using mouse drags and also overlap one another if dragged over the top of the other image.
So far I have been able to display two BufferedImages on top of one another by simply placing them within their own JLabels and then both onto the same JPanel, however I’m afraid I may not be thinking about this in the correct way.
Any help, advice or examples on this would be very appreciated.
For this kind of situation, I usually use a
JLayeredPane(which uses no-layout, meaning that you have to call yourselfsetBounds()orsetSize()/setLocation()) but which handles pretty well layering and seems quite fitted for dragging freely components.Here is a small demo example (which displays 10 draggable images (always the same)). Code could be improved but it gives you already a pretty good impression of what you could do.
The result (each “Bowser” can be freely moved/dragged within the frame with your mouse):
