The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Help creating Rectangle.class that is utilized in a different program
Discuss Help creating Rectangle.class that is utilized in a different program in the Java Help forum on Dev Shed. Help creating Rectangle.class that is utilized in a different program Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 19th, 2013, 06:49 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 3
Time spent in forums: 53 m 43 sec
Reputation 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.
|

March 19th, 2013, 06:53 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 3
Time spent in forums: 53 m 43 sec
Reputation Power: 0
|
|
|
Would I separately define each variable on its own like:
private int x1;
private int y1;
etc..?
|

March 19th, 2013, 07:47 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 3
Time spent in forums: 53 m 43 sec
Reputation 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|