'Rectangle is not showing up (paint) java

I've done this plenty of times but I'm stuck. I've checked previous projects of mine and can't find the answer. The rectangle is supposed to show up at 400,400 and be 100,100 big, (Bottom right corner). When the start button is press I want it to show the rectangle. Thanks in advance!

!!!: There are multiple classes in this I just didn't post them since they don't have a use, if they do I'll post them.

class MazeRunner extends JFrame implements ActionListener, MouseMotionListener {
    JButton b1;
    int  pkp = 0;

    MazeRunner(){
        b1= new JButton("Start");
        add(b1);
        b1.addActionListener(this);
        b1.setBounds(10,10,50,50);
        addMouseMotionListener(this);
        setTitle("Maze Runner");
        setLayout(null);
        setSize(500, 500);
        setResizable(false);
        setVisible(true);
    }

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

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == b1){ 
            remove(b1);
            pkp++;
            validate();
            repaint();
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        if (pkp == 1) {
            if(e.getX() >= 400 && e.getX() <= 500 && e.getY() >= 400 && e.getY() <= 500) {
                dispose();
            }
        }
    }

    protected void paintComponent(Graphics g) {
        draw(g);
    }

    private void draw(Graphics g) {
        if(pkp == 1) {
            g.setColor(Color.BLACK);
            g.fillRect(400, 400, 100, 100);
        }
        repaint();
    }
}


Solution 1:[1]

First of all the method paintComponent() is missing it's constructor so it should look like

public void paintComponent(Graphics g){
    super.paintComponent(g);
    draw(g);
}

second of all, im not sure if Graphics g object has a method called "fillRect" so I would recommend you to convert it to object of type Graphics2D, so in this case it would be:

public void draw(Graphics g){
    Graphics2D g2 = (Graphics2D) g;
    if(pkp == 1) {
        g2.setColor(Color.BLACK);
        g2.fillRect(400, 400, 100, 100);
    }
    repaint();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pullolo