Stack Exchange
Draw a rectangle with the corresponding size taken from a TextField
I have got a program where someone enters a width and a length and it draws the corresponding size on a dialog.
The Scenario
Here is what happens. I start it up, 

Then I press go:

And the dialogue comes up with nothing.
The code
So here is the event handling code:
public void actionPerformed(ActionEvent e){
d.init();
}

d is the class that shows the dialogue. I didn't think I would show it but all the init does is add a DrawRectangle panel to it (this is the DrawRectangle` class):
import java.awt.*;
import javax.swing.*;

public class DrawRectangle extends JPanel{
   int x = 100;
    int y = 50;
    int h;
    int w ;

    private void Dodrawing(Graphics g, int w, int h, int x, int y){
        g2d.fillRect(x, y, w, h);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        //Graphics, width, heigth, x coordinate, y coordinate
        Dodrawing(g, w, h, x, y);
    }
}

The question
Can I change the value of h and w to the values in the textfield and then update the drawing?
Edit
Here is an SSCCE:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Area;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SSCCE extends JFrame implements ActionListener{

    //Variable declaration
    JLabel LengthLabel = new JLabel("Length");
    JLabel WidthLabel = new JLabel("Width");
    JLabel Area = new JLabel ();
    JLabel Perimeter = new JLabel ();
    JLabel Volume = new JLabel();
    JTextField Length = new JTextField();
    JTextField Width = new JTextField();
    int LengthInt;
    int WidthInt;

    String LengthStr;
    String WidthStr;

    JDialog dialog;  

Color darkGreen = new Color(50, 150, 50);
JButton close = new JButton("Close");
boolean visi = true;
    JButton go = new JButton("Go");

  public SSCCE(){
      super("Geometry");
   setSize(500, 600);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      GridLayout grid = new GridLayout(20 , 10);

      setLayout(grid);

      add(LengthLabel); 
      add(Length);
      add(WidthLabel);
      add(Width); 
      add(go);
      add(Area);
      add(Perimeter);
      go.addActionListener(this);
     setVisible(true);   
}
  JPanel p = new JPanel();
   int x = 100;
    int y = 50;
    int h ;
    int w ;

    private void Dodrawing(Graphics g, int w, int h, int x, int y){
Graphics2D g2d = (Graphics2D) g;
        g2d.fillRect(x, y, w, h);
    }

    protected void paintComponent(Graphics g)
    {
        p.paintComponents(g);

        //Graphics, width, heigth, x coordinate, y coordinate
        Dodrawing(g, w, h, x, y);
    }

  //Action Peformed method
   public void actionPerformed(ActionEvent e){
       //Getting the text from the input fields
        LengthStr = Length.getText().toString();
       WidthStr = Width.getText().toString();
try{
LengthInt = Integer.parseInt(LengthStr);
WidthInt = Integer.parseInt(WidthStr);
init();
}catch(Exception event){
System.out.println(event);
}    
  }

protected void init() {  

dialog = new JDialog(this, "Copie", true);
dialog.setResizable(false);  

dialog.add(p);  

dialog.pack();  
dialog.setSize(300, 200);
Dimension Size = Toolkit.getDefaultToolkit().getScreenSize();  
dialog.setLocation(new Double((Size.getWidth()/2) - (dialog.getWidth()/2)).intValue(), new Double((Size.getHeight()/2) - (dialog.getHeight()/2)).intValue());  

dialog.setVisible(visi);
}  

protected void close() {  
this.dialog.dispose();  
this.dialog.setVisible(false);  
}  
     public static void main(String[] args){
         SSCCE ge = new SSCCE();
     }
}


From where you got this g2d in this line g2d.fillRect(x, y, w, h); ? Moreover try to override JComponent.getPreferredSize() inside your DrawRectangle class, just like you overriding paintComponent(...) method. As far as I can see, the values of w and h are zero (default initialization), and you never updated those.

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

