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 › Load new applet on button press
Page Index Toggle Pages: 1
Load new applet on button press (Read 1106 times)
Load new applet on button press
Nov 26th, 2009, 1:15am
 
Hi I am trying to load a new applet when the next button is pressed. The sample program below loads in a text file of 2 quotes. When the program initially starts it loads each line of the text file into an array.

It then displays the first element, or quote, of the array using the currentQuote property. What I am trying to do is load a new applet when the user clicks the next button and this will increment the currentQuote by one. I could just set the label that the quote is displayed on to blank and put the next quote on it, but I'd rather load it again (basically start a blank canvas).

Here is the code:
Code:

import processing.core.*;
import interfascia.*;
import guicomponents.*;


public class QuoteOfTheDay extends PApplet
{
 GUIController c;
 GLabel quoteLbl;
 IFButton nextButton;
 BufferedReader quoteReader;
 int numOfQuotes = 2;
 int currentQuote = 0; //could possibly be static
 String[] quotes = new String[numOfQuotes];
 String quote;
 
 public void setup()
 {
   size(800, 200);
   c = new GUIController(this);
   
   //Load the file for the quotes
   quote = dataPath("quotes.txt");
   try
   {
     quoteReader = new BufferedReader (new FileReader (quote));
     for (int i = 0; i < quotes.length; i++)
     {
       quote = quoteReader.readLine();
       quotes[i] = quote;
     }
   }
   catch (IOException e)
   {
     println ("Error reading file " + e);
   }
   
   
   //Display the quote in the label
   quoteLbl = new GLabel(this, quotes[currentQuote], 10, 10, 500, 20);
   quoteLbl.setBorder(1);
   quoteLbl.setOpaque(true);
   quoteLbl.setFont("Arial", 22);
   
   //Set up the button to display the next quote
   nextButton = new IFButton("NEXT", 100, 100);
   nextButton.addActionListener(this);
   c.add(nextButton);
 }
 
 public void draw()
 {
 }
 
 public void actionPerformed(GUIEvent e)
 {
   if (e.getSource() == nextButton)
   {
     currentQuote++;
     //println(currentQuote);
     //Clear the window and load the next quote
   }
 }
}


By the way this is my first post so I am a processing newbie.
Re: Load new applet on button press
Reply #1 - Nov 26th, 2009, 2:40am
 
Loading a new applet to start a blank canvas is overkill, hard to do, inefficient... Smiley

Just use background() instead! Grin

Note: Processing offers some facilities, for example instead of doing your loop to read your file (which is OK), you can just call loadStrings(). Little things, but that's what makes it friendly to newbies.
Re: Load new applet on button press
Reply #2 - Nov 26th, 2009, 2:55am
 
Thanks for the quick reply PhiLho.

Where would I put this background() call? Or how can I call it from inside the actionPerformed function? Also I am gonna set an image for the background, i.e. background(myImage), so will this still be the same?

Ye I had a quick look at the loadStrings() function and that is a more efficient way of loading text. I was initially put off by the fact that it loads each line of text as an element of the array (this was before I was using .split()), but that is what my program is doing anyway. Note to self: check reference first.

Now:
Code:

   /*Load the file for the quotes
   quote = dataPath("quotes.txt");
   try
   {
     quoteReader = new BufferedReader (new FileReader (quote));
     for (int i = 0; i < quotes.length; i++)
     {
       quote = quoteReader.readLine();
       quotes[i] = quote;
     }
   }
   catch (IOException e)
   {
     println ("Error reading file " + e);
   }*/


becomes:
Code:

quotes = loadStrings("quotes.txt");


So much better.

Now if I can just get it to reload again and display the next quote.
Re: Load new applet on button press
Reply #3 - Nov 26th, 2009, 6:03am
 
if you just call background before       currentQuote++; it should work.
Re: Load new applet on button press
Reply #4 - Nov 26th, 2009, 7:08am
 
Ye that works great. I had to call setup() again in the actionPerformed() function as well as backgroud(backgroundColour).

Thanks.
Re: Load new applet on button press
Reply #5 - Nov 26th, 2009, 7:38am
 
hadoken13 wrote on Nov 26th, 2009, 7:08am:
I had to call setup() again in the actionPerformed() function

Bad idea...
You should put in a separate function all things to init to redisplay things, and call it from setup().
No need to call size() again, to re-initialize the GUI, to reload the file, etc.
Re: Load new applet on button press
Reply #6 - Nov 30th, 2009, 9:31am
 
That makes sense.

I was trying another bit of coding (it's a bit of a mess, was in a rush so bare with me):

Code:

import processing.core.*;
import interfascia.*;
import guicomponents.*;


public class QuoteOfTheDay extends PApplet
{
 GUIController c;
 GLabel quoteLbl, languageLbl;
 IFButton nextBtn, backBtn, englishBtn, frenchBtn;
 BufferedReader quoteReader;
 int numOfQuotes = 2;
 int currentQuote = 0; //could possibly be static
 int numScreens = 0;
 boolean screenCreated = false;
 String[] quotes = new String[numOfQuotes];
 String quote;
 String choice= "";
 
 public void setup()
 {
   size(550, 250);
   background(100);
   if (numScreens == 0)
   {
     setupHomeScreen();
   }
 }
 
 public void draw()
 {
 }
 
 public void setupHomeScreen()
 {
   c = new GUIController(this);
   
   languageLbl = new GLabel(this, "Please select English or French", 10, 10, 200, 20);
   languageLbl.setBorder(1);
   languageLbl.setOpaque(true);
   languageLbl.setFont("Arial", 22);
   
   englishBtn = new IFButton("English", 70, 100);
   englishBtn.addActionListener(this);
   c.add(englishBtn);
   frenchBtn = new IFButton("French", 200, 100);
   frenchBtn.addActionListener(this);
   c.add(frenchBtn);
   numScreens++;
 }
 
 public void setupMainScreen(String quote)
 {
   c = new GUIController(this);

   //Display the quote in the label
   quoteLbl = new GLabel(this, quote, 10, 10, 600, 20);
   quoteLbl.setBorder(1);
   quoteLbl.setOpaque(true);
   quoteLbl.setFont("Arial", 22);
   
   //Set up the button to display the next quote
   nextBtn = new IFButton("NEXT", 70, 200);
   nextBtn.addActionListener(this);
   c.add(nextBtn);
   
   //Set up the button to allow user to go back to home screen
   backBtn = new IFButton("BACK", 200, 200);
   backBtn.addActionListener(this);
   c.add(backBtn);
 }
 
 public void actionPerformed(GUIEvent e)
 {
   if (e.getSource() == englishBtn)
   {
     quotes = loadStrings("englishQuotes.txt");
     choice = "english";
     setupMainScreen(quotes[currentQuote]);
   }
   else if (e.getSource() == frenchBtn)
   {
     quotes = loadStrings("frenchQuotes.txt");
     choice = "french";
     setupMainScreen(quotes[currentQuote]);
   }
   else if (e.getSource() == nextBtn)
   {
     if (choice.equals(" english"))
     {
       currentQuote++;
       setupMainScreen(quotes[currentQuote]);
     }
     else
     {
       currentQuote++;
       setupMainScreen(quotes[currentQuote]);
     }
   }
   else if (e.getSource() == backBtn)
   {
     currentQuote = 0;
     numScreens = 0;
     setup();
   }
 }
}


I know there are a lot of things wrong with this. Also when I load the next screen the previous screen's buttons and label are still showing.

I tried background() in the draw() function and calling redraw() when the buttons were pressed but still no luck. I'd say it's something simple I am overlooking.
Page Index Toggle Pages: 1