Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my panels not showing up in the JFrame

public class InputPanel extends JPanel{
    public static int shapeType; //1: Rectangle; 2: Oval; 3: Line
    public static boolean isFilled; //whether or not the shape is filled
    public static Color color; //color of the shape

    public InputPanel(){

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.setBackground(Color.GRAY);
        setPreferredSize(new Dimension(200,500));

        JButton rect = new JButton("Rectangle");
        JButton oval = new JButton("Oval");
        JButton line = new JButton("Line");
        JRadioButton fill = new JRadioButton("Filled:");
        JButton color1 = new JButton("Color..");

        rect.addActionListener(new rectListener());
        oval.addActionListener(new ovalListener());
        line.addActionListener(new lineListener());
        isFilled = fill.isSelected();
        color1.addActionListener(new colorListener());

        panel.add(rect);
        panel.add(oval);
        panel.add(line);
        panel.add(fill);
        panel.add(color1);

        this.setVisible(true);

    }
}



public class PaintPanel extends JPanel{

    public static int x1, y1, x2, y2;

    ArrayList<Shape> shapeList = new ArrayList<Shape>();

    public PaintPanel(){

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel.setBackground(Color.WHITE);
        setPreferredSize(new Dimension(500, 500));

        this.addMouseListener(new MouseAdapter() {
        @Override public void mousePressed(MouseEvent e) {
            PaintPanel.x1 = e.getX();
            PaintPanel.y1 = e.getY();
        }

        @Override public void mouseReleased(MouseEvent e) {
            PaintPanel.x2 = e.getX();
            PaintPanel.y2 = e.getY();
            if(InputPanel.shapeType == 1){
                shapeList.add(new Rectangle(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2, InputPanel.isFilled));
            }
            if(InputPanel.shapeType == 2){
                shapeList.add(new Oval(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2, InputPanel.isFilled));
            }   
            if(InputPanel.shapeType == 3){
                shapeList.add(new Line(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2));
            }
            repaint();
        }
      });

     this.setVisible(true);   
    }

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

        for(Shape s : shapeList){
            s.draw(g);
        }
    }

}

public class PaintGUI {

    public static void main(String[] args){

        JFrame frame = new JFrame("Shape Drawer!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(new InputPanel());
        frame.add(new PaintPanel());

        frame.pack();

        frame.setVisible(true);

    }

}

I'm positive I've created the JFrame properly and all of my other classes work, but there must be something in here I'm missing... When I run the main method all I get is a gray box that is clearly a square (500x500, as instantiated in the PaintPanel class. What am I doing wrong?

like image 863
user1787458 Avatar asked Dec 03 '25 12:12

user1787458


1 Answers

Apart from what Andrew mentioned, I noticed that within both your InputPanel and PaintPanel you're creating a new JPanel. You're adding new components to this panel, for sure, but at the end you're not adding this JPanel itself to your InputPanel or PaintPanel. So, make sure that in your constructors for these panels you have a add(panel) at the end.

Also, as a side note, do please keep in mind that most operations in Swing are not thread-safe and so read about "Concurrency in Swing" before creating/interacting with UI components. In other words, any updates to the user interface must happen on the event dispatch thread, like the start-up of your application:

public static void main(String[] args){

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame("Shape Drawer!");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //set the layout, add your panels

            frame.pack();
            frame.setVisible(true);             
        }
    });
}
like image 166
Sujay Avatar answered Dec 06 '25 05:12

Sujay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!