Hi,
I want to draw a rectangle but I want a user to specify its dimensions.
Currently, I have 4 JTextAreas that take x, y, width and height values.
I also have a button. I know how to display those values etc but I don't know how to link them to my draw method.
I spend a lot of time trying to solve it so any help is appreciated.

The code:

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;  // Needed for ActionListener

class MyGui4 extends JFrame {

    private JTextField x   = new JTextField(3);
    private JTextField y   = new JTextField(3);
    private JTextField width   = new JTextField(3);
    private JTextField height   = new JTextField(3);
    
    private JPanel p = new JPanel();	//draw rectangle here
    
    public MyGui4() {                                  

        JButton b = new JButton("Draw"); 
        b.addActionListener(new DrawRectangle());

        x.addActionListener(new DrawRectangle());
        y.addActionListener(new DrawRectangle());
        width.addActionListener(new DrawRectangle());
        height.addActionListener(new DrawRectangle());
        
        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());

        content.add(new JLabel("X"));
        content.add(x);             
        content.add(new JLabel("Y"));
        content.add(y);             
        content.add(new JLabel("Width"));
        content.add(width);          
        content.add(new JLabel("Height"));
        content.add(height);         
        
        content.add(b);   
        
        content.add(new JLabel("Your Rectangle"));
        content.add(p);				

        setContentPane(content);
        pack();                           
        setTitle("Rectangel Painter");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);      
    }

    class DrawRectangle implements ActionListener {        
        public void actionPerformed(ActionEvent e) {
        	
            String temp1 = x.getText();                
            int xVar = Integer.parseInt(temp1);  
            String temp2 = y.getText();                
            int yVar = Integer.parseInt(temp2);
            String temp3 = width.getText();                
            int widthVar = Integer.parseInt(temp3);
            String temp4 = height.getText();                
            int heightVar = Integer.parseInt(temp4);
            
            Rectangle rect = new Rectangle(xVar,yVar,widthVar,heightVar);
            
            rect.getX();
            rect.getY();
            rect.getWidth();
            rect.getHeight();
            //?????
            //rect(p);
        }
    }

    public static void main(String[] args) {
        MyGui4 window = new MyGui4();
        window.setVisible(true);
    }
}

how to link them to my draw method.

Normally you draw in the paintComponent() method for the GUI component that is to show the drawing.
The GUI collects the input from the user (rect coordinates) and waits for the user to press a button. The action listener for the button then edits the data and calls repaint() to request the JVM to call the paint method. When the paint method is called, it takes the data edited by the listener and draws.

^ like Norm says. Plus:
When you get the rect values you create a new Rectangle local to the ActionPerformed method, but you don't do anything with it. It will go out of scope when the method terminates, and it will be garbage collected.
One thing you could do is to declare that Rectangle somewhere where both the ActionPerformed and your "draw" method can see it, so one can set the values and the other can use them. (Although as a general principle, passing such things as parameters is usually preferable to having shared or "global" variables. You haven't posted your draw method, so it's hard to say.)

Thanks for your replies. Gonna try to do it again.

Hi, I've tried to improve the code (I applied your advices) but it is still not working. I seriously have not idea what I'm doing wrong.

It does take the values of x,y,width,height from text fields when you press the button (I made it print to check) but it does not use them to draw the rectangle (rectangle is not drawn at all even while using the initial values). I made the x,y,width,height public and initialized them as you said so that they can be seen by both actionPerformed and paintComponent.

the gui:
JPanel content has 4 labels&textfields, button and JPanel panel
JPanel panel has label and a space to show the rectangle.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.*;
import javax.swing.*;


public class NewGui extends JFrame implements ActionListener  {

	public JTextField xtf;
	public JTextField ytf;
	public JTextField wtf;
	public JTextField htf;
	public JButton b;
	public JFrame f;
	public JPanel content;
	public JPanel panel;
	//init values for rectangle
	public int x=20;	
	public int y=20;
	public int width=40;
	public int height=50;
	
	public static void main(String[] args) {
		NewGui window = new NewGui();
		window.setVisible(true);
	}
	
	//draw rectangle
	public void paintComponent(Graphics g) {
		super.paintComponents(g);
	    Graphics2D g2 = (Graphics2D)g;
	    g2.drawRect(x,y,width,height);
	}
	
	//when button is pressed
	//get values from text fields and assign them to x, y width, height
	//call repaint
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == b) { 
			
			String s = xtf.getText();
			x = Integer.parseInt(s);
			String s1 = ytf.getText();
			y = Integer.parseInt(s1);
			String s2 = wtf.getText();
			width = Integer.parseInt(s2);
			String s3 = htf.getText();
			height = Integer.parseInt(s3);
			
			repaint(); 
		}
    }

	//create gui
	public NewGui() {
		//f = new JFrame();
		
		/*this.setSize(500,500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.add(new PaintSurface(),BorderLayout.CENTER);
		this.setVisible(true);
		*/
		
		content = new JPanel();
		panel = new JPanel();
		xtf = new JTextField(5);
		ytf = new JTextField(5);
		wtf = new JTextField(5);
		htf = new JTextField(5);
		b = new JButton("Draw");
		
		xtf.addActionListener(this);
		ytf.addActionListener(this);
		wtf.addActionListener(this);
		htf.addActionListener(this);
		b.addActionListener(this);
		
		content.setLayout(new FlowLayout());
		content.add(new JLabel("X"));
		content.add(xtf);
		content.add(new JLabel("Y"));
		content.add(ytf);
		content.add(new JLabel("Width"));
		content.add(wtf);
		content.add(new JLabel("Height"));
		content.add(htf);
		content.add(b);
		
		panel.setPreferredSize(new Dimension(500, 500));
		panel.add(new JLabel("your rectangle"));
		content.add(panel);
		setContentPane(content);
		//setContentPane(panel);		
		pack();
	    setTitle("Rectangle Painter");
	    
	    setLocationRelativeTo(null);  
	}
}

Are you sure that the pen doing the drawing is a different color than the background?
White pen on white paper is hard to see.

If I change the color of the pen (I did before) nothing happens anyway.

Your paintComponent method should be for a JPanel that is added to the JFrame.
You can't override the JFrame's paint method because all the components are contained in the JFrame.
Create a JPanel, override its paintComponent method and add that to the JFrame.
Call that component's repaint()

I've done it. Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.