public class SSCCE extends JFrame implements ActionListener {

    //Variable declaration
    JLabel LengthLabel = new JLabel("Length");
    JLabel WidthLabel = new JLabel("Width");
    JLabel Area = new JLabel();
    JLabel Perimeter = new JLabel();
    JLabel Volume = new JLabel();
    JTextField Length = new JTextField();
    JTextField Width = new JTextField();
    int LengthInt;
    int WidthInt;
    String LengthStr;
    String WidthStr;
    JDialog dialog;
    Color darkGreen = new Color(50, 150, 50);
    JButton close = new JButton("Close");
    boolean visi = true;
    JButton go = new JButton("Go");

    public SSCCE() {
        super("Geometry");
        setSize(500, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GridLayout grid = new GridLayout(20, 10);

        setLayout(grid);

        add(LengthLabel);
        add(Length);
        add(WidthLabel);
        add(Width);
        add(go);
        add(Area);
        add(Perimeter);
        go.addActionListener(this);
        setVisible(true);
    }
    JPanel p = new JPanel();
    int x = 100;
    int y = 50;
    int h;
    int w;

    private void Dodrawing(Graphics g, int w, int h, int x, int y) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.fillRect(x, y, w, h);
    }

    protected void paintComponent(Graphics g) {
        p.paintComponents(g);

        //Graphics, width, heigth, x coordinate, y coordinate
        Dodrawing(g, w, h, x, y);
    }

    //Action Peformed method
    public void actionPerformed(ActionEvent e) {
        //Getting the text from the input fields
        LengthStr = Length.getText().toString();
        WidthStr = Width.getText().toString();
        try {
            LengthInt = Integer.parseInt(LengthStr);
            WidthInt = Integer.parseInt(WidthStr);
            init();
        } catch (Exception event) {
            System.out.println(event);
        }
    }

    protected void init() {

        dialog = new JDialog(this, "Copie", true);
        dialog.setResizable(false);

        dialog.add(p);

        dialog.pack();
        Dimension Size = Toolkit.getDefaultToolkit().getScreenSize();
        dialog.setLocation(new Double((Size.getWidth() / 2) - (dialog.getWidth() / 2)).intValue(), new Double((Size.getHeight() / 2) - (dialog.getHeight() / 2)).intValue());
        dialog.setLayout(new BorderLayout());
        dialog.add(new DrawRectangle(WidthInt, LengthInt));
        dialog.pack();
        dialog.setSize(300, 200);
        dialog.setVisible(visi);
    }

    protected void close() {
        this.dialog.dispose();
        this.dialog.setVisible(false);
    }

    public static void main(String[] args) {
        SSCCE ge = new SSCCE();
    }
}

class DrawRectangle extends JPanel {

    int x = 100;
    int y = 50;
    int h = 100;
    int w = 100;

    DrawRectangle(int w, int h) {
        this.w = w;
        this.h = h;
    }

    private void Dodrawing(Graphics g, int w, int h, int x, int y) {
        g.setColor(Color.RED);
        g.fillRect(x, y, w, h);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println("paintComponent");
        //Graphics, width, heigth, x coordinate, y coordinate
        Dodrawing(g, w, h, x, y);
    }
}

Other tips

Use a consistent and logical indent for code blocks.  The indentation of the code is intended to help people understand the program flow. 
A single blank line of white space in source code is always enough. 
Please learn common Java naming conventions (specifically the case used for the names) for class, method & attribute names & use them consistently. 
Don't set the size of top level containers.  Instead layout the content & call pack().
Java GUIs should be started & updated on the EDT.
Use a JSpinner instead of a text field when the app. requires numbers.


thanks so much for the tips, I don't understand what EDT is.  Also, when I call the pack() method, The container becomes terribly small. But thanks for the tip on a JSpinner because it makes my life 20 times easier. Thanks again

you have to do g.repaint() after g2d.fillRect(x,y,w,h)

I can't do g.repaint();.
