0

I have a JPanel set up, and I have JLabels acting as pseudo-buttons placed on top of the JPanel (with jPanel.add(jLabel)). I want to animate the JPanel without affecting the JLabels, 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 JLabels 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);
Shahmeer Navid
  • 566
  • 4
  • 10
  • 18
  • It is very hard to guess what you may be doing wrong since you haven't posted any code. Consider giving us more information and pertinent code. Are you using a Swing Timer for the animation? Are you drawing in the JPanel's `paintComponent(...)` method? Do you call the `super.paintComponent(...)` method first? Please give us more to allow us to understand what's going on and be able to help you. – Hovercraft Full Of Eels Dec 02 '12 at 03:24
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 02 '12 at 03:26
  • Ive updated with my code – Shahmeer Navid Dec 02 '12 at 03:31
  • 1
    I'm not seeing what's wrong in the code based on what you've posted other than its unusual tack of having a BufferedImage held by a JLabel object being drawn in a JPanel's paintComponent method (??). I second the recommendation that you create and post an [sscce](http://sscce.org) – Hovercraft Full Of Eels Dec 02 '12 at 03:37
  • Sorry I mistyped, the scene variable is in the JPanel class. Also, the reason I am using a buffered image is because I am using the java Universal Tween Engine, which requires floats to be used (just using g.fillRect() only integers). I will post a sscce soon. – Shahmeer Navid Dec 02 '12 at 03:46
  • Two working examples are shown among the answers [here](http://stackoverflow.com/q/13636623/230513). – trashgod Dec 02 '12 at 03:49

0 Answers0