如何在浏览器中运行Processing + Twitter4j草图 [英] How to run Processing+Twitter4j sketch in broswer

查看:79
本文介绍了如何在浏览器中运行Processing + Twitter4j草图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从事处理的新手程序员,最近我开发了使用Twitter4j的草图.当我从Processing Development Environment以Java模式运行草图时,它可以完美地工作.另外,当我从Java模式导出草图时,生成的应用程序可以完美运行.但是,当我切换到JavaScript模式并尝试运行草图时,浏览器预览不会显示任何内容.

I'm a newbie programmer practicing Processing and I recently developed a sketch that uses Twitter4j. When I run the sketch in Java mode from the Processing Development Environment it works perfectly. Also, when I export the sketch from Java mode the resulting application runs perfectly. However, when I switch to JavaScript mode and try to run the sketch the browser preview does not display anything.

我认为问题与Twitter4j有关,因为当我从草图中删除与Twitter4j相关的代码并运行它(以JavaScript模式)时,浏览器预览然后显示正在处理的视觉效果.

I believe the problem is related to Twitter4j because when I remove the Twitter4j-related code from the sketch and run it (in JavaScript mode) the browser preview then displays the Processing visuals.

我一直在寻找解决方案,历时数天之久,但历时数天,但找不到合适的答案.我遇到过涉及相关问题的帖子,但答案仅涉及诸如签署JAR文件"之类的术语(我认为仅与Java模式相关),或者仅揭示了在浏览器上以JavaScript模式运行处理草图的问题. -排除涉及使用Twitter4j的草图.

I've been searching for a solution to this now for several hours spanning several days but I cannot find an adequate answer. I've come across posts addressing related issues but the answers have only involved terms like "signing the JAR file" (related, I think, only to Java mode) or have only shed light on problems running Processing sketches in JavaScript mode on the browser -- excluding sketches that involve the use of Twitter4j.

我在下面提供了完整代码的副本,非常感谢您针对新手的逐步解答.我正在寻找一种解释,如果可能的话,要使我的JavaScript模式导出的Processing + Twitter4j草图在HTML/我的网站上运行.

I've included a copy of my entire code below and would really appreciate a step-by-step answer, catering to a newbie. I'm searching for an explanation of what it takes, if possible, to get my JavaScript-mode-exported Processing + Twitter4j sketch running in HTML/my website.

感谢您的时间和技能.在这篇文章的结尾,我在代码中包含了一些注释.

Thanks for your time and your skills. I've included some notes on the code at the end of this post.

import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;

Twitter twitter;
String searchString = "#brokendreams";
List<Status> tweets;
int currentTweet;
PFont f;

int amount1 = 300;
Star[] meteors = new Star[45];
StarDust[] sprinkle = {};
String chatter;

void setup() {
  size(800,600);
  f = createFont("Tahoma", 13, true);
  textFont(f, 13);

  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey("---");
  cb.setOAuthConsumerSecret("---");
  cb.setOAuthAccessToken("---");
  cb.setOAuthAccessTokenSecret("---");

  TwitterFactory tf = new TwitterFactory(cb.build());

  twitter = tf.getInstance();

  getNewTweets();

  currentTweet = 0;

  dustField();

  Status status = tweets.get(currentTweet);

  for (int j = 0; j < meteors.length; j++) {
    meteors[j] = new Star(chatterText());
  }

}

void draw() {
  background(0, 35, 0);
  for (int i = 0; i < sprinkle.length; i++) {
    StarDust dust = sprinkle[i];
    dust.move();
  }

  for (int j = 0; j < meteors.length; j++) {
    meteors[j].appear();
    meteors[j].randomOrbit();
  }

  chatterText();

}

void dustField() {
  for (int i = 0; i < amount1; i++) {
    StarDust dust = new StarDust();
    dust.swish();
    sprinkle = (StarDust[])append(sprinkle, dust);
  }
}

String chatterText() {
  currentTweet += 1;
  if (currentTweet >= tweets.size()) {
    currentTweet = 0;
  }
  Status status = tweets.get(currentTweet);
  chatter = status.getText();
  return chatter;
}

void getNewTweets() {
    try {
      Query query = new Query(searchString);
      QueryResult result = twitter.search(query);
      tweets = result.getTweets();
    } catch (TwitterException te) {
      System.out.println("Failed to search tweets: " + te.getMessage());
      System.exit(-1);
    } 
}

void refreshTweets() {
  while (true) {
    getNewTweets();
    println("Updated Tweets"); 
    delay(30000);
  }
}
class Star {

  float x, y;
  float xmove, ymove;
  float radius;
  color linecol, fillcol;
  float alpha;
  String msg;
  float frag1, frag2, frag3, frag4;

  Status status;

  Star(String message) {

    msg = message;
    x = random(width);
    y = random(height);
    radius = random(20, 60) + 10;
    linecol = color(random(255), random(255), random(255));
    fillcol = color(random(255), random(255), random(255));
    alpha = random(200);
    xmove = sin(random(2) / 2);
    ymove = sin(random(2) / 2);
    frag1 = random(radius/5);
    frag2 = random(radius/3);
    frag3 = random(radius/4);
    frag4 = random(radius/2);

  }

