Search code examples
javaclassactionlistenerpaintcomponentpublic

Drawing a 2D rectangle from Jtextfield inputs for GUI in Java


I am currently developing a GUI in Java. I am trying to display a 2D rectangle in a Jpanel called drawpanel in this case. In order to do this, I have created a class called die and initialized the paintComponent and an actionPerformed method. In the actionPerformed I have parsed the doubles from the Jtextfields which will be the inputs of my 2D rectangle. Then I proceeded to draw the rectangle and finally attach it to drawpanel. The problem is that drawpanel.draw(rect) is giving me an error, specifically in draw(rect). This error prevents it from drawing a 2D rectangle. I was wondering if anyone could please help me identify the issue I am running into here. In addition, I have a couple of questions:

  • Are my overrides placed in the correct line (location)?
  • Is there a simpler way of arriving to the same outcome I have planned? Thanks in advance! Any input is much appreciated. I have attached my code below. It is not the entire code, since the GUI I have developed is quite long. But if more code is required in order to understand the question feel free to ask for it!
class die extends JPanel implements ActionListener{
  @Override
  protected void paintComponent(Graphics g){
  super.paintComponent(g);
 
  }
  @Override
  public void actionPerformed(ActionEvent e) {
    //parsing double values from jtextfields
  
    double xcor = Double.parseDouble(xcoorin.getText()); 
    double ycor = Double.parseDouble(ycoorin.getText());
    double lengthdie = Double.parseDouble(Die_L.getText());
    double widthdie = Double.parseDouble(Die_W.getText());
    //drawing the rectangle
    Rectangle2D rect = new Rectangle2D.Double(xcor, ycor, widthdie, lengthdie);
    drawpanel.draw(rect);
    
  }
}

ActionListener listen = new die();
locate.addActionListener(listen);

In order to explain where drawpanel comes from:

It is a Jpanel created by inheriting the method Kpackage which is a 2D Drawing created. The code for the drawpanel being added is below:

