Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Beginning Java Project Help (Rectangle Construction)


(!)

Tizgone's Avatar
Tizgone Tizgone is offline
Junior Member with 4 posts.
THREAD STARTER
 
Join Date: Jan 2010
17-Sep-2011, 02:47 PM #1
Beginning Java Project Help (Rectangle Construction)
Hey guys, this is my first post and probably (as usual), within my first post, I need help. I'm supposed to be doing a project for my beginning Java class, but the teacher is rather, lacking. I was hoping that someone here could help me.

His outline says to (for the first half, at least):
1. Construct a rectangle whose top-left point coordinates are (40,60) and whose width is 100 and height is 80.
2. Construct a smaller concentric rectangle with the parameters to the constructor defined relative to those of the first rectangle. Both rectangles should be such that the width of the path between the two rectangles is uniformly 10 units. (Note: No argument to this constructor should be a constant.)
3. Using appropriate methods, print the coordinates of the vertices of each rectangle in the form {(x1, y1), (x2, y2), (x3, y3), (x4, y4)}, where the coordinates represent the vertices of the top-left, top-right, bottom-right, and bottom-left corners. Use a label to indicate which rectangle’s coordinates are printed. Also, using appropriate methods, calculate and print the area of the path between the two rectangles (Hint: subtract the areas of the rectangles.)

I can construct the rectangle with the proper coordinates and all, but I stumble around and I'm having a REALLY hard time finding out how to get the second rectangle to use the first's points as a reference. His notes give different documentation than the book which give different documentation than anything I could find online.I've tried a bunch of different stuff. I'll upload my coding so far.. it's clunky. I thought I had the second part down, so I started to try to get it to display the coordinates, but I just broke it further.

tl;dr Questions:
1. How do I get a rectangle to reference a different rectangle's points as its own (Plus or minus a few pixels)?
2. How do I print out the location of the other corners of the rectangle that aren't the origin?
Quote:
package rectangles;
import java.awt.*;

public class RectangleTester
11 {
12 public static void main(String[] args) {
13 Rectangle rectOne = new Rectangle(40, 60, 100, 80);
14 Rectangle rectTwo = new Rectangle((int)rectOne.getX()+10,(int)rectOne.getY()+10,(int)rectOne.getWid th()-20,(int)rectOne.getHeight()-20);
15
16 System.out.println("The coordinates of the bigger rectangle are: " + rectOne.getX());
17
18 System.out.println("The coordinates of the smaller rectangle are: " + rectTwo.getX());
19 }
}
So, my code is now "error free", but I'm still not sure how to reference the other parts of the rectangle (Right lower and bottom corners and left lower).

Last edited by Tizgone; 17-Sep-2011 at 03:16 PM.. Reason: Fixed some errors
andythepandy's Avatar
andythepandy andythepandy is offline
Computer Specs
Member with 365 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
17-Sep-2011, 06:18 PM #2
Well you can find out all the points in the rectangle with four pieces of information:
  • Top Left X Coordinate
  • Top Left Y Coordinate
  • Width
  • Height
There are functions to get all of these from the Rectangle.

For example the top right corner x co-ordiante would be: (TopLeftX + Width) and the y co-ornidate would be just (TopLeftY) because the two points on the top are at the same height.

Your code for creating the second Rectangle based on the first is correct (although there is a space in getWidth() in the third argument for rectTwo - don't know if this just happened when you pasted the code in)

So for your bigger rectangle the output would be generated by this:
Code:
		System.out.println("The coordinates of the bigger rectangle are: {("
				+ rectOne.getX() + ", " + rectOne.getY() + "), (" + (rectOne.getX()
						+ rectOne.getWidth()) + ", " + rectOne.getY() + "), (" + (rectOne.getX()
								+ rectOne.getWidth()) + ", " + (rectOne.getY() + rectOne.getHeight())
								+ "), (" + rectOne.getX() + ", " + (rectOne.getY() + rectOne.getHeight()) + ")}");
You can also use .getMaxX() and .getMaxY() which will give you (getX() + getWidth()) and (getY() + getWidth()) respectively. Using these would give the clearer:
Code:
		System.out.println("The coordinates of the bigger rectangle are: {("
				+ rectOne.getX() + ", " + rectOne.getY() + "), (" + rectOne.getMaxX() + ", "
				+ rectOne.getY() + "), (" + rectOne.getMaxX() + ", " + rectOne.getMaxY()
				+ "), (" + rectOne.getX() + ", " + rectOne.getMaxY() + ")}");

Hope this is clear enough and helps,
Andy
Tizgone's Avatar
Tizgone Tizgone is offline
Junior Member with 4 posts.
THREAD STARTER
 
Join Date: Jan 2010
17-Sep-2011, 11:19 PM #3
I was overthinking it, I think. I was thinking about maybe putting in points instead of just doing what I did. I over-hauled everything and just made a LOT of variables. It seems to have worked.
Quote:
:
public static void main(String[] args) {
Rectangle rectOne = new Rectangle(50, 60, 100, 80);
int ox1 = (int) (rectOne.getX());
int oy1 = (int) (rectOne.getY());
int ox2 = (int) (rectOne.getX()+rectOne.getWidth());
int oy2 = (int) (rectOne.getY()+rectOne.getHeight());
int oneArea = (int) (rectOne.getHeight()*rectOne.getWidth());
Rectangle rectTwo = new Rectangle(ox1+10,oy1+10,(int)rectOne.getWidth()-20,(int)rectOne.getHeight()-20);
int tx1 = (int) (rectTwo.getX());
int ty1 = (int) (rectTwo.getY());
int tx2 = (int) (rectTwo.getX()+rectTwo.getWidth());
int ty2 = (int) (rectTwo.getY()+rectTwo.getHeight());
int twoArea = (int) (rectTwo.getHeight()*rectTwo.getWidth());
System.out.println("The coordinates of the bigger rectangle are: {(" + ox1 + ", " + oy1 + "),(" + ox2 + "," + oy1 + "),(" + ox2 + "," + oy2 + "),(" + ox1 + "," + oy2 + ")}.");

System.out.println("The coordinates of the smaller rectangle are: {(" + tx1 + ", " + ty1 + "),(" + tx2 + "," + ty1 + "),(" + tx2 + "," + ty2 + "),(" + tx1 + "," + ty2 + ")}.");
System.out.println("The area of the path is: " +(oneArea - twoArea));
I just think it's dumb that I have to be copy+pasting code. I should be able to CODE something to do this for me. I looked ahead in the book and I thought about making my own method, but I don't know if I'd receive points off for it. It compiles and runs without any hiccups. Now I just get to transform and fiddle with it more. Yay.

Instructions:
Quote:
:

4. Translate both rectangles to the right along the x coordinate axis by
10, and upward along the y coordinate axis by 10.
5. Again, print the coordinates of both rectangles using appropriate labels
and the format described in 2. Also, print the area of the path.
6. Now, use an appropriate mutator method in the Rectangle class to
increase the width by 10 and height by 15 for each rectangle. (Read and
navigate your way through the API documentation to find a mutator
method that does this. Invoke this method twice using the two objects.
Do not create another Rectangle object to accomplish this.)
7. Again, print the coordinates of both rectangles using using the format
described above. Also, print the area of the path. Include appropriate
labels.
8. Print the length of the diagonal of each rectangle. Print the results
with appropriate labels. (Indicate the corresponding width and height
of each rectangle. diagonal = p width2 + height2).
9. Now, compile and run the program and note the area of the path between the rectangles and lengths of the diagonals of the rectangles.
10. Finally, change only the top-left coordinates of the first rectangle to
(45,55) and compile the program and execute the program. If you set
up your code correctly, the lengths of the diagonals and the area of the
path between the two rectangles should remain the same
Seems a bit much for a 25 point project, considering we spend the class listening to him talk about his travels. Yay college!

Last edited by Tizgone; 17-Sep-2011 at 11:20 PM.. Reason: Oops! Fixed formatting.
andythepandy's Avatar
andythepandy andythepandy is offline
Computer Specs
Member with 365 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
18-Sep-2011, 04:31 AM #4
Yep making those variables is a good way so you aren't calling the same methods over and over again, just take note that you will have to update them when you move and resize the rectangles.
Creating a method to print out the coordinates of the two rectangles would be a good idea seeing as you have to print them out 3 times.

Good luck with the rest of your course!
Tizgone's Avatar
Tizgone Tizgone is offline
Junior Member with 4 posts.
THREAD STARTER
 
Join Date: Jan 2010
18-Sep-2011, 09:04 AM #5
Yeah, I just ran into that problem. I translated it fine and just copy+pasted my previous print line, with ox1 and all that nonsense, and it just gave me the same output. But when I manually put in rectOne.getx() it gives the updated answers. The book is really rather useless here, but I figured out how to do it. It prints out the correct points for the bigger rectangle, but not for the smaller one.

Quote:
:
rectOne.translate(10,-10);
ox1 = (int) (rectOne.getX());
oy1 = (int) (rectOne.getY());
ox2 = (int) (rectOne.getX()+rectOne.getWidth());
oy2 = (int) (rectOne.getY()+rectOne.getHeight());
oneArea = (int) (rectOne.getHeight()*rectOne.getWidth());
tx1 = (int) (rectTwo.getX());
ty1 = (int) (rectTwo.getY());
tx2 = (int) (rectTwo.getX()+rectTwo.getWidth());
ty2 = (int) (rectTwo.getY()+rectTwo.getHeight());
twoArea = (int) (rectTwo.getHeight()*rectTwo.getWidth());
System.out.println("The coordinates of the translated bigger rectangle are: {(" + ox1 + ", " + oy1 + "),(" + ox2 + "," + oy1 + "),(" + ox2 + "," + oy2 + "),(" + ox1 + "," + oy2 + ")}.");
System.out.println("The coordinates of the translated smaller rectangle are: {(" + tx1 + ", " + ty1 + "),(" + tx2 + "," + ty1 + "),(" + tx2 + "," + ty2 + "),(" + tx1 + "," + ty2 + ")}.");
System.out.println("The area of the translated path is: " +(oneArea - twoArea));
I'd guess it's because I didn't redefine rectTwo in relation to the new shifted rectangle. I can't see why that would need to happen though. All I know is that I'm getting the same outputs for the "new" inner rectangle (rectTwo) as I was the old one.

Last edited by Tizgone; 18-Sep-2011 at 09:44 AM.. Reason: Fixing some coding
andythepandy's Avatar
andythepandy andythepandy is offline
Computer Specs
Member with 365 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
18-Sep-2011, 10:16 AM #6
Unless it has been cut of the top of the copy+paste, I don't see where you have translated the inner rectangle(rectTwo). Even though you defined rectTwo in terms of rectOne in the first place, that doesn't mean that when you now change rectOne, that rectTwo will update itself. You will need to manually call rectTwo.translate(10, -10)
Tizgone's Avatar
Tizgone Tizgone is offline
Junior Member with 4 posts.
THREAD STARTER
 
Join Date: Jan 2010
18-Sep-2011, 10:53 AM #7
I didn't translate rectangle two. I just used:
Quote:
rectTwo.setBounds(ox1+10,oy1+10,(int)w1-20,(int)h1-20);
This was after some clean up and redefining h1 to be the height of the first rect, and w1 being the width. It translated the rectangle in the output.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


Tags
java, rectangles, reference

(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