FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   native and foreign loadStrings()
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: native and foreign loadStrings()  (Read 1899 times)
st33d

WWW Email
native and foreign loadStrings()
« on: Dec 15th, 2004, 1:09pm »

I'm working on a plotter that reacts to html pulled off of the web. The basic most basic test of this is this:
 
Code:

TextField inputLine = new TextField("input URL", 30);
float m = 1.0; //intial scaling magnification
memory mem = new memory (0.0,0.0);
 
void setup() {
 
  background(255);
  size(400, 400);
  smooth();
  add(inputLine);
  mem.stack (0.0,0.0);
}
 
void loop(){
  translate(200,200);
}
 
void mousePressed(){
  String iLine = inputLine.getText();
  //remove the need to input http protocol EVERY TIME
  if (!iLine.substring(0,7).equals("http://") ){
    iLine = "http://" + iLine;
  } else if (!iLine.substring(0,7).equals("http://www.")){
    iLine = "http://www." + iLine;
  }
  remove(inputLine);
  println("request: " + iLine);
  req(iLine);
}
 
void req (String request){
  String lines[] = loadStrings(request);
  String one = join(lines, " ");
  char[] cha = new char[one.length()];
  one.getChars(0,one.length(),cha,0);
  for (int i=0; i < cha.length; i++) {
    if  (Character.isDigit(cha[i])){
 float theta = (TWO_PI / 10) * Character.getNumericValue(cha[i]);
 float newX = mem.x[mem.x.length-1] + (cos(theta) * 100.0);
 float newY = mem.y[mem.y.length-1] + (sin(theta) * 100.0);
 mem.stack(newX, newY);
    }
  }
  drawIt();
}
 
// scaling function to shrink drawing to applet size
void drawIt(){
  float xmax = mem.x[0];
  float ymax = mem.y[0];
  for (int i = 0; i < mem.x.length; i++){
    if (abs(mem.x[i]) > width / 2 && abs(mem.x[i]) > xmax){
 xmax = abs(mem.x[i]);
 m = (width/2)/xmax;
    } else if (abs(mem.y[i]) > height / 2 && abs(mem.y[i]) > ymax){
 ymax = abs(mem.y[i]);
 m = (height/2)/ ymax;
    }
  }
  ellipse(mem.x[0]-5, mem.y[0]-5, 10, 10);
  for (int i = 1; i < mem.x.length; i++){
    line (mem.x[i-1]*m, mem.y[i-1]*m, mem.x[i]*m, mem.y[i]*m);
  }
  println(mem.x[mem.x.length-1]*m);
  println("done");
  ellipse(mem.x[mem.x.length-1]-5, mem.y[mem.y.length-1]-5, 10, 10);
}
 
//storage for co-ords to be scaled down
class memory{
  float [] x=new float[1];
  float [] y=new float[1];
  memory (float xt,float yt){
    x[0]=xt;
    y[0]=xt;
  }
  void stack (float xa, float ya){
    float[] tempx = new float[x.length + 1];
    System.arraycopy(x, 0, tempx, 0, x.length);
    tempx[x.length] = x[x.length-1];
    x = tempx;
    float[] tempy = new float[y.length + 1];
    System.arraycopy(y, 0, tempy, 0, y.length);
    tempy[y.length] = y[y.length-1];
    y = tempy;
    x[x.length-1]=xa;
    y[y.length-1]=ya;
  }
}

 
Go on, just type in processing.org. See, it turns the numbers into a rather basic drawing.
 
Now export it.
 
Doesn't work on mine. However, if you look at the version I put on my site for testing and enter any URL native to the site it works:
 
http://www.st33d.net/processing/html.html
 
Request anything outside and it doesn't.
 
So I've gathered that loadStrings() becomes strictly native when exported. I was thinking of seeing if there was another language which didn't have this limitation which could then write a document with a param to pass to an image drawing applet. But I'm also wondering if there is a work around to force loadStrings() to stop being exclusively native when exported. Can anyone help?
 

I could murder a pint.
toxi

WWW
Re: native and foreign loadStrings()
« Reply #1 on: Dec 15th, 2004, 2:37pm »

here you have to blame java's security restrictions for applets. by default an applet has only access to files within the same domain as the applet host. when running your sketch inside the PDE this restriction doesn't apply.
 
to get out of the dilemma either use some server side script which loads the external data from a different domain and passes it back to your applet (see below) or sign your applet with the jarsigner tool which comes with your JDK.
 
here's some minimal PHP to forward any URL to your applet. just put it somewhere on your server which also hosts the applet:
 
Code:
<?php
readfile($_GET['url']);
?>

 
then in your sketch do:
 
Code:
String request="http://processing.org";
String lines[] = loadStrings("loadexternal.php?url="+request);

 
a signed version of your applet is here:
http://www.toxi.co.uk/p5/permissions/4st33d/
« Last Edit: Dec 15th, 2004, 2:38pm by toxi »  

http://toxi.co.uk/
st33d

WWW Email
Re: native and foreign loadStrings()
« Reply #2 on: Dec 16th, 2004, 8:30pm »

I was tempted to complain that I haven't purchased server-side-script but I checked my hosts and I get php for free. Can't complain. As for signing the applet I shall try to get my head around it (downloading JDK right now) the future of my project requires the URL request to be dynamic. Thanks for the tip. (I'm hoping I just have to load up one of the exported components and alter it as you've directed but I honestly haven't got a clue as to Java's syntax priorities.)
 

I could murder a pint.
Pages: 1 

« Previous topic | Next topic »