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

public class TextInputExample extends JPanel {
  // x, y, width, height
  private Rectangle mBox = 
    new Rectangle(100, 100, 20, 30);

  // text fields for the x and y coordinates
  private JTextField mXField = new JTextField(5);
  private JTextField mYField = new JTextField(5);

  // a button to allow the user to update the
  // rectangle location
  private JButton mMoveButton = 
    new JButton("Move");

  public TextInputExample() {
    super();

    mMoveButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          // reset the coordinates of mBox
          int x = Integer.parseInt(mXField.getText());
          int y = Integer.parseInt(mYField.getText());
          mBox.setLocation(x, y);
          repaint(); // ask the JPanel to refresh itself
        } 
      });

    // set up the panel
    add(new JLabel("x = ")); add(mXField);
    add(new JLabel("y = ")); add(mYField);
    add(mMoveButton);
  }

  public void paintComponent(Graphics g) {
    // first let the superclass erase the old contents
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.draw(mBox); // now draw our box
  }


  public static void main(String [] args) {
    JFrame myFrame = new JFrame();
    myFrame.getContentPane().add(new TextInputExample());
    myFrame.setSize(300,300);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setVisible(true); 
  }
}
