Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37

Thread: draw 4 different polygons.

  1. #26
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    how do I save id, x, y array polygons
    I thank you in advance

    <
           Polygon[] polygons = new Polygon[4]; 
           int id_number = 0;
           int x = 0;
           int y = 0;
           for (POINTS n : POLYGON) {
           id_number = n.getID();    
           x = n.getX();
           y = n.getY();
           id_number++;
           x++;
           y++;       
           }
    >

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: draw 4 different polygons.

    Outside the loop: Define two arrays for the x and y values and an index to use with them.
    Inside the loop: assign the current POINT's x and y values in the arrays and increment the index:
    x[index] = POINT's x value
    y[index] = POINT's y value
    index++
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    I apologize as we move forward I can not see me for help

    <
                int[] x = null  ;
                int[] y = null ;
                int index = 0;
     
                for (POINTS pnt : POLYGON) {
                x[index] = pnt.getX();
                y[index++] = pnt.getY();
             }
    >

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: draw 4 different polygons.

    The x an y arrays should be defined the same size as the POLYGON collection.This is copied from your code:
    int n = POLYGON.size() ;
     
    int[] x = new int[n];
    int[] y = new int[n];

    Now look at the steps in post #15.
    You need to recognize when the id for the POINTs object changes.
    When it changes then you are at the end of the points for the last id and need to create a Polygon object that has all the points that have been saved in the x and y arrays and save that new Polygon object in the polygons array.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    I apologize to recognize when the id changes should I use if () {}
    or how do I know when the id changes and pass the points array
    and draw thank you in advance
    <
     
               int n = POLYGON.size() ;
               int[] x = new int[n];
               int[] y = new int[n];
               int index = 0;
     
     
               Polygon[] polygons = new Polygon[4];
               for (POINTS pnt : POLYGON) {
               x[index] = pnt.getX();
               y[index++] = pnt.getY();
     
             }
    >

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: draw 4 different polygons.

    how do I know when the id changes
    When the value returned by the getID() method changes.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    I'm sorry I can not go on I thank you in advance.

    <
     
               int n = POLYGON.size() ;           
               int[] x = new int[n];
               int[] y = new int[n];
               int index = 0;
     
     
               Polygon[] polygons = new Polygon[4];
               for (POINTS pnt : POLYGON) {
               int id = pnt.getID();
               x[index] = pnt.getX();
               y[index++] = pnt.getY();        
             }
    >

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: draw 4 different polygons.

    You need an int variable that keeps track of the id of the POINTS. It should start at 1 to match the id of the first POINTS in the POLYGON collection.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    I'm sorry but how do I save the collection

    <
               int n = POLYGON.size() ;           
               int[] x = new int[n];
               int[] y = new int[n];
               int index = 0;
               int id = 0;
     
               Polygon[] polygons = new Polygon[4];
               for (POINTS pnt : POLYGON) {
               id = pnt.getID()-1;
               x[index] = pnt.getX();
               y[index++] = pnt.getY();
               id++;
             }
    >

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: draw 4 different polygons.

    how do I save the collection
    Please explain.
    What variable contains a reference to the collection?
    What do you mean by "save"? Write it to disk file or put it into a database?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #36
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    I'm sorry not to save but to pass the points based
    on the id to polygons and then draw that is the question

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: draw 4 different polygons.

    If it is too complicated now to separate the x,y pairs into separate Polygons based on their id, put all of the x,y points into one Polygon object. Change the loop in the paintComponent() method that loops through the array of Polygon obects to only draw the first polygon (which will have the points for all 4 polygons).
    When that is working, then come back to the logic that will detect when the next POINTS from the POLYGON collection has a different id from the last POINTS object and use that to create a Polygon object for the x,y points saved so far and to reset the pointer to start saving the x,y points for the next id.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Help with Draw Program
    By kaso2793 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 23rd, 2012, 02:12 PM
  2. Re: Draw labyrinth ( I need help)
    By tubluforu in forum Java Theory & Questions
    Replies: 1
    Last Post: January 6th, 2012, 11:08 PM
  3. Draw labyrinth ( I need help)
    By ramin in forum Loops & Control Statements
    Replies: 2
    Last Post: January 3rd, 2012, 05:27 PM
  4. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 02:29 AM