I have a JPanel
set up, and I have JLabel
s acting as pseudo-buttons placed on top of the JPanel
(with jPanel.add(jLabel)
). I want to animate the JPanel
without affecting the JLabel
s, but every time I repaint the JPanel
, the labels disappear. (I believe they are being painted on).
I've also tried using a JLayeredPane
and separating the JLabel
s and JPanel
, but that doesn't do the trick either.
Is there a way I can achieve what I want?
My Code
This sets it up the JLabel and jPanel.
mainPanel = new JPanel();
JLayeredPane c = new JLayeredPane();
c.setPreferredSize(new Dimension(WIDTH, HEIGHT));
c.setVisible(true);
c.setOpaque(true);
c.add(mainPanel,new Integer(0));
c.add(test, new Integer(2));
add(c);
mainPanel.setBounds(0, 0, WIDTH, HEIGHT);
This shows my paintComponent method inside the JPanel (its a custom class that extends JPanel)
public void paintComponent(Graphics g){
super.paintComponent(g);
scene = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
g2d = scene.createGraphics();
Rectangle2D.Double rec = new Rectangle2D.Double(questionBox.getXFloat(), questionBox.getYFloat(), questionBox.getWidthFloat(), questionBox.getHeightFloat());
g2d.fill(rec);
g.drawImage(scene, 0, 0, this);
}
For clarification, the questionBox
methods just get the updated values for the rectangle. w
and h
stand for width and height.
This is in the constructer for my JLabel class
scene = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);