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 2 of 2

Thread: Question on Rectangles

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Question on Rectangles

    Hi there,

    I am to code a method that creates a rectangle (r3) which is the area of two intersected rectangles (r1, r2).
    r3 must be in (x, y, width, height) format.
    The first rectangle (r1) is randomly created (x, y, width, height - are all random inbetween 0-100)
    The second rectangle (r2) is created by user input (x, y, width, height - entered by user between 0-100)

    If there is an intersection, the program prints "r3(x, y, width, height)"
    If there is no intersection the program prints "no intersection"

    How would I go about doing this? A while loop inside an if statement?

    private void findIntersection(Rectangle r1) {
    r2 = new Rectangle(x, y, width, height);
    r2 = userRectangle(); //method for user input
     
    if(r1.intersects(r2)) {
    //while loop?
    System.out.println("The intersection is ");
    }else{
    System.out.println("No intersection");
    }
    }

    Any hints or tips appreciated
    Thanks


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Question on Rectangles

    What is this Rectangle class?

    If you are using java.awt.Rectangle then there is a method to find intersections. If not - ie if you are writing the Rectangle class - you should concentrate on writing a method that produces a new Rectangle by intersection with some given rectangle.

    Then once you have such a Rectangle method (or using the java.awt.Rectangle one) you can write the code to (a) get user input) (b) create rectangles (c)intersect them (d) output the result.

    The point is to separate the user input code from the intersection code.

    -----

    I can't see anything in your statement of the problem that calls for a while loop: ie any action that repeats over and over again. Certainly finding the intersection of two rectangles does not involve any loops.

Similar Threads

  1. adding rectangles in images
    By mserrao in forum AWT / Java Swing
    Replies: 4
    Last Post: September 1st, 2011, 06:27 AM
  2. Drawing Rectangles and Lines
    By andreizeus in forum AWT / Java Swing
    Replies: 21
    Last Post: October 28th, 2010, 12:59 PM
  3. How to form a rectangle from the union of two Rectangles.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 3rd, 2010, 07:22 AM