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 › Applet help
Page Index Toggle Pages: 1
Applet help (Read 2136 times)
Applet help
Jun 12th, 2009, 1:07pm
 
I'm having some serious problems with an applet embedded in a website. http : // lashextensions . ie / gallery . htm (without the spaces, sorry, I don't currently have enough posts to post a link) is the site and the gallery on it only works about 2/3's of the time. It's very strange, just press refresh if it works the first time. Could someone please help?

Code:

PImage[] images = new PImage[7];
int x;
PFont font;
boolean released,drawIt,loadIt,first;
void setup(){
 size(640,480);
 for(int i = 1; i <= 7; i++){
   images[i-1] = null;
 }
 x = 1;
 font = loadFont("ArialMT-32.vlw");
 textFont(font,32);
 text("Loading...",250,height/2);
 first = true;
 drawIt = true;
 loadIt = true;
}
void draw(){


 if(drawIt){
   if(loadIt){
     images[x-1] = loadImage("images/"+x+".jpg");

   }
   drawIt = false;
   loadIt = false;
   background(0);
   image(images[x-1],(width - images[x-1].width)/2,(height - images[x-1].height)/2);
   fill(0,0,0,100);
   if(first){
     noStroke();
     rect(0,height - 70,width,70);
     fill(250);
     text("Click for next image",190,height - 25);
     first = false;
   }
 }

 if(mousePressed && released){
   if (x == 7){
     x = 1;
   }
   else{
     x = x+1;
   }
   if(images[x-1] ==  null){    
     loadIt = true;
   }
   fill(0);
   rect(0,height - 70,width,70);
   fill(250);
   text("Loading...",250,height - 25);
   drawIt = true;
   released = false;
 }

 if(!mousePressed){
   released = true;
 }
}


I named the images 1.jpg - 7.jpg in a "images" folder.

P.S. It works 100% of the time when I run the code from the computer.
Re: Applet help
Reply #1 - Jun 12th, 2009, 2:01pm
 
The reason that it works on your computer and not in the browser is that you can't refresh Processing, you always start the program from scratch each time you run it.

When you refresh the browser the Applets (you have 2 on the page) are 'torndown' and reload. This is the output from the Java Console in Firefox when your page is refreshed.

Applet Status: Starting applet teardown
Applet Status: Finished applet teardown
Applet Status: Starting applet teardown
Applet Status: Finished applet teardown
Applet Status: Applet loaded.
Applet Status: Applet resized and added to parent container
Applet Status: Applet initialized
Applet Status: Applet made visible
Applet Status: Starting applet
Applet Status: Applet started
Applet Status: Told clients applet is started
Applet Status: Applet loaded.
Applet Status: Applet resized and added to parent container
Applet Status: Applet initialized
Applet Status: Applet made visible
Applet Status: Starting applet
Applet Status: Applet started
Applet Status: Told clients applet is started

From previous experience I suspect that although the Applet is being reloaded it is coming from the browsers file cache rather than the web server. This means that the Applet is restarted but some of the variables are still using their old values.

You could add a void stop() method to your Applet to clear out the image array and reinitialse all the variables to their starting point.
The stop() method is called when an Applet is ended due to refresh or move to another page.

You might also consider loading all the images in setup() as you only have 7 images the delay on loading would not be that great. This would simplify the coding dramatically.

I don't know if these suggestions will solve the problem but I'll keep my fingers crossed.
Smiley
Re: Applet help
Reply #2 - Jun 12th, 2009, 3:07pm
 
Ok, that's cool. I added the void stop() method. It works a lot better now but still sometimes the first image just doesn't display. I don't want to load all the images at once because that would take ages to load. I'm hoping to put about 100 images on this program soon so I don't think that would be a viable option. Do you have any ideas to fix my minor problem?
Re: Applet help
Reply #3 - Jun 13th, 2009, 11:38am
 
I don't know why I didn't think of this before there is another method that you could use void start(). This method is called after the Applet has been initialised and before the draw method.  I  have copied the comments for this method from the Java source files.
Code:
    /**
    * Called by the browser or applet viewer to inform
    * this applet that it should start its execution. It is called after
    * the <code>init</code> method and each time the applet is revisited
    * in a Web page.
    * <p>
    * A subclass of <code>Applet</code> should override this method if
    * it has any operation that it wants to perform each time the Web
    * page containing it is visited. For example, an applet with
    * animation might want to use the <code>start</code> method to
    * resume animation, and the <code>stop</code> method to suspend the
    * animation.
    * <p>
    * Note: some methods, such as <code>getLocationOnScreen</code>, can only
    * provide meaningful results if the applet is showing.  Because
    * <code>isShowing</code> returns <code>false</code> when the applet's
    * <code>start</code> is first called, methods requiring
    * <code>isShowing</code> to return <code>true</code> should be called from
    * a <code>ComponentListener</code>.


I am not too sure why the first image is showing but I suggest that you load the first image either in setup() or start() leaving the rest when the user clicks the button. In your code at present the first image is not being loaded until the Applet is visible - might solve your problem.
Smiley
Re: Applet help
Reply #4 - Jun 18th, 2009, 3:54pm
 
Sorry it took me so long to get back. Hmm I didn't add a void start() method but I did something! And it worked!

Here's the code:

Code:

PFont font;
int maxim;
String[] textArray;
boolean locked;
float pos,sPos,x;

void setup(){

size(300,400);
background(0);
font = loadFont("ArialMT-11.vlw");
textFont(font,11);
x = 1;
sPos = 3;
textArray = loadStrings("text.txt");
// doesnt work after 135 lines
noStroke();
smooth();
}

void draw(){
background(0);
fill(25);
rect(width-8,0,6,height);

for(int i = 0; i <= textArray.length - 1; i++){
fill(200);
text(textArray[i], 5, (i + 1) * 11 + x);
}

maxim = (textArray.length*11)-(height/2);
pos = sPos/(height-52);
x = 10-pos*maxim;
if(mousePressed && over() || locked) {
locked = true;
if (mouseY-50 <= 2){
sPos = 3;
} else if (mouseY+50 >= height - 2){
sPos = height - 103;
} else {
sPos = mouseY - 50;
}
}
if(!mousePressed) {
locked = false;
}
fill(240);
rect(width - 8, sPos, 6,100);
ellipse(width - 5, sPos,6,6);
ellipse(width - 5, sPos+100,6,6);
println(textArray.length);
}
boolean over(){
return mouseX > width - 8 && mouseX < width - 2 && mouseY > sPos && mouseY < sPos + 100;
}

Page Index Toggle Pages: 1