Can't run my sketch as Javascript
in
Programming Questions
•
7 months ago
I've created a sketch, using
Twitter4J. Now I can't get my sketch to run as Javascript.
I'd like to launch my work in a web browser. How can I do this?
If needed, you can view my code below:
- import java.util.ConcurrentModificationException;
- //a colour gradient, going from red to green.
- int[][] gradient = {
- {255,0,0},
- {254,1,0},
- {253,2,0},
- {252,3,0},
- {251,4,0},
- {250,5,0},
- {249,6,0},
- {248,7,0},
- {247,8,0},
- {246,9,0},
- {245,10,0},
- {244,11,0},
- {243,12,0},
- {242,13,0},
- {241,14,0},
- {240,15,0},
- {239,16,0},
- {238,17,0},
- {237,18,0},
- {236,19,0},
- {235,20,0},
- {234,21,0},
- {233,22,0},
- {232,23,0},
- {231,24,0},
- {230,25,0},
- {229,26,0},
- {228,27,0},
- {227,28,0},
- {226,29,0},
- {225,30,0},
- {224,31,0},
- {223,32,0},
- {222,33,0},
- {221,34,0},
- {220,35,0},
- {219,36,0},
- {218,37,0},
- {217,38,0},
- {216,39,0},
- {215,40,0},
- {214,41,0},
- {213,42,0},
- {212,43,0},
- {211,44,0},
- {210,45,0},
- {209,46,0},
- {208,47,0},
- {207,48,0},
- {206,49,0},
- {205,50,0},
- {204,51,0},
- {203,52,0},
- {202,53,0},
- {201,54,0},
- {200,55,0},
- {199,56,0},
- {198,57,0},
- {197,58,0},
- {196,59,0},
- {195,60,0},
- {194,61,0},
- {193,62,0},
- {192,63,0},
- {191,64,0},
- {190,65,0},
- {189,66,0},
- {188,67,0},
- {187,68,0},
- {186,69,0},
- {185,70,0},
- {184,71,0},
- {183,72,0},
- {182,73,0},
- {181,74,0},
- {180,75,0},
- {179,76,0},
- {178,77,0},
- {177,78,0},
- {176,79,0},
- {175,80,0},
- {174,81,0},
- {173,82,0},
- {172,83,0},
- {171,84,0},
- {170,85,0},
- {169,86,0},
- {168,87,0},
- {167,88,0},
- {166,89,0},
- {165,90,0},
- {164,91,0},
- {163,92,0},
- {162,93,0},
- {161,94,0},
- {160,95,0},
- {159,96,0},
- {158,97,0},
- {157,98,0},
- {156,99,0},
- {155,100,0},
- {154,101,0},
- {153,102,0},
- {152,103,0},
- {151,104,0},
- {150,105,0},
- {149,106,0},
- {148,107,0},
- {147,108,0},
- {146,109,0},
- {145,110,0},
- {144,111,0},
- {143,112,0},
- {142,113,0},
- {141,114,0},
- {140,115,0},
- {139,116,0},
- {138,117,0},
- {137,118,0},
- {136,119,0},
- {135,120,0},
- {134,121,0},
- {133,122,0},
- {132,123,0},
- {131,124,0},
- {130,125,0},
- {129,126,0},
- {128,127,0},
- {127,128,0},
- {126,129,0},
- {125,130,0},
- {124,131,0},
- {123,132,0},
- {122,133,0},
- {121,134,0},
- {120,135,0},
- {119,136,0},
- {118,137,0},
- {117,138,0},
- {116,139,0},
- {115,140,0},
- {114,141,0},
- {113,142,0},
- {112,143,0},
- {111,144,0},
- {110,145,0},
- {109,146,0},
- {108,147,0},
- {107,148,0},
- {106,149,0},
- {105,150,0},
- {104,151,0},
- {103,152,0},
- {102,153,0},
- {101,154,0},
- {100,155,0},
- {99,156,0},
- {98,157,0},
- {97,158,0},
- {96,159,0},
- {95,160,0},
- {94,161,0},
- {93,162,0},
- {92,163,0},
- {91,164,0},
- {90,165,0},
- {89,166,0},
- {88,167,0},
- {87,168,0},
- {86,169,0},
- {85,170,0},
- {84,171,0},
- {83,172,0},
- {82,173,0},
- {81,174,0},
- {80,175,0},
- {79,176,0},
- {78,177,0},
- {77,178,0},
- {76,179,0},
- {75,180,0},
- {74,181,0},
- {73,182,0},
- {72,183,0},
- {71,184,0},
- {70,185,0},
- {69,186,0},
- {68,187,0},
- {67,188,0},
- {66,189,0},
- {65,190,0},
- {64,191,0},
- {63,192,0},
- {62,193,0},
- {61,194,0},
- {60,195,0},
- {59,196,0},
- {58,197,0},
- {57,198,0},
- {56,199,0},
- {55,200,0},
- {54,201,0},
- {53,202,0},
- {52,203,0},
- {51,204,0},
- {50,205,0},
- {49,206,0},
- {48,207,0},
- {47,208,0},
- {46,209,0},
- {45,210,0},
- {44,211,0},
- {43,212,0},
- {42,213,0},
- {41,214,0},
- {40,215,0},
- {39,216,0},
- {38,217,0},
- {37,218,0},
- {36,219,0},
- {35,220,0},
- {34,221,0},
- {33,222,0},
- {32,223,0},
- {31,224,0},
- {30,225,0},
- {29,226,0},
- {28,227,0},
- {27,228,0},
- {26,229,0},
- {25,230,0},
- {24,231,0},
- {23,232,0},
- {22,233,0},
- {21,234,0},
- {20,235,0},
- {19,236,0},
- {18,237,0},
- {17,238,0},
- {16,239,0},
- {15,240,0},
- {14,241,0},
- {13,242,0},
- {12,243,0},
- {11,244,0},
- {10,245,0},
- {9,246,0},
- {8,247,0},
- {7,248,0},
- {6,249,0},
- {5,250,0},
- {4,251,0},
- {3,252,0},
- {2,253,0},
- {1,254,0},
- {0,255,0}
- };
- //the amount of tweets to look at
- int tweetLimit = 200;
- //Variables
- static String OAuthConsumerKey = "xxxxxxxxxxxxxxxxxxx";
- static String OAuthConsumerSecret = "xxxxxxxxxxxxxxxxxxx";
- static String AccessToken = "xxxxxxxxxxxxxxxxxxx";
- static String AccessTokenSecret = "xxxxxxxxxxxxxxxxxxx";
- //store all our tweets
- ArrayList < String > tweets;
- //The keywords needed for our search
- String keywords[] = {
- ":)", ":("
- };
- //the definition of what a tweet must contain to be happy
- String happy = ":)";
- //the definition of what a tweet must contain to be sad
- String sad = ":(";
- //happiness, as a number between 0 and 1
- float happiness = 0;
- //which colour is needed from the gradient
- int gradientIndex = 0;
- TwitterStream twitter = new TwitterStreamFactory().getInstance();
- void setup() {
- //Set the size of the stage, and the background to black.
- size(550, 550);
- //create our arrayList
- tweets = new ArrayList();
- connectTwitter();
- twitter.addListener(listener);
- //only listen to :) / :( tweets
- twitter.filter(new FilterQuery().track(keywords));
- } //end setup
- // Initial connection
- void connectTwitter() {
- twitter.setOAuthConsumer(OAuthConsumerKey, OAuthConsumerSecret);
- AccessToken accessToken = loadAccessToken();
- twitter.setOAuthAccessToken(accessToken);
- }
- StatusListener listener = new StatusListener() {
- public void onStatus(Status status) {
- //add the tweet at the front of our tweets ArrayList
- tweets.add(0, status.getText());
- }
- public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
- //System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
- }
- public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
- // System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
- }
- public void onScrubGeo(long userId, long upToStatusId) {
- System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
- }
- public void onException(Exception ex) {
- ex.printStackTrace();
- }
- };
- private static AccessToken loadAccessToken() {
- return new AccessToken(AccessToken, AccessTokenSecret);
- }
- void draw() {
- //the amount of happy tweets
- float happyCount = 1;
- //the amount of sad tweets
- float sadCount = 1;
- //go through all our stored tweets
- for (int i = 0; i < tweets.size(); i++) {
- if (tweets.get(i).indexOf(sad) == -1) {
- //this is a happy tweet
- happyCount++;
- } else {
- //this is a sad tweet
- sadCount++;
- }
- }
- //calculate the happiness, a number ranging between 0 and 1
- happiness = happyCount / (happyCount + sadCount);
- //find the appropiate colour
- gradientIndex = floor(happiness * 255);
- //create a rectangle to fill the screen with the appropiate colour
- fill(gradient[gradientIndex][0], gradient[gradientIndex][1], gradient[gradientIndex][2]);
- rect(0, 0, width, height);
- removeOldTweets();
- }
- void removeOldTweets() {
- //sometimes I'm getting some random ConcurrentModificationException, just use a blank catch
- try {
- if (tweets.size() > tweetLimit) {
- //only keep the most recent tweets
- tweets.subList(tweetLimit - 1, tweets.size()).clear();
- }
- } catch (ConcurrentModificationException e) {
- //do nothing
- }
- }
1