Reputation: 462
I want to overlap two image. For that, I use JLabel and set an ImageIcon to it. I also have a JPanel using a gridlayout containing my images (like a tile-based map). So far, no problem and it works great.
The problem I encounter, is when I tried to overlap two image. For that, I tried with a JLayeredPane in the gridlayout and put two images (JLabel) in a different level. This method throws me this exception:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
I made some research and tried to downgrade to jdk-6 instead of 7, cause it appears that in the jdk-7 it doesn't work well. The exception has now disappeared, but nothing is drawn on the screen.
Then I tried a new method by creating a class that extends JComponent and I override the paintComponent() method. My class contain an arrayList of my images (JLabel) and in the paintComponent method, I iterate through the arrayList and call each label's paintComponent method with the Graphics object of my own class. The same exception is raised.
Here's a sample my paintComponent method:
@Override
public void paintComponent(Graphics g) {
for(JLabel label : this.images) {
label.paintComponents(g);
}
}
Upvotes: 1
Views: 2119
Reputation: 32391
If overridding paint
or paintComponent
is an option then I would suggest you use the Graphics
or Graphics2d
APIs for drawing images:
g.drawImage();
It offers a lot more flexibility for what you need.
Upvotes: 2
Reputation: 109813
you can to use
JLayer (Java7) based on JXLayer (Java6)
then you can (for example) moving or animate with Icons / ImageIcons placed in JLabel
Upvotes: 4