  void appear() {

    if (mouseX > x-(radius/2) && mouseX < x+(radius/2) && mouseY > y-(radius/2) && mouseY < y+(radius/2)) {

      strokeWeight(1);
      stroke(linecol);
      noFill();
      ellipse(x, y, radius, radius);

    } else {

      noStroke();
      fill(fillcol, alpha);
      ellipse(x, y, radius, radius);

    }

    fill(fillcol);
    noStroke();
    beginShape();
    vertex(x - (radius/3), y + frag1 - frag2);
    vertex(x, y - (radius/3));
    vertex(x, y);
    endShape(CLOSE);

    fill(linecol, alpha);
    beginShape();
    vertex(x, y);
    vertex(x, y - (radius/3));
    vertex(x + (radius/4), y - frag3 - frag4);
    vertex(x + (radius/3), y + frag1 - frag2);
    endShape(CLOSE);

    fill(fillcol);
    beginShape();
    vertex(x, y);
    vertex(x + (radius/3), y + frag1 - frag2);
    vertex(x + (radius/4), y + frag3);
    endShape(CLOSE);

    fill(linecol, alpha);
    beginShape();
    vertex(x, y);
    vertex(x + (radius/4), y + frag3);
    vertex(x + (radius/5), y + frag2);
    vertex(x, y + (radius/3));
    vertex(x - (radius/3), y + frag1 - frag2);
    endShape(CLOSE);

  }

  void randomOrbit() {

    x += xmove;
    y += ymove;
    if (x > (width + radius)) { x = 0 - radius; }
    if (x < (0 - radius)) { x = width + radius; }
    if (y > (height + radius)) { y = 0 - radius; }
    if (y < (0 - radius)) { y = height + radius; }
    appear();
    displayTweet();

  }

  void displayTweet() {

    if (mouseX > x-(radius/2) && mouseX < x+(radius/2) && mouseY > y-(radius/2) && mouseY < y+(radius/2) && mousePressed) {
      x = mouseX;
      y = mouseY;
      fill(255);
      text(msg, mouseX + 30, mouseY, 200, 200);
      mouseReleased();

    }

  }

  void mouseReleased() {alpha = 80;}

}
class StarDust {

  float x, y;
  float xmove, ymove;
  float radius;
  float alpha, beta;

  Status status;

  StarDust() {
    x = random(width);
    y = random(height);
    radius = random(2, 4);
    alpha = random(255);
    beta = random(155);
    xmove = random(1.9) - 0.9;
    ymove = random(1.9) - 0.9;

  }

  void flipColor() {

      fill(random(255), random(255), random(255), beta);
      stroke(random(255), random(255), random(255), beta);

  }

  void swish() {

    flipColor();
    rect(x, y, radius, radius);

  }

  void move() {

    swish();
    x += xmove;
    y += ymove;
    if (x > (width + radius)) { x = 0 - radius; }
    if (x < (0 - radius)) { x = width + radius; }
    if (y > (height + radius)) { y = 0 - radius; }
    if (y < (0 - radius)) { y = height + radius; }
    squarespray(xmove);

  }

  void squarespray(float spray) {
    stroke(random(175, 255), random(255), 0, alpha);
    strokeWeight(random(7));
    point(x + random(radius), y + random(spray));
    point(y + sin(spray) * radius, x - spray);
    point(random(spray) - 60 + y, random(radius) + x - 30);
  }

}

-对不起,我没有评论.这是我仍在培养的习惯.但是,在涉及Twitter4j的主要话题之后,我认为没有任何意义.

-Sorry I didn't comment. This is a habit I'm still working on developing. However I don't believe anything is relevant after the main lines involving Twitter4j.

-在"---"上方的代码中,代替了我的钥匙.

-In the code above "---" is a substitute for my keys.

-这是与我从JavaScript模式导出的代码完全相同的代码,并且希望从我的浏览器运行.

-This is the code otherwise exactly as I've exported it from JavaScript mode and would like to run from my browser.

-我以前在我的网站上运行过处理草图";这是第一次涉及Twitter4j.​​

-I've run Processing sketches on my website before; this is the first involving Twitter4j.

推荐答案

您的评论正确:您的草图在JavaScript模式下不起作用,因为您使用的是Java库.只要您依赖Java库,您的代码就只能在Java模式下工作.您可能想查看JavaScript控制台中的错误消息.

Your comment is correct: your sketch won't work under JavaScript mode, because you're using a Java library. As long as you depend on a Java library, your code will only work in Java mode. You might want to check out the JavaScript console for error messages.

要解决此问题,您可以尝试查找等效的JavaScript库,并使用该库代替twitter4j.然后您的代码将在JavaScript模式下工作.这可能是正确的事情.

To solve the issue, you could try to look for an equivalent JavaScript library and use that instead of twitter4j. Then your code would work in JavaScript mode. That's probably the correct thing to do.

我之所以这样说,是因为要让Java应用程序在浏览器中运行非常痛苦.您必须创建一个applet,然后使用付费证书在jar上签名,或者让您的用户修改其Java安全设置-即使这样,您的用户在运行applet之前也会收到一系列安全警告和提示.

The reason I say that is it's a huge pain to get Java applications to run in the browser. You'd have to create an applet, and then either sign the jar with a paid certificate, or have your users modify their Java security settings- and even then, your users will get a bunch of security warnings and prompts before they run your applet.

最好将其部署为可运行的jar或特定于平台的可执行文件,或者通过查找等效的JavaScript库完全切换到JavaScript模式.

You're better off either deploying as a runnable jar or platform-specific executable, or switching entirely to JavaScript mode by finding an equivalent JavaScript library.

这篇关于如何在浏览器中运行Processing + Twitter4j草图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