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 2708 times)
Re: searching arraylist
Reply #30 - May 7th, 2009, 1:12am
 
Im getting to desperation point, im still struggling to get the first half of code (the incoming messages from the db) and the searching part working together as a whole.....i really would appreciate any help with this.
Re: searching arraylist
Reply #31 - May 7th, 2009, 7:12am
 
Don't worry, I think that this evening or tomorrow I should find some time to show some code here.

Not entirely sure how you want stuff to be displayed, I think I will grab the new messages from the DB and play an animation per message, up to 3 (or more or less) simultaneously.
The idea is:
- if I grab, say, 3 messages or less, I assign them to a pre-defined sub-area of the sketch area (allowing a bit of overlap but not to much, for readability) and play the animations.
- if I get more, I play the first 3 and at the end of the animation I play the next 3, etc.

I hope I caught your idea, if you have adjustments to suggest, just say! Smiley
Re: searching arraylist
Reply #32 - May 10th, 2009, 4:48pm
 
hey i was wondering if anyone can help build on what philho has sent me, im getting to desperation time now and its driving me crazy

i have messages getting pulled in from a db, they are being check for a specific word then the text is displayed depending on what class is triggered by a word.

problem im having with the code is that the text and the scalable vectors im calling in go crazy when i try to change the positions so the appear static and then start cropping up about 100 times a second can someone help point out what is going wrong ?

Showdbmessages class

import java.sql.Timestamp;

final color BACK_COLOR = color(200);
final int BASE_DISPLAY_FONT_SIZE = 50;
final int BASE_MESSAGE_DISPLAY_TIME = 4; // in seconds
final int MESSAGE_DISPLAY_NB = 5; // How many sentences are displayed simultaneously
final int DB_CHECK_INTERVAL = 5; // in seconds
int lastCheck = - DB_CHECK_INTERVAL - 1;

int startFrameCount;
int currentFrameCount;
String sentence;
Drawer drawer;

DBMessages dbMessageList;
MessageDisplayer[] displayedMessages = new MessageDisplayer[MESSAGE_DISPLAY_NB];


void setup()
{
 size(1000, 700);
 smooth();

 PFont f = createFont("Arial Bold", BASE_DISPLAY_FONT_SIZE);
 textFont(f);

 dbMessageList = new DBMessages(this);
 for (int i = 0; i < MESSAGE_DISPLAY_NB; i++)
 {
   UpdateMessageList(i);
 }
}

void stop()
{
//~   println("Stopping connection");
 dbMessageList.Stop();
}

void draw()
{
 background(BACK_COLOR);
 for (int i = 0; i < MESSAGE_DISPLAY_NB; i++)
 {
   MessageDisplayer displayer = displayedMessages[i];
   displayer.Draw();
   if (displayer.HasEnded())
   {
     println("Time to update " + i);
     // Animation for this message has ended, replace it with another message
     // from the queue
     UpdateMessageList(i);
   }
 }
}

void UpdateMessageList(int i)
{
 // Get a fresh new message (or a random old one)
 Message message = dbMessageList.GetMessage();

 // Sentence to display
 String sentence = message.m_message;
 MessageDisplayer displayer = GetDisplayer(sentence);
 displayedMessages[i] = displayer;
}