public class MyFrame{

public MyFrame(){

//Creating new panels for the content window "Frame"
JPanel cyanpanel = new JPanel();
JPanel orangepanel = new JPanel();
JPanel greenpanel = new JPanel();
JPanel magentapanel = new JPanel();
JPanel whitepanel = new JPanel();
JPanel graypanel = new JPanel();
JPanel drawpanel = new Kpackage(); 

And then, the code for Kpackage() is here:

public class Kpackage extends JPanel{
   @Override
   public void paintComponent(Graphics g) {
       super.paintComponent(g);
       Graphics2D g2d = (Graphics2D) g;
       g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
       Dimension size = this.getSize();

//Dimensions and variables for the package
       double scale = 30; //scale of the k package

       double aTO_4L = 1.42*scale;
       double bTO_4L = 0.30*scale;
       double cTO_4L = 5.99*scale;
       double dTO_4L = 3.00*scale;
       double d1TO_4L = 1.5*scale;
       double d2TO_4L = 2.60*scale;
       double eTO_4L = 7.26*scale;
       double fTO_4L = 0.64*scale;
       double gTO_4L = 7.18*scale;
       double hTO_4L = 0.38*scale;
       double iTO_4L = 7.79*scale;
       double jTO_4L = 13.26*scale;
       double kTO_4L = 3.78*scale;
       double dia_inner = 3.58*scale;
       double dia_outer = 7.18*scale;
       double tot_width=jTO_4L+2*hTO_4L;
       double tot_height=iTO_4L+eTO_4L+d1TO_4L;
       double x = (size.width/2);
       double y = (size.height/2);

//creating the K package envelope

     g2d.draw(new Line2D.Double(x-cTO_4L/2-aTO_4L, y-eTO_4L-d1TO_4L, x-cTO_4L/2,y-eTO_4L-d1TO_4L ));
     g2d.draw(new Line2D.Double( x-cTO_4L/2,y-eTO_4L-d1TO_4L ,x-cTO_4L/2,y-eTO_4L-d1TO_4L +bTO_4L));
     g2d.draw(new Line2D.Double(x-cTO_4L/2,y-eTO_4L-d1TO_4L +bTO_4L,x-cTO_4L/2+cTO_4L,y-eTO_4L-d1TO_4L+bTO_4L));
     g2d.draw(new Line2D.Double(x-cTO_4L/2+cTO_4L,y-eTO_4L-d1TO_4L+bTO_4L,x-cTO_4L/2+cTO_4L,y-eTO_4L-d1TO_4L));
     g2d.draw(new Line2D.Double(x-cTO_4L/2+cTO_4L,y-eTO_4L-d1TO_4L,x-cTO_4L/2+cTO_4L+aTO_4L,y-eTO_4L-d1TO_4L));
     g2d.draw(new Line2D.Double(x-cTO_4L/2+cTO_4L+aTO_4L,y-eTO_4L-d1TO_4L,x-cTO_4L/2+cTO_4L+aTO_4L+d2TO_4L,y-eTO_4L-d1TO_4L+d1TO_4L));
     g2d.draw(new Line2D.Double(x-cTO_4L/2+cTO_4L+aTO_4L+d2TO_4L,y-eTO_4L-d1TO_4L+d1TO_4L,x-cTO_4L/2+cTO_4L+aTO_4L+d2TO_4L,y-eTO_4L-d1TO_4L+d1TO_4L+eTO_4L));
     g2d.draw(new Line2D.Double(x-cTO_4L/2+cTO_4L+aTO_4L+d2TO_4L,y-eTO_4L-d1TO_4L+d1TO_4L+eTO_4L,x-cTO_4L/2+cTO_4L+aTO_4L+d2TO_4L-hTO_4L,y-eTO_4L-d1TO_4L+d1TO_4L+eTO_4L));
     g2d.draw(new Line2D.Double(x-cTO_4L/2+cTO_4L+aTO_4L+d2TO_4L-hTO_4L,y-eTO_4L-d1TO_4L+d1TO_4L+eTO_4L,x-cTO_4L/2+cTO_4L+aTO_4L+d2TO_4L-hTO_4L,y-eTO_4L-d1TO_4L+d1TO_4L+eTO_4L+iTO_4L));
     g2d.draw(new Line2D.Double(x+jTO_4L/2,y-eTO_4L-d1TO_4L+d1TO_4L+eTO_4L+iTO_4L,x-jTO_4L/2,y-eTO_4L-d1TO_4L+d1TO_4L+eTO_4L+iTO_4L));
     g2d.draw(new Line2D.Double(x-jTO_4L/2,y-eTO_4L-d1TO_4L+d1TO_4L+eTO_4L+iTO_4L,x-jTO_4L/2,y-eTO_4L-d1TO_4L+d1TO_4L+eTO_4L));
     g2d.draw(new Line2D.Double(x-jTO_4L/2,y-eTO_4L-d1TO_4L+d1TO_4L+eTO_4L+iTO_4L,x-jTO_4L/2,y-eTO_4L-d1TO_4L+d1TO_4L+eTO_4L));
     g2d.draw(new Line2D.Double(x-jTO_4L/2,y,x-jTO_4L/2-hTO_4L,y));
     g2d.draw(new Line2D.Double(x-jTO_4L/2-hTO_4L,y,x-jTO_4L/2-hTO_4L,y-eTO_4L));
     g2d.draw(new Line2D.Double(x-jTO_4L/2-hTO_4L,y-eTO_4L,x-jTO_4L/2-hTO_4L+d2TO_4L,y-eTO_4L-d1TO_4L));
     g2d.draw(new Ellipse2D.Double(x-dia_inner/2,y-dia_inner/2-kTO_4L,dia_inner,dia_inner));
     g2d.draw(new Ellipse2D.Double(x-dia_outer/2,y-dia_outer/2-kTO_4L,dia_outer,dia_outer));

//creating the 4 leg leads 

//variables and names
    
    double D_lead = 2.529*scale;
    double S_lead = 4.770*scale;
    double K_lead=1.5*scale;
    double G_lead=2.080*scale;


    double S_p1=1.1*scale;
    double S_p2=4.270*scale;
    double S_p3=0.940*scale;
    double S_p4=2.630*scale;
    double S_rad=0.5*scale;
    
    double lead_w=1.2*scale;
    double lead_h=1.6*scale;

    double K_p1=0.32*scale;
    double K_p2=1.5*scale;
    double K_p3=0.620*scale;
    
    double G_p1=0.880*scale;

    double length_lead=3.0*scale;

    double dist_1=0.178*scale;
    double dist_2=0.450*scale;
    double dist_3=0.720*scale;
    double dist_4=0.4*scale;

 //Creating the lead positions and dimensions

 //Drawing D LEAD

 g2d.draw(new Line2D.Double(x-jTO_4L/2,y+iTO_4L,x-jTO_4L/2,y+iTO_4L+length_lead));
 g2d.draw(new Line2D.Double(x-jTO_4L/2+D_lead,y+iTO_4L,x-jTO_4L/2+D_lead,y+iTO_4L+length_lead));

  //Drawing G LEAD
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2,y+iTO_4L+dist_1,x+jTO_4L/2-dist_2,y+iTO_4L+dist_1+lead_h));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2,y+iTO_4L+dist_1+lead_h,x+jTO_4L/2-dist_2-G_p1,y+iTO_4L+dist_1+lead_h));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2,y+iTO_4L+dist_1,x+jTO_4L/2-dist_2-G_lead,y+iTO_4L+dist_1));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead,y+iTO_4L+dist_1,x+jTO_4L/2-dist_2-G_lead,y+iTO_4L+dist_1+length_lead));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_p1,y+iTO_4L+dist_1+lead_h,x+jTO_4L/2-dist_2-G_p1,y+iTO_4L+dist_1+length_lead));


 //Drawing K LEAD
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3,y+iTO_4L+dist_1,x+jTO_4L/2-dist_2-G_lead-dist_3,y+iTO_4L+dist_1+lead_h));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3,y+iTO_4L+dist_1,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2,y+iTO_4L+dist_1));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2,y+iTO_4L+dist_1,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2,y+iTO_4L+dist_1+lead_h));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3,y+iTO_4L+dist_1+lead_h,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p3,y+iTO_4L+dist_1+lead_h));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p3,y+iTO_4L+dist_1+lead_h,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p3,y+iTO_4L+dist_1+length_lead));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2,y+iTO_4L+dist_1+lead_h,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1,y+iTO_4L+dist_1+lead_h));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1,y+iTO_4L+dist_1+lead_h,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1,y+iTO_4L+dist_1+length_lead));

 //Drawing S LEAD

 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4,y+iTO_4L+dist_1+lead_h,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4,y+iTO_4L+dist_1));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4,y+iTO_4L+dist_1,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4-S_p2,y+iTO_4L+dist_1));

 //connect before end point and after start point
 g2d.draw(new Arc2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4-S_rad-S_p2,y+iTO_4L+dist_1,2*S_rad,2*S_rad,90, 90,Arc2D.OPEN));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4-S_rad-S_p2,y+iTO_4L+dist_1+S_rad,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4-S_rad-S_p2,y+iTO_4L+dist_1+lead_h));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4,y+iTO_4L+dist_1+lead_h,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4-S_p3,y+iTO_4L+dist_1+lead_h));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4-S_p3,y+iTO_4L+dist_1+lead_h,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4-S_p3,y+iTO_4L+dist_1+length_lead));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4-S_rad-S_p2,y+iTO_4L+dist_1+lead_h,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4-S_rad-S_p2+S_p4,y+iTO_4L+dist_1+lead_h));
 g2d.draw(new Line2D.Double(x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4-S_rad-S_p2+S_p4,y+iTO_4L+dist_1+lead_h,x+jTO_4L/2-dist_2-G_lead-dist_3-K_p2-K_p1-dist_4-S_rad-S_p2+S_p4,y+iTO_4L+dist_1+length_lead));

   }}

