So I'm working on a Java assignment where I had to create a Rectangle class that was utilized in a program that draws overlapping rectangles and where the rectangles overlap, a new rectangle is drawn, with a new color. I have included a link to the HW description as I figured it be easiest to just let you look over that than me trying to explain it..http://myslu.stlawu.edu/~ltorrey/courses/spring13/cs219/hw/hw6.html
I am currently stuck on creating the getOverlap method, which draws the new overlapped rectangle. I had to create a draw method that obviously draws the rectangles and am confused on whether the getOverlap method defines new parameters for the variables that were previously defined and then reroute into the draw method? Any help would be greatly appreciated.
If
java.awt.Rectangle
is permitted, here's an outline of how you can use it in your implementation:With a little clever use of
Area
you can get theGraphics2D
API to do it for you.Basically, I create an
Area
that is the result of an exclusiveOR operation on the two rectangles. I then subtract this from anArea
that is the result of adding the two rectangles togetherPlaying with MadProgrammer's code I found a couple of simpler ways to do this:
Using an Area:
Or using another Rectangle: