Using string instead of kind of shape in createShape()












2














I want to pass an array of strings to a function in draw() and have the function draw each shape with the first parameter specifying which type of shape to draw. Here's my code (inside of draw()):



params = new String{"LINE","0","0","2","3"};
if (params.length == 2) {
createShape(params[0],float(params[1]));
} else if (params.length == 3) {
createShape(params[0],float(params[1]),float(params[2]));
} ...
} else if (params.length == 5) {
createShape(params[0],float(params[1]),float(params[2]),float(params[3]),float(params[4]));
} ...


The ellipsis marks denote more else if statements with different numbers of parameters. I know that passing values to createShape() in this way seems redundant, but it's the best way I could find to serve my purpose, and I would like to know how to make it work. Currently, this code gives me an error and says The function createShape() expects parameters like: "createShape(int,)".



Is there any way to get around this, or will I have to specify a case for every single kind of shape?










share|improve this question






















  • why not show us the createShape method?
    – Scary Wombat
    Nov 13 '18 at 1:39










  • The createShape(kind,p) function draws a kind (can be things like LINE, ELLIPSE, RECT) with parameters p where p is some array of floats. I'm pretty sure it's specific to the processing library . Here's the documentation: processing.org/reference/createShape_.html
    – user6003925
    Nov 13 '18 at 1:44












  • Yes, as per the link you sent me Parameters kind int: either POINT, LINE, TRIANGLE, QUAD, RECT, ELLIPSE, ARC, BOX, SPHERE The first parameter e.g. LINE is actually an integer. It will be defined somewhere
    – Scary Wombat
    Nov 13 '18 at 1:54
















2














I want to pass an array of strings to a function in draw() and have the function draw each shape with the first parameter specifying which type of shape to draw. Here's my code (inside of draw()):



params = new String{"LINE","0","0","2","3"};
if (params.length == 2) {
createShape(params[0],float(params[1]));
} else if (params.length == 3) {
createShape(params[0],float(params[1]),float(params[2]));
} ...
} else if (params.length == 5) {
createShape(params[0],float(params[1]),float(params[2]),float(params[3]),float(params[4]));
} ...


The ellipsis marks denote more else if statements with different numbers of parameters. I know that passing values to createShape() in this way seems redundant, but it's the best way I could find to serve my purpose, and I would like to know how to make it work. Currently, this code gives me an error and says The function createShape() expects parameters like: "createShape(int,)".



Is there any way to get around this, or will I have to specify a case for every single kind of shape?










share|improve this question






















  • why not show us the createShape method?
    – Scary Wombat
    Nov 13 '18 at 1:39










  • The createShape(kind,p) function draws a kind (can be things like LINE, ELLIPSE, RECT) with parameters p where p is some array of floats. I'm pretty sure it's specific to the processing library . Here's the documentation: processing.org/reference/createShape_.html
    – user6003925
    Nov 13 '18 at 1:44












  • Yes, as per the link you sent me Parameters kind int: either POINT, LINE, TRIANGLE, QUAD, RECT, ELLIPSE, ARC, BOX, SPHERE The first parameter e.g. LINE is actually an integer. It will be defined somewhere
    – Scary Wombat
    Nov 13 '18 at 1:54














2












2








2







I want to pass an array of strings to a function in draw() and have the function draw each shape with the first parameter specifying which type of shape to draw. Here's my code (inside of draw()):



params = new String{"LINE","0","0","2","3"};
if (params.length == 2) {
createShape(params[0],float(params[1]));
} else if (params.length == 3) {
createShape(params[0],float(params[1]),float(params[2]));
} ...
} else if (params.length == 5) {
createShape(params[0],float(params[1]),float(params[2]),float(params[3]),float(params[4]));
} ...


The ellipsis marks denote more else if statements with different numbers of parameters. I know that passing values to createShape() in this way seems redundant, but it's the best way I could find to serve my purpose, and I would like to know how to make it work. Currently, this code gives me an error and says The function createShape() expects parameters like: "createShape(int,)".



Is there any way to get around this, or will I have to specify a case for every single kind of shape?










share|improve this question













I want to pass an array of strings to a function in draw() and have the function draw each shape with the first parameter specifying which type of shape to draw. Here's my code (inside of draw()):



params = new String{"LINE","0","0","2","3"};
if (params.length == 2) {
createShape(params[0],float(params[1]));
} else if (params.length == 3) {
createShape(params[0],float(params[1]),float(params[2]));
} ...
} else if (params.length == 5) {
createShape(params[0],float(params[1]),float(params[2]),float(params[3]),float(params[4]));
} ...


The ellipsis marks denote more else if statements with different numbers of parameters. I know that passing values to createShape() in this way seems redundant, but it's the best way I could find to serve my purpose, and I would like to know how to make it work. Currently, this code gives me an error and says The function createShape() expects parameters like: "createShape(int,)".



Is there any way to get around this, or will I have to specify a case for every single kind of shape?







java processing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 1:36









user6003925

2314




