0

I'm new here. Been reading questions and answers for aeons. Recently I found time to start studying Java, and I'm seriously enjoying the process. Until I started to write some code. The getting stuck is killing me. So I've come to seek advice on something extremely simple but I cannot crack it.

The code below attempts to create a frame, maximize it, and place elements inside. I was just fooling around. First the button1, I tried to change its size (so I got it into a FlowLayout). Then a button in the mainPanel, just to... try. Then an oval. I tried for 2 hours to get the oval to display but it is Impossible. When I found about "drawOval" I thought that was it but it made no difference. And to think that I was planning for the button1 to create Moving Balls. I'm so, So far away from that.

Please, why does the silly Oval refuse to display itself. Help.

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

public class MainClass {
    JFrame frame = new JFrame();
    JPanel mainPanel = new JPanel();
    JPanel southPanel = new JPanel();
    JButton button1 = new JButton("Button1");
    JButton button2 = new JButton("Button2");
    Oval oval = new Oval();

    public static void main(String[] args) {
        MainClass program = new MainClass();
        program.go();
    }

    public void go() {
        buildGUI();
    }

    public void buildGUI() {
        button1.setBorder(BorderFactory.createMatteBorder(2,2,2,2, Color.BLACK));
        button1.addActionListener(new Button1Listener());

        frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
        button1.setPreferredSize(new Dimension(200, 50));
        frame.getContentPane().add(BorderLayout.SOUTH, southPanel);
        southPanel.add(button1);
        mainPanel.add(button2);
        mainPanel.add(oval);
        frame.setSize(400, 400);
        frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    class Button1Listener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            //What the button will do.
        }
    }
}

And the Oval part

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

public class Oval extends JPanel {
    public void paintComponent(Graphics g) {
        g.setColor(Color.orange);
        g.fillOval(20, 50, 100, 100);
    }
}

1 Answers1

0

JPanel uses a FlowLayout by default. Since you Oval class doesn't provide any sizing hints, then it's set to a default size of 0x0.

Start by updating your class to something more like...

public class Oval extends JPanel {

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(120, 150);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.orange);
        g.fillOval(20, 50, 100, 100);
    }
}

to create Moving Balls.

Ok, you might want to have a look at:

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you Divine Entity of Clarification and Unstuckness. Really sorry for the noobness but just to learn: in the fillOval(20,50,100,100) part aren't those 100 the size I'm indicating the Oval to have? And if you'd leave me a note on why the super.paintComponent(g) I'd be super happy. – Maximiliano Gamón Jan 09 '20 at 03:58
  • The size of the oval only dictates how large to paint the oval, not the size the component would like to be, you can paint beyond the visible range of the component (as you have done). Unless you understand what `paintComponent` does and are willing to take over it's functionality, call it's super implementation – MadProgrammer Jan 09 '20 at 05:31