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 › searching arraylist
Pages: 1 2 3 
searching arraylist (Read 2707 times)
Re: searching arraylist
Reply #15 - Apr 21st, 2009, 6:39am
 
ok so what is the best way to search the arraylist within the code that you so kindly helped with before?
Re: searching arraylist
Reply #16 - Apr 21st, 2009, 7:21am
 
Using PhiLho's example as the basis if we assume that the variable sentences is no longer an array of Strings but an ArrayList of Strings (your SMS messages) then there are only a couple of changes to make

Code:

 for (int s = 0; s < sentences.length; s++)  {
   String sentence = sentences[s];


becomes
Code:

 for (int s = 0; s < sentences.size(); s++)  {
   String sentence = sentences.get(s);

Re: searching arraylist
Reply #17 - Apr 21st, 2009, 7:40am
 
the code im using to get my messages is earlier in the post, not sure how to put it together

i tried changing what you suggested but got error message

cannot invoke size() on the array type string[]

is that because the the example isnt using an arrylist? that thought only just came to me there :S
Re: searching arraylist
Reply #18 - Apr 21st, 2009, 8:17am
 
From PhiLho's example copy the code
Code:

class ColorNameAndValue{
 String name;  
 color value;
 ColorNameAndValue(String n, color c)  {
   name = n;
   value = c;  
}
}

ColorNameAndValue[] triggers ={  
 new ColorNameAndValue("blue", #0000FF),
 new ColorNameAndValue("green", #00FF00),
 new ColorNameAndValue("red", #FF0000),
 new ColorNameAndValue("cyan", #00FFFF),
 new ColorNameAndValue("magenta", #FF00FF),
 new ColorNameAndValue("yellow", #FFFF00),  
 new ColorNameAndValue("black", #000000),  
 new ColorNameAndValue("white", #FFFFFF)
};

into your sketch.

You have a method called
void ShowMessage(Message m, int pos)

At the beginning of this method insert the following code
Code:

   color col = #7F7F7F; // Default colour to use
   for (int c = 0; c < triggers.length; c++)    {
      if(m.m_message.toLowerCase().contains(triggers[c].name)) {
         col = triggers[c].value;
         break;
      }    
  }
   fill(col);
   // Your code follows here make sure you use the right
   //  colours as your has some fill statements as well.


This should work but I havn't tested it.

have to go logoff now I will try and have a look tonight and see how you are getting on.

Re: searching arraylist
Reply #19 - Apr 23rd, 2009, 7:41am
 
got that working so another question is how do i then get the trigger to implament drawing something instead of just display the text?
Re: searching arraylist
Reply #20 - Apr 23rd, 2009, 7:49am
 
and can add more than a colour code into the nameandvalue like what font to use ? or a class i could call
Re: searching arraylist
Reply #21 - Apr 23rd, 2009, 8:27am
 
Good timing, I just finished a demo sketch answering your questions...
I anticipated them in my previous answer! Wink
Now, I might have overdone it a bit (adding the Matcher interface), but it was fun...  Cheesy
For clarity and organization, I made three files:
SearchWordClass.pde Code:
int DISPLAY_FONT_SIZE = 50;
int DISPLAY_TIME = 120; // 2 seconds at 60 FPS
color BACK_COLOR = color(200);

int currentSentenceIdx;
int startFrameCount;
int currentFrameCount;
String currentSentence;
Drawer currentDrawer;

class MatcherAndDrawer
{
 Matcher matcher;
 Drawer drawer;

 MatcherAndDrawer(Matcher m, Drawer d)
 {
   matcher = m;
   drawer = d;
 }
}

MatcherAndDrawer[] triggers =
{
 new MatcherAndDrawer(
     new WordInside("grow"), new Growing()),
 new MatcherAndDrawer(
     new RegExMatcher("(tiny|small|little)", true),
new Shrinking()),
 new MatcherAndDrawer(
     new RegExMatcher("fade.*black"),
new Fade(BACK_COLOR, #000000)),
 new MatcherAndDrawer(
     new RegExMatcher("fade.*gr[ea]y"),
new Fade(BACK_COLOR, #7F7F7F)),
 // Only if at start of sentence!
 new MatcherAndDrawer(
     new RegExMatcher("black.*"), new PlainColor(#000000)),
 new MatcherAndDrawer(
     new WordInside("white"), new PlainColor(#FFFFFF)),
 new MatcherAndDrawer(
     new WordInside("blue"), new PlainColor(#0000FF)),
 new MatcherAndDrawer(
     new WordInside("green"), new PlainColor(#00FF00)),
 new MatcherAndDrawer(
     new WordInside("red"), new PlainColor(#FF0000)),
 // Only as a separate word, but anywhere in the sentence
 new MatcherAndDrawer(
     new RegExMatcher("\\bcyan\\b", true),
new PlainColor(#00FFFF)),
 new MatcherAndDrawer(
     new WordInside("magenta"), new PlainColor(#FF00FF)),
 new MatcherAndDrawer(
     new WordInside("yellow"), new PlainColor(#FFFF00))
};

String[] sentences =
{
 "Black Magic Woman",
 "Blue Velvet",
 "Fade to black",
 "Yellow Submarine",
 "Growing and Multiplying",
 "Cyanide and Hapiness",
 "White Christmas",
 "Brown Sugar", // Voluntarily not matching
 "Little House on the pairie",
 "My Baby (Cyan Sleep)",
 "The Black Belt Ninja", // Voluntarily doesn't match "black" matcher
 "The Yellow Brick Road",
 "The Blues Brothers",
 "Steven Spielberg Presents... Tiny Toon Adventures",
 "Greensleeves",
 "Fade to gray",
 "The Red Line",
 "Magenta is rare in song and film names..."
};

void setup()
{
 size(1000, 100);
 background(200);
 PFont f = createFont("Arial Bold", DISPLAY_FONT_SIZE);
 textFont(f);
 UpdateData();
}

void draw()
{
 background(BACK_COLOR);
 currentDrawer.Draw(currentSentence, 10, height - 10, currentFrameCount);
 currentFrameCount++;
 if (frameCount - startFrameCount > DISPLAY_TIME) // Display  a message for a given duration
 {
   currentSentenceIdx++;
   if (currentSentenceIdx == sentences.length)
   {
background(BACK_COLOR);
noLoop();
return;
   }
   UpdateData();
 }
}

void UpdateData()
{
 currentSentence = sentences[currentSentenceIdx];
 println(currentSentence);
 currentDrawer = defaultDrawer;
 for (int t = 0; t < triggers.length; t++)
 {
   if (triggers[t].matcher.IsMatching(currentSentence))
   {
currentDrawer = triggers[t].drawer;
break;
   }
 }
 currentFrameCount = 0;
 startFrameCount = frameCount;
}


Matchers.pde Code:
interface Matcher
{
 boolean IsMatching(String sentence);
}

class WordInside implements Matcher
{
 String word;

 WordInside(String w)
 {
   word = w;
 }
 boolean IsMatching(String sentence)
 {
   return sentence.toLowerCase().contains(word);
 }
}

class RegExMatcher implements Matcher
{
 String regex;

 RegExMatcher(String re)
 {
   regex = re;
 }
 RegExMatcher(String re, boolean bLoose)
 {
   if (bLoose)
   {
regex = ".*" + re + ".*";
   }
   else
   {
regex = re;
   }
 }
 boolean IsMatching(String sentence)
 {
   return sentence.toLowerCase().matches(regex);
 }
}

/*
Other possible variants (can be combined):
- strict: doesn't use toLowerCase
- at start: uses sentence.startsWith()
- at end (endsWith())
- and so on
*/

Drawers.pde Code:
Drawer defaultDrawer = new PlainColor(#7F7F7F);

interface Drawer
{
 void Draw(String message, int x, int y, int frame);
}

class PlainColor implements Drawer
{
 color col;

 PlainColor(color c)
 {
   col = c;
 }
 void Draw(String message, int x, int y, int frame)
 {
   fill(col);
   text(message, x, y);
   // I don't use frame here
 }
}

class Fade implements Drawer
{
 color col1, col2;

 Fade(color c1, color c2)
 {
   col1 = c1;
   col2 = c2;
 }
 void Draw(String message, int x, int y, int frame)
 {
   fill(lerpColor(col1, col2, frame / (float) DISPLAY_TIME));
   text(message, x, y);
 }
}

class Growing implements Drawer
{
 Growing()
 {
 }
 void Draw(String message, int x, int y, int frame)
 {
   fill(0);
   textSize(lerp(0, DISPLAY_FONT_SIZE, frame / (float) DISPLAY_TIME));
   text(message, x, y);
 }
}

class Shrinking implements Drawer
{
 Shrinking()
 {
 }
 void Draw(String message, int x, int y, int frame)
 {
   float a = frame / (float) DISPLAY_TIME;
   pushStyle();
   fill(0);
   textSize(lerp(DISPLAY_FONT_SIZE, 0, a));
   float posX = lerp(width - textWidth(message), x, a);
   float posY = lerp(height - DISPLAY_FONT_SIZE, y, a);
   text(message, posX, posY);
   popStyle();
 }
}

You can simplify (get rid of the matchers, using "contains" as before) or expand (adding a time of display per word) this code...
Re: searching arraylist
Reply #22 - Apr 27th, 2009, 7:05am
 
ok thats really helpful little confused as how to put it together with the text coming in from the mysql connection first day i've had to work on in since you posted so im going to work on it at the moment
Re: searching arraylist
Reply #23 - Apr 28th, 2009, 7:19am
 
Hey guys im really struggling to put together the code that pulls in my strings from my database and the wordclass matcher and drawer, im very confused
Re: searching arraylist
Reply #24 - Apr 28th, 2009, 12:28pm
 
trying to add a new class that would draw some circles

but getting an error the type WordClassstuff.Circles must implement the inherited abstract method WordClassstuff.Drawer.Draw(string, int, int,int)

Undecided
Re: searching arraylist
Reply #25 - Apr 28th, 2009, 12:50pm
 
I am not too sure how to help you. You should write more precise specifications... Wink

If I understood correctly, you have messages added from time to time to the database table.
My initial program fetches all the new messages from the last check and add them to list of known messages.

Now, we must know how you want to display these messages. Are they displayed permanently? For a given time? One at a time? What to do when there is no new message? Just show a dark screen? A generic message? Restart at beginning, with new messages interrupting this? Etc.

To answer your question, your new classes must use implements Drawer in the class definition, and therefore honor the Drawer contract (that's the role of interfaces) by implementing the Draw() function with these parameters (even if you don't use them all). You can add other methods, but this one is mandatory.
Re: searching arraylist
Reply #26 - Apr 28th, 2009, 1:01pm
 
ok precise
a user would send a text message depending on the content it would either show up as static text(various fonts coulours sizes), or would draw something (like a paint drip or a rainbow sine wave)

the messages would fade after a certain time and there is no limit to how many are on screen at the time , after the messages fade it would sit idle until a new message appeared.
Re: searching arraylist
Reply #27 - Apr 28th, 2009, 1:07pm
 
this is the class i tried to add to the drawers not sure what is going wrong as it looks the same as the others

class Circles implements Drawer
{
 Circles()
 {
 }
 void draw(String message, int x, int y, int frame) {
 drawCircles(250, 250, 100, 10);
 }
void drawCircles(float x, float y, int radius, int level)
{
 noStroke();
 float tt = 126 * level / 6.0;
 fill (tt, 0, 116);
 ellipse(x, y, radius*2, radius*2);
 if (level > 1) {
   level = level - 1;
   int num = int (random(2, 5));
   for(int i=0; i<num; i++) {
     float a = random(0, TWO_PI);
     float nx = x + cos(a) * 6.0 * level;
     float ny = y + sin(a) * 6.0 * level;
     drawCircles(nx, ny, radius/2, level);
   }
 }
}
Re: searching arraylist
Reply #28 - Apr 28th, 2009, 1:42pm
 
ilikebagels wrote on Apr 28th, 2009, 1:07pm:
as it looks the same as the others

No, Java is case sensitive... I stick to my habit of making initial letter of methods uppercase, but if you prefer to stick to common convention, which is understandable, you have to change Draw() to draw() in the interface definition.
Re: searching arraylist
Reply #29 - Apr 28th, 2009, 1:47pm
 
ok that makes sense got that working what about the rest of it  Undecided its giving me such a headache
Pages: 1 2 3