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?