1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Beginning Java Project Help (Rectangle Construction)

Discussion in 'Software Development' started by Tizgone, Sep 17, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. Tizgone

    Tizgone Thread Starter

    Joined:
    Jan 22, 2010
    Messages:
    4
    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?
    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).
     
  2. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    367
    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
     
  3. Tizgone

    Tizgone Thread Starter

    Joined:
    Jan 22, 2010
    Messages:
    4
    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.
    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:
    Seems a bit much for a 25 point project, considering we spend the class listening to him talk about his travels. Yay college!
     
  4. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    367
    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!
     
  5. Tizgone

    Tizgone Thread Starter

    Joined:
    Jan 22, 2010
    Messages:
    4
    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.

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

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    367
    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)
     
  7. Tizgone

    Tizgone Thread Starter

    Joined:
    Jan 22, 2010
    Messages:
    4
    I didn't translate rectangle two. I just used:
    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.
     
  8. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1018141