javaclassimportactionlistenerpaintcomponent

How to update 2D rectangle drawing in the respective JPanel


I am running into an issue where I have a working code that is able to display a rectangle in a Jpanel drawpanel given input parameters from Jtextfields. However when trying to create a new rectangle, this new rectangle is added to the drawing. So now I have two rectangles instead of only the updated one. I was wondering if anyone could help me identify why the rectangles are showing up one after the other instead of having it only display one newly updated rectangle. In order to illustrate my question better, here is an image:

The locate button here is what creates the rectangle, but everytime I change parameters a new rectangle is created.

Here is my code for the class die created which extracts doubles from jtextfields and creates a 2Drectangle.

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);
    ((Kpackage) drawpanel).draw(rect);
  }
}

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

Below, is the code that creates the JPanel drawpanel which is where the JPanel is created by inheriting the method Kpackage which is a 2D Drawing. 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(); //inheriting information from my panel which is the 2D drawing
JPanel dieinputpanel = new JPanel();
JPanel diepanel = new JPanel();

The code for Kpackage(); is here below, this code displays the larger drawing that is bounding the smaller rectangles:

public class Kpackage extends JPanel{

private ArrayList<Rectangle2D> rectangles = new ArrayList<>();

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

   @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));


for (Rectangle2D rectangle : rectangles){
  //Draw a basic rectangle, just as proof that this works
 g2d.draw(rectangle);

   }}}

NOTE: This is the continuation of my previous question. The link is here: Drawing a 2D rectangle from Jtextfield inputs for GUI in Java

Thanks for the help! Looking forward to everyone's response.


Solution

  • The problem is that you are adding each rectangle to the List and at the end, printing out the entire list. If you only want one rectangle, then remove the list and update a single instance of Rectangle2D.

    You add the rectangle here.

    private ArrayList<Rectangle2D> rectangles = new ArrayList<>();
    
    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();
    }
    

    Each time you print them all here.

    for (Rectangle2D rectangle : rectangles){
      //Draw a basic rectangle, just as proof that this works
          g2d.draw(rectangle);
    }