6

I'm trying to gradually build up an image based on user inputs. What I'm trying to do is create a bunch of graphics and add them as layers however I'm having some issues as they won't show up. Here is the code I'm using:

public class ClassA 
{
    protected final static int dimesionsY = 1000;
    private static int dimesionsX;
    private static JFrame window;
    private static JLayeredPane layeredPane;

    public void init()
    {
        window = new JFrame("Foo");
        dimesionsX = // some user input
        window.setPreferredSize(new Dimension(dimesionsX, dimesionsY));
        window.setLayout(new BorderLayout());

            layeredPane = new JLayeredPane();
        layeredPane.setBounds(0, 0, dimesionsX, dimesionsY);
        window.add(layeredPane, BorderLayout.CENTER);

            ClassB myGraphic = new ClassB();    
        myGraphic.drawGraphic();

        layeredPane.add(myGrpahic, new Integer(0), 0);

        window.pack();
        window.setVisible(true);
    }
}



public class ClassB extends JPanel
{
    public void drawGraphic()
    {
        repaint();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.setColor(Color.BLACK);
        g.fillRect(10, 10, 100, 100);
    }
}

However my graphic doesn't seem to show up and I don't understand why. I have also tried add it to a JPanel first, adding that JPanel to the JLayeredPane however that didn't work either.

Please can someone help me out?

2 Answers2

13

If you add a component to a JLayeredPane, it's like adding it to a null layout using container: you must fully specify the component's size and position.

e.g.,

import java.awt.*;

import javax.swing.*;

public class ClassA {
   protected final static int dimesionsY = 800;
   protected final static int dimesionsX = 1000; //!!
   private static JFrame window;
   private static JLayeredPane layeredPane;

   public void init() {
      window = new JFrame("Foo");
      // !! dimesionsX = // some user input

      //!! window.setPreferredSize(new Dimension(dimesionsX, dimesionsY));
      window.setLayout(new BorderLayout());

      layeredPane = new JLayeredPane();
      //!! layeredPane.setBounds(0, 0, dimesionsX, dimesionsY);
      layeredPane.setPreferredSize(new Dimension(dimesionsX, dimesionsY));
      window.add(layeredPane, BorderLayout.CENTER);

      ClassB myGraphic = new ClassB();
      myGraphic.drawGraphic();

      myGraphic.setSize(layeredPane.getPreferredSize());
      myGraphic.setLocation(0, 0);
      //!! layeredPane.add(myGraphic, new Integer(0), 0);
      layeredPane.add(myGraphic, JLayeredPane.DEFAULT_LAYER);

      window.pack();
      window.setVisible(true);
   }

   public static void main(String[] args) {
      new ClassA().init();
   }
}

class ClassB extends JPanel {
   public void drawGraphic() {
      repaint();
   }

   public void paintComponent(Graphics g) {
      super.paintComponent(g);

      g.setColor(Color.BLACK);
      g.fillRect(10, 10, 100, 100);
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Also, if you're laying JPanels on top of each other, hopefully you are taking care that opaque is set to false. Another solution for your problem include layering BufferedImages on top of each other. – Hovercraft Full Of Eels Oct 29 '11 at 12:40
  • Thank you for your help on this. I noticed you used `JLayeredPane.DEFAULT_LAYER` when adding the graphic. I was just wondering what is best to use in the case that I want to add additional graphics? –  Oct 29 '11 at 12:40
  • I would use whatever logic dictates to work the best. If I have something that I know will be on the bottom, I'll put it in the Default layer. – Hovercraft Full Of Eels Oct 29 '11 at 12:42
  • Ok that makes sense. How would I go about adding a layer so that its on top of everything? –  Oct 29 '11 at 13:44
  • @JonW09: it just has to have the highest Integer(...) constant when adding it to the JLayeredPane. Also check out the JLayeredPane API for the default constants that Swing uses and that you can use too. – Hovercraft Full Of Eels Oct 29 '11 at 17:25
1

See Laying Out Components in a Layered Pane, from The Java Tutorials.

Also, sometimes you need to set the preferred size:

layeredPane.setPreferredSize(new Dimension(width, height));

The Aviv
  • 345
  • 1
  • 2
  • 11