Results 1 to 5 of 5
  1. #1
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Problem with drawing rectangles from an array list

    WellI was able to fix all my other problems but I seem to be stuck on this one. The program is where you will click anywhere in the jframe and it will draw a new rectangle. I have altered the code given to us but the problem that I am having is the fact that when I click the rectangle will always be connected to the upper left hand part of the jframe, no matter where I click. The program should create rectangles of the same length and width wherever you click. Here is what I have. Would love to be nudged in the right direction. I believe I have to change something in the addRectangle method, but nothing that I have tried works.

    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import javax.swing.JComponent;
    import java.util.ArrayList;
    
    public class RectangleComponent2 extends JComponent
    {
       private static final int BOX_X = 100;
       private static final int BOX_Y = 100;
       private static final int BOX_WIDTH = 20;
       private static final int BOX_HEIGHT = 30;
       public ArrayList<Rectangle> boxes = new ArrayList <Rectangle>();
       private Rectangle box;
    
       public RectangleComponent2()
       {  
          box = new Rectangle(BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT);         
       }
       public void paintComponent(Graphics g)
       {  
          Graphics2D g2 = (Graphics2D) g;
          for(Rectangle box : boxes)
          {
             g2.draw(box);
          }
       }
       public void addRectangle(int x, int y)
       {
          boxes.add(new Rectangle(x, y));
          box.setLocation(x, y);
          repaint();      
       }
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Problem with drawing rectangles from an array list

    I'd first blame the method which calls addRectangle() as it is probably passing 0,0 for the x and y parameters always.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Problem with drawing rectangles from an array list

    This is what calls the addRectangle method. X and Y are found inside the method as well.

    Java Code:
          public void mousePressed(MouseEvent event)
          {  
             int x = event.getX();
             int y = event.getY();
             scene.addRectangle(x, y);
          }

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Problem with drawing rectangles from an array list

    Oh wait, you're using the wrong Rectangle constructor.

    https://docs.oracle.com/javase/7/doc...8int,%20int%29

    See? You're using the one that sets a width and height, not an X and Y.

    Java Code:
    public RectangleComponent2()
    { 
          box = new Rectangle(BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT);       // notice how you create this box...
    }
    
    public void addRectangle(int x, int y)
    {
          boxes.add(new Rectangle(x, y));  // and how you're creating this box. Different constructor
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Problem with drawing rectangles from an array list

    Well, I am somewhat confused as to what you want to do here. It looks like you are combining parts of your rectangle component (RC) class and your driver class (the class to test it) into a single class. Why do you add all the rectangles to an array list? Shouldn't an RC class have just one rectangle? I would also assume that the RC class would have the origin at 0,0 and that you would place the components using some layout manager. Yet you change the location of the rectangle within the class via setLocation().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Drawing Rectangles - NullPointerExceptions
    By cselic in forum Java 2D
    Replies: 4
    Last Post: 05-20-2010, 02:37 PM
  2. Replies: 1
    Last Post: 07-16-2009, 02:15 PM
  3. [Hurry!!!] Got a problem in Array List...
    By kidwave1 in forum New To Java
    Replies: 7
    Last Post: 11-24-2008, 03:17 AM
  4. Array List problem
    By kurenai in forum New To Java
    Replies: 2
    Last Post: 06-25-2008, 07:30 AM
  5. Array List Problem
    By khamuruddeen in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •