# Beginning Java Project Help (Rectangle Construction)



## Tizgone (Jan 23, 2010)

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 deﬁned relative to those of the ﬁrst 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? 


> package rectangles;
> import java.awt.*;
> 
> public class RectangleTester
> ...


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).


----------



## andythepandy (Jul 2, 2006)

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:

```
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:

```
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 (Jan 23, 2010)

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. 


> :
> public static void main(String[] args) {
> Rectangle rectOne = new Rectangle(50, 60, 100, 80);
> int ox1 = (int) (rectOne.getX());
> ...


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:


> :
> 
> 4. Translate both rectangles to the right along the x coordinate axis by
> 10, and upward along the y coordinate axis by 10.
> ...


Seems a bit much for a 25 point project, considering we spend the class listening to him talk about his travels. Yay college!


----------



## andythepandy (Jul 2, 2006)

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 (Jan 23, 2010)

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.



> :
> rectOne.translate(10,-10);
> ox1 = (int) (rectOne.getX());
> oy1 = (int) (rectOne.getY());
> ...


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.


----------



## andythepandy (Jul 2, 2006)

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 (Jan 23, 2010)

I didn't translate rectangle two. I just used:


> 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.


----------

