So I have a DisplayPanel class that extends JPanel and that also paints my numerous images for my program using Graphics2D. In order to be able to easily customly use this I set it up so that every time the panel is repainted it uses a List, that I can add to or remove from as the program processes. My problem is with layering. I've run into an issue where the List must have reached its resizing point (or something whacky like that) and so the images i want to display end up beneath all of the other images already on the screen. I've come to the community for an answer because I have faith you will provide a good one.
DisplayPanel:
package earthworm;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class DisplayPanel extends JPanel {
private List<ImageMap> images = new ArrayList();
public DisplayPanel() {
setSize(800, 640);
refresh();
}
public void refresh() {
revalidate();
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.clearRect(0, 0, 800, 640);
for(int i = 0; i < images.size(); i++) g2d.drawImage(
images.get(i).getImage(), images.get(i).getX(),
images.get(i).getY(), null);
}
public void paintImage(ImageMap[] images, ImageMap[] clearImages, boolean clear) {
if(clear) this.images.clear();
else if(clearImages!=null) for(int i = 0; i < clearImages.length; i++) this.images.remove(clearImages[i]);
if(images!=null) for(int i = 0; i<images.length; i++) this.images.add(images[i]);
refresh();
}
}