MessageDisplayer GetDisplayer(String sentence)
{
 Drawer drawer = null;
 String lowS = sentence.toLowerCase();
 if (lowS.indexOf("hide") >= 0)
 {
   drawer = new Shrinker(sentence, #2288FF);
 }
 else if (lowS.indexOf("free") >= 0)
 {
   drawer = new Grower(sentence, #992200);
 }
 
 else if (lowS.indexOf("yellow") >= 0 || lowS.indexOf("tang") >= 0)
 {
   drawer = new ColorMove(sentence, #FFFF55, #99FF22, BASE_MESSAGE_DISPLAY_TIME * random(0.5, 1.5));
 }
 else // Default
 {
   drawer = new ColorMove(sentence, #FFFFFF, #000000, BASE_MESSAGE_DISPLAY_TIME * random(1.0, 2.0));
 }
 println("Using " + drawer + " with " + sentence.substring(0, min(sentence.length(), 20)));
 
 return new MessageDisplayer(drawer);
}

colormove class

class ColorMove implements Drawer
{
 String message;
 color col1, col2;
 float duration;

 ColorMove(String m, color c1, color c2, float d)
 {
   message = m;
   col1 = c1;
   col2 = c2;
   duration = d;
 }
 void Draw(int frame)
 {
   float a = frame / (GetDuration() * frameRate);
   pushStyle();
   fill(lerpColor(col1, col2, a));
  // float posX = lerp(width - textWidth(message), 0, a);
  // float posY = lerp(height - BASE_DISPLAY_FONT_SIZE, 0, a);
 
  float posX = random(100,400);
  float posY = random(100,900);
   text(message, posX, posY);
   popStyle();
 }
 float GetDuration()
 {
   return duration;
 }
}

dbmessages class


import java.sql.Timestamp;
import de.bezier.data.sql.*;

public class DBMessages
{
 private MySQLLayer database;

 static final long DB_CHECK_INTERVAL = 2; // seconds
 private long lastCheck = - DB_CHECK_INTERVAL - 1;
 private Timestamp lastTimestamp = new Timestamp(0L);

 private ArrayList messageList = new ArrayList();

 public DBMessages(PApplet pa)
 {
   database = new MySQLLayer(pa);
 }
 /**
  * Clean up routine, must be called at the end of the program.
  */
 public void Stop()
 {
   database.Disconnect();
 }

 /**
  * Gets the next message (oldest), removing it from the list.
  * If none is available, returns a random message from the database.
  */
 public Message GetMessage()
 {
   UpdateData();
   Message message;
   if (messageList.size() == 0)
   {
     // Empty queue, get a random message
     message = database.GetRandomMessage();
   }
   else
   {
     message = (Message) messageList.remove(0);
   }
   println(message.m_author);
   return message;
 }

 /**
  * When getting a message, if it is time to poll the database again,
  * gets all new messages and add them to the queue.
  */
 protected void UpdateData()
 {
   long time = millis() / 1000;
   if (time - lastCheck > DB_CHECK_INTERVAL)
   {
     lastCheck = time;
     ArrayList newMessages = (ArrayList) database.GetNewMessages(lastTimestamp);
     for (int i = 0; i < newMessages.size(); i++)
     {
       Message message = (Message) newMessages.get(i);
       messageList.add(message);
       lastTimestamp = message.m_date;
     }
   }
 }
}

message class

import java.sql.Timestamp;

public class Message
{
 public String m_author;
 public String m_message;
 public Timestamp m_date;

 public Message(String author, String message, Timestamp date)
 {
   m_author = author;
   m_message = message;
   m_date = date;
 }
}

messagedisplayer

public interface Drawer
{
 public void Draw(int frame);
 public float GetDuration();
}

public class MessageDisplayer
{
 private Drawer m_drawer;
 private int m_startFrame;
 private long m_startTime;
 private long m_endTime;

 public MessageDisplayer(Drawer drawer)
 {
   m_drawer = drawer;
   m_startFrame = frameCount;
   m_startTime = millis();
   m_endTime = m_startTime + int(drawer.GetDuration() * 1000);
 }
 public void Draw()
 {
   m_drawer.Draw(frameCount - m_startFrame);
 }
 public boolean HasEnded()
 {
   return millis() >= m_endTime;
 }
}



Re: searching arraylist
Reply #33 - May 10th, 2009, 4:50pm
 
mysqllayer class

import de.bezier.data.sql.*;
import java.sql.Timestamp;

static final String TABLE_NAME = "inbound";

public class MySQLLayer
{
 private MySQL m_mySQL;

 public MySQLLayer(PApplet pa)
 {
   m_mySQL = new MySQL(pa, "134.36.216.17", "daver", "daver", "superhero");
   if (!m_mySQL.connect())
   {
     System.err.println("Cannot connect to database!");
     exit();
   }
 }

 public void Disconnect()
 {
   m_mySQL.close();
 }

 public int GetCount()
 {
   m_mySQL.query("SELECT COUNT(*) FROM " + TABLE_NAME);
   m_mySQL.next();
   return m_mySQL.getInt(1);
 }

 public ArrayList GetNewMessages(Timestamp previousDate)
 {
   String query = "SELECT creator, message, date_added FROM " + TABLE_NAME +
       " WHERE date_added > '" + FormatTimestamp(previousDate) + "'" +
       " ORDER BY date_added ASC";
//~     System.out.println(query);
   m_mySQL.query(query);

   ArrayList newMessageList = new ArrayList();
   while (m_mySQL.next())
   {
     String a = m_mySQL.getString(1);
     String m = m_mySQL.getString(2);
     Timestamp d = m_mySQL.getTimestamp(3);
     Message msg = new Message(a, m, d);
     newMessageList.add(msg);
   }
   println("Read " + newMessageList.size() + " messages");
   return newMessageList;
 }

 public Message GetMessage(int idx)
 {
   m_mySQL.query("SELECT creator, message, date_added FROM " + TABLE_NAME +
       " WHERE id=" + idx
   );
   m_mySQL.next();
   String a = m_mySQL.getString(1);
   String m = m_mySQL.getString(2);
   Timestamp d = m_mySQL.getTimestamp(3);
   return new Message(a, m, d);
 }

 public Message GetRandomMessage()
 {
   m_mySQL.query("SELECT creator, message, date_added FROM " + TABLE_NAME +
       " ORDER BY RAND() LIMIT 0, 1"
   );
   m_mySQL.next();
   String a = m_mySQL.getString(1);
   String m = m_mySQL.getString(2);
   Timestamp d = m_mySQL.getTimestamp(3);
   println("Read a random message");
   return new Message(a, m, d);
 }

 public String FormatHour(Timestamp ts)
 {
   DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
   return formatter.format(ts);
 }

 public String FormatTimestamp(Timestamp ts)
 {
   DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   return formatter.format(ts);
 }
}

shrinker class

class Shrinker implements Drawer
{
 String message;
 color col;

 Shrinker(String m, color c)
 {
   message = m;
   col = c;
 }
 void Draw(int frame)
 {
   float a = frame / (float) (GetDuration() * frameRate);
   pushStyle();
   fill(col);
   textSize(lerp(BASE_DISPLAY_FONT_SIZE, 0, a));
   float posX = lerp(width - textWidth(message), 0, a);
   float posY = lerp(height - BASE_DISPLAY_FONT_SIZE, 0, a);
   text(message, posX, posY);
   popStyle();
 }
 float GetDuration()
 {
   return BASE_MESSAGE_DISPLAY_TIME * 2;
 }
}

class Grower implements Drawer
{
 String message;
 color col;

 Grower(String m, color c)
 {
   message = m;
   col = c;
 }
 void Draw(int frame)
 {
   float a = frame / (float) (GetDuration() * frameRate);
   pushStyle();
   fill(col);
   textSize(lerp(0, BASE_DISPLAY_FONT_SIZE, frame / (float) BASE_MESSAGE_DISPLAY_TIME * 2));
   float posX = lerp(width - textWidth(message), 0, a);
   float posY = lerp(height - BASE_DISPLAY_FONT_SIZE, 0, a);
   text(message, posX, posY);
   popStyle();
 }
 float GetDuration()
 {
   return BASE_MESSAGE_DISPLAY_TIME * 2;
 }
//class Hello implements Drawer
//{
// String message;
// String col ;
// PShape bot;
// Hello(String m, color c)
// {
//   bot = loadShape("arrow1.svg");
//   message = m;
//   col = c;
// }
//  void Draw()
//  {
//  float a = frame / (float) (GetDuration() * frameRate);
//  float x = random(100,500);
//  float y = random(100,900);
//  fill(col);
//  text(message, x,y);
//  shape(bot,x,y);
//  }
//  float GetDuration()
//  {
//    return BASE_MESSAGE_DISPLAY_TIME * 2;
//  }
//}
}


also having a slight issue creating a new class i called it hello and tried to have a matcher but it came up with the error that the class didn't exist

Re: searching arraylist
Reply #34 - May 10th, 2009, 10:20pm
 
ilikebagels wrote on May 10th, 2009, 4:48pm:

  float posX = random(100,400);
  float posY = random(100,900);

I had a hard time to see what has changed, it is less practical than comparing with a diff utility... Smiley
The culprit is above: you assign a random place on the sketch on each draw, hence the described behavior.
You should put these variables as member of the class and initialize them in the constructor.
Re: searching arraylist
Reply #35 - May 10th, 2009, 11:00pm
 
I'm such a tool, it works............phew got it loading in the shapes and its not crazy !!! it totally amkes sense now...it didnt at 2 am
Pages: 1 2 3