2314












  • why not show us the createShape method?
    – Scary Wombat
    Nov 13 '18 at 1:39










  • The createShape(kind,p) function draws a kind (can be things like LINE, ELLIPSE, RECT) with parameters p where p is some array of floats. I'm pretty sure it's specific to the processing library . Here's the documentation: processing.org/reference/createShape_.html
    – user6003925
    Nov 13 '18 at 1:44












  • Yes, as per the link you sent me Parameters kind int: either POINT, LINE, TRIANGLE, QUAD, RECT, ELLIPSE, ARC, BOX, SPHERE The first parameter e.g. LINE is actually an integer. It will be defined somewhere
    – Scary Wombat
    Nov 13 '18 at 1:54


















  • why not show us the createShape method?
    – Scary Wombat
    Nov 13 '18 at 1:39










  • The createShape(kind,p) function draws a kind (can be things like LINE, ELLIPSE, RECT) with parameters p where p is some array of floats. I'm pretty sure it's specific to the processing library . Here's the documentation: processing.org/reference/createShape_.html
    – user6003925
    Nov 13 '18 at 1:44












  • Yes, as per the link you sent me Parameters kind int: either POINT, LINE, TRIANGLE, QUAD, RECT, ELLIPSE, ARC, BOX, SPHERE The first parameter e.g. LINE is actually an integer. It will be defined somewhere
    – Scary Wombat
    Nov 13 '18 at 1:54
















why not show us the createShape method?
– Scary Wombat
Nov 13 '18 at 1:39




why not show us the createShape method?
– Scary Wombat
Nov 13 '18 at 1:39












The createShape(kind,p) function draws a kind (can be things like LINE, ELLIPSE, RECT) with parameters p where p is some array of floats. I'm pretty sure it's specific to the processing library . Here's the documentation: processing.org/reference/createShape_.html
– user6003925
Nov 13 '18 at 1:44






The createShape(kind,p) function draws a kind (can be things like LINE, ELLIPSE, RECT) with parameters p where p is some array of floats. I'm pretty sure it's specific to the processing library . Here's the documentation: processing.org/reference/createShape_.html
– user6003925
Nov 13 '18 at 1:44














Yes, as per the link you sent me Parameters kind int: either POINT, LINE, TRIANGLE, QUAD, RECT, ELLIPSE, ARC, BOX, SPHERE The first parameter e.g. LINE is actually an integer. It will be defined somewhere
– Scary Wombat
Nov 13 '18 at 1:54




Yes, as per the link you sent me Parameters kind int: either POINT, LINE, TRIANGLE, QUAD, RECT, ELLIPSE, ARC, BOX, SPHERE The first parameter e.g. LINE is actually an integer. It will be defined somewhere
– Scary Wombat
Nov 13 '18 at 1:54












1 Answer
1






active

oldest

votes


















0














You can't do this out of the box, but you could do something like this:



void createMyShape(String myArray){
if(myArray[0].equals("LINE")){
Shape s = createShape(LINE);
s.vertex(int(myArray[1]), int(myArray[2]));
s.vertex(int(myArray[3]), int(myArray[4]));
s.endShape();
}
else if ...
}


This function parses the myArray parameter and uses if statements to take the right actions to create a shape. It also uses the int() function to parse the string array.



Note that this is not a great design. You should probably not use a String array for this. It's probably better to use a class to encapsulate your data.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272523%2fusing-string-instead-of-kind-of-shape-in-createshape%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can't do this out of the box, but you could do something like this:



    void createMyShape(String myArray){
    if(myArray[0].equals("LINE")){
    Shape s = createShape(LINE);
    s.vertex(int(myArray[1]), int(myArray[2]));
    s.vertex(int(myArray[3]), int(myArray[4]));
    s.endShape();
    }
    else if ...
    }


    This function parses the myArray parameter and uses if statements to take the right actions to create a shape. It also uses the int() function to parse the string array.



    Note that this is not a great design. You should probably not use a String array for this. It's probably better to use a class to encapsulate your data.






    share|improve this answer


























      0














      You can't do this out of the box, but you could do something like this:



      void createMyShape(String myArray){
      if(myArray[0].equals("LINE")){
      Shape s = createShape(LINE);
      s.vertex(int(myArray[1]), int(myArray[2]));
      s.vertex(int(myArray[3]), int(myArray[4]));
      s.endShape();
      }
      else if ...
      }


      This function parses the myArray parameter and uses if statements to take the right actions to create a shape. It also uses the int() function to parse the string array.



      Note that this is not a great design. You should probably not use a String array for this. It's probably better to use a class to encapsulate your data.






      share|improve this answer
























        0












        0








        0






        You can't do this out of the box, but you could do something like this:



        void createMyShape(String myArray){
        if(myArray[0].equals("LINE")){
        Shape s = createShape(LINE);
        s.vertex(int(myArray[1]), int(myArray[2]));
        s.vertex(int(myArray[3]), int(myArray[4]));
        s.endShape();
        }
        else if ...
        }


        This function parses the myArray parameter and uses if statements to take the right actions to create a shape. It also uses the int() function to parse the string array.



        Note that this is not a great design. You should probably not use a String array for this. It's probably better to use a class to encapsulate your data.






        share|improve this answer












        You can't do this out of the box, but you could do something like this:



        void createMyShape(String myArray){
        if(myArray[0].equals("LINE")){
        Shape s = createShape(LINE);
        s.vertex(int(myArray[1]), int(myArray[2]));
        s.vertex(int(myArray[3]), int(myArray[4]));
        s.endShape();
        }
        else if ...
        }


        This function parses the myArray parameter and uses if statements to take the right actions to create a shape. It also uses the int() function to parse the string array.



        Note that this is not a great design. You should probably not use a String array for this. It's probably better to use a class to encapsulate your data.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 1:50









        Kevin Workman

        33.4k53969




        33.4k53969






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272523%2fusing-string-instead-of-kind-of-shape-in-createshape%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine