Search code examples
javaswingpaintcomponentrectangles

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,

Length and width

Then I press go:

Dialogue

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();
     }
}

Solution

  • 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

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