Search code examples
javaswingjpanelpaintcomponent

Pass a JPanel class to the main class in JAVA


I need to pass a JPanel extending class to the main class.

Here is what I have so far: Main class

import java.awt.Color;
import javax.swing.*;

public class main {

    private gamePanel gamepanel = new gamePanel();

    public JPanel createContentPane(){
        // We create a bottom JPanel to place everything on.
        JPanel totalGUI = new JPanel();

        //We set the Layout Manager to null so we can manually place
        // the Panels.
        totalGUI.setLayout(null);

        //Now we create a new panel and add it to the bottom JPanel.

        totalGUI.add(gamepanel);



        totalGUI.setOpaque(true);
        return totalGUI;
    }

    private static void createAndShowGUI(){
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] There's a JPanel in here! [=]");

        //Create and set up the content pane.
        main demo = new main();
        frame.setContentPane(demo.createContentPane());

        //The other bits and pieces that make your program a bit more stable.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700,500);
        frame.setVisible(true);

    }
    public static void main(String[] args) {
        //Schedule a jog for the event-dispatching thread:
        //creating and showing this application's GUI.
        System.out.println(gamepanel);
        SwingUtilities.invokeLater(new Runnable() {
            public void run(){
                createAndShowGUI();
            }
        });
    }
}

The gamePanel class

public class gamePanel extends JPanel implements Commons {

    private Dimension d;
    private ArrayList snowmens;
    private coreFunctions functions = new coreFunctions();
    private int snowmenX = 150;
    private int snowmenY = 5;
    private final String snowmenpix = "snowman.png";
    private JPanel background;

public gamePanel() {



    add(new JLabel("TEST"));
    setFocusable(true);


    setDoubleBuffered(true);
}


@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, 700, 700);



}
}

I can't figure out why the blue background and the label is not being shown...

EDIT:

Here are more details:

Ok so I am trying to make a little 2D game. For that I need to create some snowmen on the gamePanel class and display it via the main class. To start it off, the createContentPane creates a background panel, the root panel if you want. The createandshowgui creates a JFrame.

The gamepanel class is in fact a JPanel which has 1 panel as of now, which is the background panel. For now, I only want it to have a blue background.

I tried putting it like this because I saw some examples and they were pretty similar to what I have, but for some reason, I can't get it to work....

Thank you, Ara


Solution

  • You should use LayoutManager instead of setLayout(null); and if only one component is being added no need for it either (that component will stretch to fill unless others are added)

    see here for tutorial on LayoutManagers:

    If all you are trying to do is have a blue background (on which you can add components) then simply override paintComponent() of your gamePanel and paint the background blue:

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLUE);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
        }
    

    then you can add your JLabel as if it was a normal Panel as the background is now being painted blue and not set by/as a component.

    If you have an image look into g.drawImage(Image image,int x,int y,ImageObserver iO)

    EDIT

    DROP THE CALL TO:

    setLayout(null);

    and it will work