Loading...
Logo
Processing Forum
professormk's Profile
2 Posts
2 Responses
0 Followers

Activity Trend

Last 30 days
Show:
Private Message
    This is a test program for sorting an Array of Objects, based on 1 of the objects fields. I was able to overload Java's CompareTo method and call Arrays.sort(Object) -- This works well if the sort field is fixed. But I want an example where I can press "r", "g" or "a" on the keyboard and it will sort dynamically based on correct field (response, gender, age). 

    It is compiling and running, but I'm getting a strange bug where the first one or two Key Pressed work okay, then I have to hit the key TWICE for it to sort. And there a bug where hitting, say, 'r' and 'g' intermittently causes problems... I am stumped. Any ideas?


    1. PFont font;
    2. Person[] people;
    3. Person[] tmp;
    4. char sortVal;

    5. void setup()
    6. {
    7.   size(800, 400);
    8.   font = loadFont("HelveticaNeue-Light-18.vlw");
    9.   textFont(font);
    10.   textAlign(CENTER, CENTER);
    11.   noStroke();
    12.   smooth();  

    13.   //import the XML data
    14.   XMLElement xml = new XMLElement(this, "people.xml");
    15.   //split all the people up into an array of XMLElements
    16.   XMLElement[] kids = xml.getChildren("row");
    17.   //Make a new, blank array that will hold 10 People
    18.   people = new Person[10];
    19.   //Go through the array of people, fill that index with a new Person, based on the 
    20.   //information in this index of the XMLElement array.
    21.   for (int i=0; i<people.length; i++)
    22.   {
    23.     //create temp vars to store the answer, gender, age data and 
    24.     //pass them into the Constructor for this Person.
    25.     int res = int(kids[i].getChild("answer").getContent());
    26.     int gend = int(kids[i].getChild("gender").getContent());
    27.     int age = int(kids[i].getChild("age").getContent());
    28.     people[i] = new Person(age, res, gend);
    29.     println(people[i].sorter);
    30.   }
    31.   tmp = people;
    32. }

    33. void draw()
    34. {
    35.   background(255, 250, 240);
    36.   for (int i=0; i<people.length; i++)
    37.   {
    38.       tmp[i].update();
    39.       tmp[i].display(50+(i*70), height/2);
    40.   }
    41. }

    42. void keyPressed()
    43. {
    44.   if (key=='r')
    45.   {
    46.     sortVal='r'; //reminder: sortVal is global -- not part of the Person class...
    47.   }
    48.   else if (key=='a')
    49.   {
    50.     sortVal='a';
    51.   }
    52.   else if (key=='g')
    53.   {
    54.     sortVal='g';
    55.   }
    56.   else
    57.   {
    58.     println("Not a valid sort option");
    59.   }
    60.   println(sortVal);
    61.   tmp = people;  //replace the reference to the original People array
    62.   Arrays.sort(tmp); //sort the reference
    63.   // the idea here was to always sort from the original ordering... 
    64. }


    65. //The Person Class
    66. class Person implements Comparable<Person>
    67. {
    68.   int age;
    69.   int response;
    70.   int gender;
    71.   int sorter;
    72.   int original;

    73.   Person(int _age, int _response, int _gender)
    74.   {
    75.     age = _age;
    76.     response = _response;
    77.     gender = _gender;
    78.     original = 0;
    79.   }

    80.   void display(float x, float y)
    81.   {
    82.     if (gender==0)
    83.       fill(80, 100, 230);
    84.     else
    85.       fill(230, 80, 170);
    86.     ellipse(x, y, age, age);

    87.     char temp;
    88.     if (response==0) temp='N';
    89.     else temp='Y';
    90.     fill(255); 
    91.     text(temp, x, y);
    92.     fill(100);
    93.     text(age, x, height/2-50);
    94.   }
    95.   void update()
    96.   {
    97.     if (sortVal=='a') sorter=age;
    98.     else if (sortVal=='g') sorter=gender;
    99.     else if (sortVal=='r') sorter=response;
    100.     else sorter=original;
    101.   }

    102.   /* Overload compareTo method */
    103.   public int compareTo(Person tmp)
    104.   {
    105.     if (this.sorter < tmp.sorter)
    106.     {
    107.       /* instance lt received */
    108.       return -1;
    109.     } 
    110.     else if (this.sorter > tmp.sorter)
    111.     {
    112.       /* instance gt received */
    113.       return 1;
    114.     }
    115.     /* instance == received */
    116.     return 0;
    117.   }
    118. }


    The data is being read from this XML file:
    <rows>
      <row>
        <age>23</age>
        <answer>1</answer>
        <gender>1</gender>
      </row>
      <row>
        <age>24</age>
        <answer>1</answer>
        <gender>0</gender>
      </row>
      <row>
        <age>15</age>
        <answer>0</answer>
        <gender>1</gender>
      </row>
      <row>
        <age>32</age>
        <answer>1</answer>
        <gender>1</gender>
      </row>
      <row>
        <age>30</age>
        <answer>1</answer>
        <gender>0</gender>
      </row>
      <row>
        <age>29</age>
        <answer>0</answer>
        <gender>0</gender>
      </row>
      <row>
        <age>29</age>
        <answer>0</answer>
        <gender>0</gender>
      </row>
      <row>
        <age>28</age>
        <answer>0</answer>
        <gender>1</gender>
      </row>
      <row>
        <age>45</age>
        <answer>0</answer>
        <gender>1</gender>
      </row>
      <row>
        <age>62</age>
        <answer>1</answer>
        <gender>1</gender>
      </row>
    </rows>
    I've been exploring the Twitter4j library for a class demo, with Jer Thorp's tutorial as a starting point.
    So far everything has worked well, but I notice that every time I try to grab a geo location, it's consistently NULL. 

    Am I missing something? Or can it really be that SO MANY tweets have no geo-locations associated with them?? I've tried every method, object/constructor and field I can find with "geo" in the name... Everything has been null. 

    For testing, this code just pulls one tweet using noLoop() and a getTweet() function. A mouse event gets a new tweet... 

    //Build an ArrayList to hold all of the words that we get from the imported tweets
    ArrayList<String> words = new ArrayList();
    ConfigurationBuilder cb = new ConfigurationBuilder();
    Twitter twitter;

    void setup() {
      //Set the size of the stage, and the background to black.
      size(100, 100); 
      background(0);
      smooth();

      //Credentials
      cb.setOAuthConsumerKey("yours");
      cb.setOAuthConsumerSecret(" yours ");
      cb.setOAuthAccessToken(" yours ");
      cb.setOAuthAccessTokenSecret(" yours ");
      //Make the twitter object and prepare the query
      twitter = new TwitterFactory(cb.build()).getInstance();
      noLoop();
    }

    void draw() {
      getTweet();
    }

    void getTweet()
    {
      Query query = new Query("i%love%you");
      query.setRpp(1);

      //Try making the query request.
      try {
        QueryResult result = twitter.search(query);
        ArrayList tweets = (ArrayList) result.getTweets();
        Tweet t = (Tweet) tweets.get(0); //just get one for now...
        double lat=0;
        double lon=0; //These aren't doing anything right now

        String user = t.getFromUser();
        String msg = t.getText();
        Date d = t.getCreatedAt(); 

        //Attempted Geotagging
        GeoLocation g = t.getGeoLocation(); //Nothing...
        GeoQuery gq = new GeoQuery(g); //Nothing...
        GeoLocation ng = gq.getLocation();

        println(g + ":" + ng); //Nulls all around
        println(msg);

      }
      catch (TwitterException te) {
        println("Couldn't connect: " + te);
      }
    }
    void mousePressed()
    {
      redraw(); // Get a new tweet
    }