#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Help creating Rectangle.class that is utilized in a different program


    Hey everyone. I have an assignment to create a rectangle class. The end result of the assignment is a graphic with multiple rectangles of different colors and on top of that, different colors where they overlap. "The Rectangle constructor should accept arguments in this order: x1, y1, x2, y2, r, g, b. The r,g,b should determine the color of the rectangle. The coordinates should determine its location, but it shouldn't matter what order they come in. The left side of the rectangle should be at whichever x is smaller, and the right side should be at the larger x. Likewise, the top side of the rectangle should be at whichever y is smaller, and the bottom side should be at the larger y." This is the instructions for the first section of the program and I am a little confused as to what instance data I am creating and how to set up the constructor? Any help would be appreciated.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Would I separately define each variable on its own like:
    private int x1;
    private int y1;
    etc..?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    import java.awt.Color;
    import java.awt.Graphics;

    public class Rectangle {

    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private int r, g, b;



    public Rectangle (int x1, int y1, int x2, int y2, int r, int g, int b) {

    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    this.r = r;
    this.g = g;
    this.b = b;


    }

    public void draw(Graphics page) {
    Color color = new Color (r, g, b);
    page.setColor(color);
    page.drawRect(x1, y1, x2, y2);
    }
    }

    This is what I have so far. Sorry it isn't in the right format...I forgot how to do that if anyone wants to refresh my memory.
    Now I have to create a method called overlapsWith. The overlapsWith method should return whether or not the rectangle you call it on (this) overlaps with another rectangle (the argument). Here's a strategy for checking if this rectangle overlaps with another:

    If the left x of this rectangle is larger than the right x of the other (or vice versa), they don't overlap.
    If the top y of this rectangle is larger than the bottom y of the other (or vice versa), they don't overlap.
    Otherwise, they do overlap.

    Is this a boolean method? Some pointers on this section would be awesome guys.

IMN logo majestic logo threadwatch logo seochat tools logo