Hi, I'd like to ask you a few questions about a things which don't work right now in my (very simple) graphic interface. The code I have:

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

public class Simulation{

    public Simulation() {
        JFrame frame = new JFrame();
        frame.setSize(1200,900);
        frame.setTitle("Traffic simulator");
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        JPanel background = new JPanel(new GridBagLayout());
        background.setBackground(Color.lightGray);

        RoadSystem roads = new RoadSystem();
        background.add(roads, new GridBagConstraints(0,0,1,3,3.0,1.0,
                                                         GridBagConstraints.WEST, GridBagConstraints.NONE,
                                                         new Insets(10, 10, 10, 10), 0, 0));

        Stats stats = new Stats();
        background.add(stats, new GridBagConstraints(1,0,1,1,1.0,1.0,
                                                        GridBagConstraints.WEST, GridBagConstraints.NONE,
                                                        new Insets(10, 10, 10, 10), 0, 0));

        Settings settings = new Settings();
        background.add(settings , new GridBagConstraints(1,1,1,1,1.0,1.0,
                                                         GridBagConstraints.WEST, GridBagConstraints.NONE,
                                                         new Insets(10, 10, 10, 10), 0, 0));

        JButton startButton = new JButton();
        startButton.setSize(185,10);
        background.add(startButton, new GridBagConstraints(1,2,1,1,1.0,0.2,
                                                        GridBagConstraints.WEST, GridBagConstraints.NONE,
                                                         new Insets(10, 10, 10, 10), 0, 0));

        frame.add(background);

    }

    public static void main(String[] args) {
        new Simulation();
    }
}

////////////////////////////////////////////////////////////////////

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

public class RoadSystem extends JPanel {

    ArrayList<Road> roads = new ArrayList<> ();

    public RoadSystem() {
        setBackground(new Color(0,140,0));
        setMinimumSize(new Dimension(1000,800));
        setPreferredSize(new Dimension(1000,800));
    }

}

///////////////////////////////////////////////////////////////

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

public class Road extends JComponent {
    Point2D start, end;

    public Road(int x, int y, int x_k, int y_k) {
        start = new Point2D.Double(x, y);
        end = new Point2D.Double(x_k, y_k);
    }

    @Override
    public void paintComponents(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.BLACK);
        g.fillRect((int)start.getX(), (int)start.getY(), 100, 1000);
        g.fillRect(10, 10, 100, 1000);
    }

}

/////////////////////////////////////////////////////////////////

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

public class Settings extends JPanel {
    public Settings() {
        setBackground(Color.yellow);
        setMinimumSize(new Dimension(185,300));
        setPreferredSize(new Dimension(185,300));
    }
}

/////////////////////////////////////////////////////////////

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

public class Stats  extends JPanel {
    public Stats() {
        setBackground(Color.red);
        setMinimumSize(new Dimension(185,300));
        setPreferredSize(new Dimension(185,300));
    }
}

The problems I have:

  1. When I run program I see just empty frame, I have to minimalize it and maximalize again and then I see all panels etc. It's quite weird - resizing helps as well and I don't know what is the problem, so I would be glad for any suggesions.

  2. At first I had all JPanels resizable, now I set a fixed size of everything and blocked resizing of the frame and it all looks very weird - I can't make it look as good as before:
    Before
    Now
    Can you help me or say how to make it better to have fized size of everythink and to make it symetric and good looking?

  3. I wanted to draw on green JPanel rectangles and I tried to do it a few ways - nothing worked. I have paintComponents function in Road class, can you help me and show how to paint this rectangle inside green JPanel (RoadSystem)? I would like the panel and everything in it to be separate entity with green background and shapes inside.

1 At startup you are updating the content of a visible component on a non-swing thread - use invokeLater etc

2 Don't use fixed sizes. That destroys your portability.

3 see other post... paintComponent (no s)