Results 1 to 6 of 6
  1. #1
    jikatz09 is offline Member
    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Default Help overlapping rectangle assignment

    Hey everyone. I am working on an assignment right now where I have to create a Rectangle class that will be utilized in a test program. The Rectangle class is to accept x and y data, create a draw method for the rectangles, create a overlapsWith method to check if the rectangles overlap, and then a getOverlap method that draws a new rectangle where the overlap occurs and changes the color of this new rectangle. The new color is the rgb of both rectangles combined, but not to exceed 255. The test program draws three triangles diagonally, with the one in the middle being intersected by both of the other rectangles. Right now, my getOverlap method is not working and I am stumped on what is wrong. Any help would be appreciated. Here is my code so far and the test code.

  2. #2
    jikatz09 is offline Member
    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help overlapping rectangle assignment

    Java Code:
    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) {
    		if (this.x2 >= a.x1 &&
    				this.x1 <= a.x2 &&
    				this.y2 >= a.y1 &&
    				this.y1 <= a.y2) {
    				return true;
    		}
    		else {
    		return false;
    		}
    	}
    	public Rectangle getOverlap(Rectangle a) {
    
    		if (this.x1 < a.x1 && a.y1 < this.y2) {
    			x1 = a.x1;
    			y1 = a.y1;
    			x2 = this.x2;
    			y2 = this.y2;
    				
    			r = this.r + a.r;
    			b = this.b + a.b;
    			g = this.g + a.g;
    			
    			Rectangle newrectangle = new Rectangle(x1, y1, x2, y2, r, g, b);
    			return newrectangle;
    		
    		}
    		if (this.x1 > a.x1 && a.y1 > this.y1) {
    			x1 = this.x1;
    			y1 = this.y1;
    			x2 = a.x2;
    			y2 = a.y2;
    			
    			r = this.r + a.r;
    			b = this.b + a.b;
    			g = this.g + a.g;
    			
    			Rectangle newrectangle = new Rectangle(x1, y1, x2, y2, r, g, b);
    			return newrectangle;
    		}
    		else {
    		return null;
    		}
    	}
    }

  3. #3
    jikatz09 is offline Member
    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help overlapping rectangle assignment

    Java Code:
    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_CLOSE);
    	
    		RectanglePanel panel = new RectanglePanel();
    		frame.getContentPane().add(panel);
    	
    		frame.pack();
    		frame.setVisible(true);
    	}
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Help overlapping rectangle assignment

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Help overlapping rectangle assignment

    Ok, I have looked at your code. Both your overlapsWith and getOverlap methods are incomplete (and incorrect). You need to do the following:


    1. Make certain to test for any corner of one rectangle inside another. That consists of four, straightforward tests which may be able to be condensed into shorter tests. I can get confusing so draw it out on paper to see the possibilities. It may make your tests easier if you develop some helper routines for similar comparisons. Just make certain you test your routines independently of your code by generating various rectangles and seeing if they are correct. You can do this in a separate program.
    2. Make certain your tests are symmetric. For any two overlapping rectangles, redRect.overlapsWith(greenRect) must always equal greenRect.overlapsWith(redRect).
    3. In your getOverlap method you are assigning the same variables to each other (e.g. x2 = this.x2 where x2 is the same instance variable).
    4. When you recalculate r, g, and b for the new rectangle, you are using the the instance variables r, g, and b to store the new color. That is wiping out the color of your calling object. If you were to redraw it, the color would be different. Use local variables for r, g, and b.
    5. Also in getOverlap you are wiping out your calling objects x1,x2, y1, y2 values. Use local values for passing to the constructor for a new rectangle.


    Regards,
    Jim
    Last edited by jim829; 03-20-2013 at 03:41 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help overlapping rectangle assignment

    Think of two line segments, both in one dimension: L,R and l,r; if L < l the intersection starts at l else L and if R > r the intersection ends at r else R. If the length of the intersection is less than zero there is no overlap. Moving to two dimensions is trivial for a rectangle.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Overlapping JLabels
    By littledrummerboy in forum New To Java
    Replies: 4
    Last Post: 05-24-2011, 04:47 PM
  2. frame overlapping
    By sameerk in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-19-2010, 07:45 AM
  3. Overlapping JLabels
    By techbossmb in forum AWT / Swing
    Replies: 3
    Last Post: 09-21-2009, 03:21 PM
  4. Help with overlapping JPanels
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 03-09-2009, 10:19 PM
  5. overlapping forms
    By keerthi_y19 in forum New To Java
    Replies: 4
    Last Post: 08-26-2008, 10:30 PM

Posting Permissions

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