Solution

  • To expand on my comment. To make this work you need to do several things:

    1. Add a draw method to the Kpackage class that accepts a Rectangle2D as an input like so public void draw(Rectangle2D rect){...}, this will fix your initial error.

    2. Create a class variable inside your Kpackage class that can store rectangles ArrayList<Rectangle2D > rectangles = new ArrayList<>();

    3. In the draw method save the inputted rectangle to the ArrayList, then call repaint();

    4. Now the trick here is that the paintComponent method MUST loop the list of rectangles in the ArrayList and draw each one accordingly.

    Here is what the Kpackage class might look like if we did all of the above:

    public class Kpackage extends JPanel{
        //List of added rectangles
        private ArrayList<Rectangle2D > rectangles = new ArrayList<>();
        
        //Draw method to insert a new rectangle
        public void draw(Rectangle2D rect){
            //Save the new rectangle to the list
            rectangles.add(rect);
            //Trigger a repaint which will call the paintComponent method and draw all the rectangles
            repaint();
        }
        
        //Overridden method that actually pantis the rectangles
        @Override
        public void paintComponent(Graphics g){
            //Call super to paint the JPanel before anything else
            super.paintComponent(g);
            
            //Get the 2D graphics
            Graphics2D g2d = (Graphics2D) g;
    
            //Now paint any borders or custom items
            //...
            //your code here
            //etc
            
            //Finally paint the rectangles saved in the list
            for (Rectangle2D rectangle : rectangles){
                //Draw a basic rectangle, just as proof that this works
                g2d.draw(rectangle);
                
                //You will actually do custom drawing scaled to fit your Kpackage here
                //But I removed it for clarity, you can play with this section to make it work
                //Get sizes and scales
                //...
    
                //Draw sides
                //...
    
                //Fill rectangle
                //...
    
                //etc, etc
            }
        }
    }
    

    And usage would be exactly as you already have it inside your actionPerformed method:

    //drawing the rectangle
    Rectangle2D rect = new Rectangle2D.Double(x, y, width, length);
    drawpanel.draw(rect);