Hello Everyone,
I am trying to pass a url to the applet, but nothing seems to be working and I do not get any errors:
1. From the code below, I export to an applet and I pass a url through <param > tag with no success. The code works fine in processing if I code the url directly into the code:
Code:
import traer.physics.*;
import traer.animation.*;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.HashMap;
import processing.net.*;
import org.htmlparser.*;
import org.htmlparser.util.*;
import org.htmlparser.filters.*;
import org.htmlparser.nodes.*;
int totalNumberOfNodes = 0;
ArrayList elements = new ArrayList();
ArrayList parents = new ArrayList();
int nodesAdded = 0;
int maxDegree = 0;
HashMap particleNodeLookup = new HashMap();
HashMap particleInfoLookup = new HashMap();
ParticleSystem physics;
Smoother3D centroid;
//private String urlPath ="http://www.bing.com";
private String content;
String urlPath;
float NODE_SIZE = 30;
float EDGE_LENGTH = 50;
float EDGE_STRENGTH = 0.2;
float SPACER_STRENGTH = 2000;
final String GRAY = "155,155,155";
final String BLUE = "0,0,155";
final String ORANGE = "255,155,51";
final String YELLOW = "255,255,51";
final String RED = "255,0,0";
final String GREEN = "0,155,0";
final String VIOLET = "204,0,255";
final String BLACK = "0,0,0";
void setup() {
size(600, 400);
String urlPath = param("mypath");
//urlPath="http://www.ibm.com";
smooth();
frameRate(24);
strokeWeight(2);
ellipseMode(CENTER);
physics = new ParticleSystem( 0, 0.25 );
centroid = new Smoother3D( 0.0, 0.0, 1.0,0.8 );
initialize();
this.getDataFromClient();
}
void getDataFromClient() {
try {
org.htmlparser.Parser ps = new org.htmlparser.Parser ();
ps.setURL(urlPath);
OrFilter orf = new OrFilter();
NodeFilter[] nfls = new NodeFilter[1];
nfls[0] = new TagNameFilter("html");
orf.setPredicates(nfls);
NodeList nList = ps.parse(orf);
Node node = nList.elementAt (0);
this.parseTree(node);
EDGE_STRENGTH = (1.0 / maxDegree) * 5;
if (EDGE_STRENGTH > 0.2) EDGE_STRENGTH = 0.2;
}
catch (Exception e) {
e.printStackTrace();
}
}
void initialize() {
physics.clear();
}
void parseTree(Node node) {
int degree = 0;
if (node == null) return;
String nodeText = node.getText();
if (node instanceof TagNode && !((TagNode)node).isEndTag()) {
//println(((TagNode)node).getTagName());
totalNumberOfNodes++;
elements.add(node);
parents.add(((TagNode)node).getParent());
}
NodeList children = node.getChildren();
if (children == null) return;
SimpleNodeIterator iter = children.elements();
while(iter.hasMoreNodes()) {
Node child = iter.nextNode();
if (child instanceof TagNode && !((TagNode)child).isEndTag()) degree++;
parseTree(child);
}
if (degree > maxDegree) maxDegree = degree;
}
Particle addNode(Particle q) {
Particle p = physics.makeParticle();
if (q == null) return p;
addSpacersToNode( p, q );
makeEdgeBetween( p, q );
float randomX = (float)((Math.random() * 0.5) + 0.5);
if (Math.random() < 0.5) randomX *= -1;
float randomY = (float)((Math.random() * 0.5) + 0.5);
if (Math.random() < 0.5) randomY *= -1;
//p.moveTo( q.position().x() +randomX, q.position().y() + randomY, 0 );
p.position().set( q.position().x() +randomX, q.position().y() + randomY, 0 );
return p;
}
void draw() {
//uncomment this if you want your network saved as pdfs
//beginRecord(PDF, "frameimage-####.pdf");
if (nodesAdded < totalNumberOfNodes) {
this.addNodesToGraph();
}
else {
if (EDGE_STRENGTH < 0.05) EDGE_STRENGTH += 0.0001;
}
physics.tick( 1.0 );
if (physics.numberOfParticles() > 1) {
updateCentroid();
}
centroid.tick();
background(255);
translate(width/2, height/2);
scale(centroid.z());
translate( -centroid.x(), -centroid.y() );
drawNetwork();
//uncomment this if you want your network saved as pdfs
//endRecord();
}
void addNodesToGraph() {
Particle newParticle;
TagNode tagNodeToAdd = (TagNode)elements.get(nodesAdded);
Node parentNode = (Node)parents.get(nodesAdded);
if (parentNode == null) {
// this is the html element
newParticle = addNode(null);
}
else {
Particle parentParticle = (Particle)particleNodeLookup.get(parentNode);
newParticle = addNode(parentParticle);
}
particleNodeLookup.put(tagNodeToAdd,newParticle);
String nodeColor = GRAY;
if (tagNodeToAdd.getTagName().equalsIgnoreCase("a")) nodeColor = BLUE;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("div")) nodeColor = GREEN;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("html")) nodeColor = BLACK;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("tr")) nodeColor = RED;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("td")) nodeColor = RED;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("table")) nodeColor = RED;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("br")) nodeColor = GRAY;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("p")) nodeColor = ORANGE;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("blockquote")) nodeColor = ORANGE;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("img")) nodeColor = VIOLET;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("form")) nodeColor = YELLOW;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("input")) nodeColor = YELLOW;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("textarea")) nodeColor = YELLOW;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("select")) nodeColor = YELLOW;
else if (tagNodeToAdd.getTagName().equalsIgnoreCase("option")) nodeColor = YELLOW;
particleInfoLookup.put(newParticle,nodeColor);
nodesAdded++; Sorry, could not paste the whole code
Please help, if you can