We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › integrate slider positions into class of shapes
Page Index Toggle Pages: 1
integrate slider positions into class of shapes (Read 991 times)
integrate slider positions into class of shapes
Oct 10th, 2009, 5:23pm
 
I am working on a program that generates graphics according to a user's text input. I am using a some ControlP5 buttons to start the conversion from text to image. At the moment I am not doing anything in the draw() method but would like to have the final shapes follow the position of a slider because i found some textinput might be so long it goes off the screen. I have put part of the code below. How  do I have to amend the draw() (or any other ones) method and where to call the class of shapes to make it work.


import controlP5.*;
int yPos = 0;
Shapes[] shapes;

void setup
{
 size(500, 500)
  frameRate(25);
 background(backGroundCoLour);
 smooth();
 
 
 myTextfield = controlP5.addTextfield("texts",marginX,height-100,200,20);
 myTextfield.setFocus(true);
 myTextfield.setColorBackground(grey-30);
 myTextfield.setColorActive(colorActive);
 
 controlP5.Button ger  = controlP5.addButton("GER",22,320,height-100,20,20);
 ger.setColorBackground(grey-30);
 ger.setColorActive(colorActive);
 ger.setId(1);
   
 controlP5.Button fre  = controlP5.addButton("FRE",23,350,height-100,20,20);
 fre.setColorBackground(grey-30);
 fre.setId(2);
 fre.setColorActive(colorActive);
 
 Slider s = controlP5.addSlider("mySlider", 0, height-200, (height-200)/2 , width - 15, 50 , 5  , height -200);
 s.setColorActive(colorActive);
 s.setColorBackground(grey);
 s.setColorForeground(grey-50);
 s.setId(21);
}

void draw()    
{
 background(backGroundCoLour);
}

void controlEvent(ControlEvent theEvent)
{
//textfield
if( theEvent.controller().id() == -1)
{
String textinput = (myTextfield.getText());
String myTargetLanguage = "ENGLISH";
textfieldhasbeenUsed = true;
}
//button 1
else if( theEvent.controller().id() == 1)
  {
    String textinput = (myTextfield.getText());
    String target = "GERMAN";
    categorizeThis(textinput, target);
  }
//button 2
else if( theEvent.controller().id() == 2)
  {
    String textinput = (myTextfield.getText());
    String target = "FRENCH";
    categorizeThis(textinput, target);
  }
//slider
else if( theEvent.controller().id() == 21)
  {
       yPos = theEvent.controller().value();
  }
 
 
categorizeThis(String textinput, String target)
  {
    //method calls a categorization tool ans generates the String source
     translatethis(textinput, target, source);
  }
 
 
translatethis(String textinput, String target,String source)
  {
    //method tanslates the text into different languages with google api
    drawthewords(textinput, target, source);
  }
 
 
 
drawthewords(String textinput, String target,String source)  
{
 String[] splitintoWords = splitTokens(textinput," ");
 ArrayList allwords = new ArrayList();
 for ( int i = 0; i < splitintoWords.length; i++)
     {
       String eachWord = " " + splitintoWords[i] + " ";
       allwords.add(eachWord);
     }
 String[] words = new String[allwords.size()];
 for (int k = 0 ; k < allwords.size(); k++)
     {
       words[k] = (String)allwords.get(k);
     }  
 shapes = new Shapes[words.length];
 for (int i=0; i < words.length; i++)
     {  
       //this class contains all the methods for the shapes I want to draw to the screen
        shapes[i] = new Shapes (words.length, words, i, words[i]);
        shapes[i].createTwoCombinations();
     }
}
Re: integrate slider positions into class of shapes
Reply #1 - Oct 10th, 2009, 6:25pm
 
Some of your troubles may be coming from these errors:
- void setup() needs a set of parentheses ()
- size(500,500) needs a semicolon after ;
- controlEvent(){ needs a closing } brace
- the rest of your functions need a "void" in front.

Can't actually run your code since it is partial.  If you have custom shapes that need to move around, then your shape class needs to store the information required to draw the shapes.  The position variables of those objects get modified however you choose, and each frame, the shapes render in their new location.  You render them by looping through your shapes[] array in draw(), sending render() or whatever to each one.

--Ben
Re: integrate slider positions into class of shapes
Reply #2 - Oct 11th, 2009, 5:38am
 
oh sorry, I just extracted the code from the program and forgot these. thanks for spotting.

I think my major problem is that I am generating the parameters for the shapes loop through a textfield. So the shapes that are called in draw() depend on the user input. Which means I can't just put the loop straight  into draw() as this will cause an error. How can I get around that?
Re: integrate slider positions into class of shapes
Reply #3 - Oct 11th, 2009, 8:20am
 
There are several approaches possible.  How about just including a test to ensure the properties are set before trying to draw them?
Page Index Toggle Pages: 1