We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Web Slowdown /& Bad Code
Page Index Toggle Pages: 1
Web Slowdown /& Bad Code? (Read 1208 times)
Web Slowdown /& Bad Code?
Apr 22nd, 2005, 4:12am
 
I have created a small applet that generates arrows and then moves them to the cursor then clicked, and then back to their original position when released.  The applet runs great when I run it form my computer but on the web it runs terribly for me and multiple other people I have had look at it.  Is it my sloppy, non-coding knowledge coding or just a Java problem.  Any suggestions welcomed with open arms.

Code:

void setup() {
size (200,200);
}

//set up variables
int NUM=10;
boolean start=true;
int[] x1 = new int[NUM];
int[] y1 = new int[NUM];
int[] x1old = new int[NUM];
int[] y1old = new int[NUM];
int x1new;
int y1new;
boolean end1=false;

void draw() {
background(255);
//fill arrays for beginning positions once
if (start==true){
for (int i=0; i<NUM; i++){
x1[i]=int(random(20,width-20));
y1[i]=int(random(5,height-5));
x1old[i]=x1[i];
y1old[i]=y1[i];
}
start=false;
}
//draw arrows
for(int i=0; i<NUM; i++) {
pushMatrix();
translate(x1[i],y1[i]);
rotate(radians(360)/NUM*i);
line(-20,0,0,0);
line(0,0,-5,-5);
line(0,0,-5,+5);
popMatrix();
}
//translate when mouse pressed
if (mousePressed==true) {
end1=false;
x1new=mouseX;
y1new=mouseY;
for (int i=0;i<NUM;i++) {
if (y1new>y1[i]) {
y1[i]++;
}
if (y1new<y1[i]) {
y1[i]--;
}
if (x1new>x1[i]) {
x1[i]++;
}
if (x1new<x1[i]) {
x1[i]--;
}
}
}
//snap back to position when mouse is relased *below* is triggered
if (end1==true) {
for (int i=0;i<NUM;i++) {
if (y1old[i]>y1[i]) {
y1[i]++;
}
if (y1old[i]<y1[i]) {
y1[i]--;
}
if (x1old[i]>x1[i]) {
x1[i]++;
}
if (x1old[i]<x1[i]) {
x1[i]--;
}
}
}
}

void mouseReleased() {
end1=true;
}

Hope it is easy to understand.
-sean!
Re: Web Slowdown /& Bad Code?
Reply #1 - Apr 22nd, 2005, 11:03am
 
How slow is slow? There shouldn't be too large a discrepancy in my experience between the sketch and export versions. So, it could be another issue.

I've made a couple of very quick changes, but nothing that's going to greatly affect the speed of things.

The start flag isn't required if you chuck the initialization stuff into setup and the same goes for the mouse test stuff.


Code:


//set up variables
int NUM=20;
int[] x1 = new int[NUM];
int[] y1 = new int[NUM];
int[] x1old = new int[NUM];
int[] y1old = new int[NUM];
int x1new;
int y1new;


void setup() {
size (200,200);

for (int i=0; i<NUM; i++){
x1[i]=int(random(20,width-20));
y1[i]=int(random(5,height-5));
x1old[i]=x1[i];
y1old[i]=y1[i];
}

}

void draw() {
background(255);

//draw arrows
for(int i=0; i<NUM; i++) {
pushMatrix();
translate(x1[i],y1[i]);
rotate(radians(360)/NUM*i);
line(-20,0,0,0);
line(0,0,-5,-5);
line(0,0,-5,+5);
popMatrix();
}

//translate when mouse pressed
if (mousePressed) {

x1new=mouseX;
y1new=mouseY;

for (int i=0;i<NUM;i++) {
if (y1new>y1[i]) {
y1[i]++;
}
if (y1new<y1[i]) {
y1[i]--;
}
if (x1new>x1[i]) {
x1[i]++;
}
if (x1new<x1[i]) {
x1[i]--;
}
}
}
//snap back to position when mouse is relased *below* is triggered
else {
for (int i=0;i<NUM;i++) {
if (y1old[i]>y1[i]) {
y1[i]++;
}
if (y1old[i]<y1[i]) {
y1[i]--;
}
if (x1old[i]>x1[i]) {
x1[i]++;
}
if (x1old[i]<x1[i]) {
x1[i]--;
}
}
}
}

void mouseReleased() {

for (int i=0; i<NUM; i++){
x1old[i]=int(random(20,width-20));
y1old[i]=int(random(5,height-5));
}
}

Re: Web Slowdown /& Bad Code
Reply #2 - Apr 22nd, 2005, 5:02pm
 
Thanks a lot Mark.  Learn a little bit every day.  By slow I mean that when I run the sketch from processing the arrows converge at the mouse in around 1 second and when viewing them in a browser (IE or Firefox) it takes upwards of 10 seconds.  Starting to think its a java problem, I tried to mess around with the java plug-in in control panel but no luck.

Side-note: Really enjoyed the pieces you have on your website Mark.  The style is very appealing to me.
Re: Web Slowdown /& Bad Code?
Reply #3 - Apr 22nd, 2005, 9:17pm
 
do you have java 1.5 installed? if so, that may be what's running in your browser, and causing things to run poorly (since the environment uses 1.4).
Re: Web Slowdown /& Bad Code?
Reply #4 - May 6th, 2005, 6:16pm
 
Strangely enough I had 1.4.2 installed before when it was slow, I recently upgraded to 1.5 because I had to have it to run another program and now they are equally fast on the web and from within the program.  So 1.5 seemed to fix the problem, but I haven't worked in processing since I downloaded to find any problems.
Page Index Toggle Pages: 1