Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with method to check if two rectangles overlap

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with method to check if two rectangles overlap

    Hey everyone. I am working on a hw assignment where I have to create a class called Rectangle that is utilized in another program. The end result is a graphic of colored rectangles, which overlap in certain areas and change color where they overlap. I have to create a method called overlapWith that checks to see if two rectangles overlap.

    if (redRect.overlapsWith(greenRect)) {
    Rectangle overlap = redRect.getOverlap(greenRect);
    overlap.draw(page);
    This piece of code is from the program that utilizes the overlapsWith method I am trying to create. redRect and greenRect were rectangles that were previously defined. This is from my hw assignment: 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.
    I am stuck and do not know how to start this method. How do I compare x,y data from two seperate variables? Any help would be appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with method to check if two rectangles overlap

    Where are you having problems coding the algorithm that was given?

    Take a piece of paper and draw a rectangle on it and label the corners with the values you are given to define the location of a rectangle: x,y and width and height. Looking at that diagram you have drawn read the algorithm's steps for determining an overlap. You could draw several other rectangles around the first one with some overlapping and others not overlapping.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with method to check if two rectangles overlap

    Ok so I've got that last part finished. Now I need to create a getOverlap method that draws a new rectangle over the overlap, and assigns a new color that is the rgb value of both rectangles combined, but not to reach over 255 on any given value. I really don't know how to get this one started.

    --- Update ---

    // Jon Katz
    // Initiates Rectangle class and draw and getOverlap methods.

    import java.awt.Color;
    import java.awt.Graphics;

    public class Rectangle {

    // Instance data for Rectangle class
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private int r, g, b;


    // Initialize instance data, Constructor
    public Rectangle (int x1, int y1, int x2, int y2, int r, int g, int b) {

    // Define variables
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    this.r = r;
    this.g = g;
    this.b = b;

    }

    // Create a draw method
    public void draw(Graphics page) {

    int width = x2 - x1;
    int height = y2 - y1;
    Color color = new Color (r, g, b);
    page.setColor(color);
    page.fillRect(x1, y1, width, height);
    }

    // Create overlapsWith method
    public boolean overlapsWith(Rectangle a) {
    return this.x2 >= a.x1 &&
    this.x1 <= a.x2 &&
    this.y2 >= a.y1 &&
    this.y1 <= a.y2;
    }
    public void getOverlap(Rectangle a) {
    r = this.r + a.r;
    b = this.b + a.b;
    g = this.g + a.g;
    Color overlapColor = new Color (r, g, b);
    page.setColor(overlapColor);
    }

    }

    --- Update ---

    // Lisa Torrey
    // A custom panel that draws a pattern of overlapping rectangles.

    import java.awt.*;
    import javax.swing.*;

    public class RectanglePanel extends JPanel {

    public static final int WIDTH = 200; // Panel width
    public static final int HEIGHT = 200; // Panel height
    public static final int RGB_MAX = 255; // For colors

    // Set the panel's size and color
    public RectanglePanel() {
    super.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    super.setBackground(Color.white);
    }

    // Draw some overlapping rectangles
    public void paintComponent(Graphics page) {
    super.paintComponent(page);

    // A red one in the top left
    Rectangle redRect = new Rectangle(0, 0, WIDTH/2, HEIGHT/2, RGB_MAX, 0, 0);
    redRect.draw(page);

    // A green one in the bottom right
    Rectangle greenRect = new Rectangle(WIDTH/2, HEIGHT/2, WIDTH, HEIGHT, 0, RGB_MAX, 0);
    greenRect.draw(page);

    // A blue one in the middle
    Rectangle blueRect = new Rectangle(WIDTH/4, HEIGHT/4, WIDTH*3/4, HEIGHT*3/4, 0, 0, RGB_MAX);
    blueRect.draw(page);

    // Possibly an overlap of red and green
    if (redRect.overlapsWith(greenRect)) {
    Rectangle overlap = redRect.getOverlap(greenRect);
    overlap.draw(page);
    }

    // Possibly an overlap of red and blue
    if (redRect.overlapsWith(blueRect)) {
    Rectangle overlap = redRect.getOverlap(blueRect);
    overlap.draw(page);
    }

    // Possibly an overlap of green and blue
    if (greenRect.overlapsWith(blueRect)) {
    Rectangle overlap = greenRect.getOverlap(blueRect);
    overlap.draw(page);
    }
    }

    // Create and display one of these panels
    public static void main(String[] args) {

    JFrame frame = new JFrame("Rectangle Panel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    RectanglePanel panel = new RectanglePanel();
    frame.getContentPane().add(panel);

    frame.pack();
    frame.setVisible(true);
    }
    }

    --- Update ---

    The first is my code so far and the second is the test program. I am confused on how to set up the getOverlap method. Any ideas?

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Help with method to check if two rectangles overlap

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/70466-help-overlapping-rectangle-assignment.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with method to check if two rectangles overlap

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. check for boolean method.
    By daitobu in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2012, 08:54 AM
  2. Disabling Check Boxes created by different method.
    By xdega in forum AWT / Java Swing
    Replies: 3
    Last Post: April 23rd, 2012, 11:06 AM
  3. check for valid argument to a method
    By lupis in forum Exceptions
    Replies: 8
    Last Post: March 24th, 2012, 11:09 PM
  4. Why Graphics objects don't overlap.
    By SyntheticD in forum Java Theory & Questions
    Replies: 0
    Last Post: March 27th, 2011, 09:40 PM
  5. Method for Cedit card check, help!!!!1
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 31st, 2009, 09:16 AM