Q:

How can I create a resizable bar with given instance variables and it two different colors set to it, red for (a) and green for (b)?

When I call the paintComponent to the frame I get a graphical display that is not resizable. How do I get the paintcomponent to paint a resizable object to the frame that will resize with the window using the given instance variables?
public class Program3Component extends JComponent
{
    private static final long serialVersionUID = 5449209515657047425L;

        private final int yTp = 20;
        private final int xLt = 10;
        private final int xRt = 10;
        private final int yBtm = 40;

        public void paintComponent(java.awt.Graphics g)
        {

            Graphics2D g2 = (Graphics2D)g;

            Rectangle bar = new Rectangle(10,10,300,70);
            g2.draw(bar);
            g2.setColor(Color.GREEN);
            g2.setColor(Color.RED);

        }

A:

You use the size of the component you are painting, to determine the coordinates of your bar.
For example, if you want a bar that is 10 pixels away from each side of the component:
    public void paintComponent(java.awt.Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D)g;

        Rectangle bar = new Rectangle(10,10,getWidth() - 20, getHeight() - 20 );
        g2.setColor(Color.GREEN);            
        g2.draw(bar);

    }

The reason to deduct 20 from the width (or height) is that the width of the bar is the width of the window, but with a margin of 10 on both sides.

