Question: Making simple graphics in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theNewb
    New Member
    • Feb 2007
    • 8

    #1

    Question: Making simple graphics in Java

    Two questions on graphics:

    1. I'm trying to create the rooftop for a 'house' which needs to be 61 pixels long. In order to center the roof properly, I need to have both base points at 30.5 pixels from the center.

    I am trying to accomplish this by making three points, the peak, and left and right base points, and connecting them with lines.

    However, code like this:
    Point2D.Float roofPointLeft = new Point2D.Float (9.5 + x, 40 + y);

    produces an error.

    I've been told you can't have half a pixel, and this makes sense to me. However, without half a pixel I cannot center my roof according to the assignment instructions.

    Insight, anyone?

    2. The roof of my house consists of three points, two lines for the sides, and the top of the rectangle which serves as the main body of the house as the base.

    How can I fill an enclosed area like my roof?

    A related question, if I arrange three shape objects (say a circle, a triangle, and a square) so that there is an enclosed space between them -- is there a way to fill this space?

    Thanks for any and all help!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by theNewb
    Two questions on graphics:

    1. I'm trying to create the rooftop for a 'house' which needs to be 61 pixels long. In order to center the roof properly, I need to have both base points at 30.5 pixels from the center.

    I am trying to accomplish this by making three points, the peak, and left and right base points, and connecting them with lines.

    However, code like this:
    Point2D.Float roofPointLeft = new Point2D.Float (9.5 + x, 40 + y);

    produces an error.

    I've been told you can't have half a pixel, and this makes sense to me. However, without half a pixel I cannot center my roof according to the assignment instructions.

    Insight, anyone?

    2. The roof of my house consists of three points, two lines for the sides, and the top of the rectangle which serves as the main body of the house as the base.

    How can I fill an enclosed area like my roof?

    A related question, if I arrange three shape objects (say a circle, a triangle, and a square) so that there is an enclosed space between them -- is there a way to fill this space?

    Thanks for any and all help!
    Let's start with 1

    What error did you get?

    Comment

    • theNewb
      New Member
      • Feb 2007
      • 8

      #3
      The constructor Point2D.Float(d ouble, int) is undefined.

      This complaint persists even if I remove the x & y variables. If I add a decimal to the second number (by writing 40.0) the complaint simply switches to (double, double).

      From my perspective, having written Point2D.Float, I was expecting (float, float).

      I thought I'd tried this (see below) before posting my question, but I tried it again just now, and does seem to accept the decimals if I write:

      Point2D.Double roofPointLeft = new Point2D.Double (9.5 + x, 40 + y);

      d'Oh!

      Does anyone know why it accept decimals with .Double but not with .Float?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by theNewb
        The constructor Point2D.Float(d ouble, int) is undefined.

        This complaint persists even if I remove the x & y variables. If I add a decimal to the second number (by writing 40.0) the complaint simply switches to (double, double).

        From my perspective, having written Point2D.Float, I was expecting (float, float).

        I thought I'd tried this (see below) before posting my question, but I tried it again just now, and does seem to accept the decimals if I write:

        Point2D.Double roofPointLeft = new Point2D.Double (9.5 + x, 40 + y);

        d'Oh!

        Does anyone know why it accept decimals with .Double but not with .Float?
        The constructor for Point2D.Float takes floats. Decimals are double by default. You could add an f at the end to indicate a float with 9.5f, the compiler would accept that. In this case I think you are better off using Point2D.Double which is accepted because decimals are double by default

        Comment

        • theNewb
          New Member
          • Feb 2007
          • 8

          #5
          Originally posted by r035198x
          The constructor for Point2D.Float takes floats. Decimals are double by default. You could add an f at the end to indicate a float with 9.5f, the compiler would accept that. In this case I think you are better off using Point2D.Double which is accepted because decimals are double by default
          Thanks! I didn't know that re: Floats & Doubles.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by theNewb
            Thanks! I didn't know that re: Floats & Doubles.
            Welcome. I suppose you should be able to complete the rest of it now?

            Comment

            • theNewb
              New Member
              • Feb 2007
              • 8

              #7
              Well, I still have no idea how to fill the roof in black! ;)

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by theNewb
                Well, I still have no idea how to fill the roof in black! ;)
                Have you had a look at the methods available here?

                Comment

                • theNewb
                  New Member
                  • Feb 2007
                  • 8

                  #9
                  Originally posted by r035198x
                  Have you had a look at the methods available here?
                  Yes I have. I also tried quite a few searches before posting here.

                  My guess is that I'm having trouble finding an answer because I don't know the correct jargon, so I'm not searching for the right keywords. It's tough being a newb!

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    You could use the fillPolygon method to fill that area. You would have to construct an array of the x points of the roof and an array of the y points of the roof.

                    A while ago, I simulated a 'filling' effect for a roof by drawing a line from the 'peak' of the roof to every point between the left end and the right end. Each line was black, and so the roof appeared filled. However, this doesn't always fill the area completely, and it's not terribly efficient.

                    Comment

                    • theNewb
                      New Member
                      • Feb 2007
                      • 8

                      #11
                      Yikes! I did see that fillPolygon method, but didn't realise it applied to my situation.

                      I'm not sure I know how to build an array like that. I'll give it a go though.

                      Clever newb-solution with the lines. ;)

                      Thanks for your help!

                      Originally posted by Ganon11
                      You could use the fillPolygon method to fill that area. You would have to construct an array of the x points of the roof and an array of the y points of the roof.

                      A while ago, I simulated a 'filling' effect for a roof by drawing a line from the 'peak' of the roof to every point between the left end and the right end. Each line was black, and so the roof appeared filled. However, this doesn't always fill the area completely, and it's not terribly efficient.

                      Comment

                      Working...