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 › need help big time
Page Index Toggle Pages: 1
need help big time (Read 639 times)
need help big time
Oct 19th, 2009, 3:19pm
 
Hello to whoever is reading this.  I'm new at this, so please help me.
These length of codes are supported to do when every time a key is pressed, a letter and associated image are shown on the screen.  Plus, when another key is pressed, the previous letter and image are hidden.

Every time a key is pressed a letter is shown on the screen; however, the associated image is not shown on the screen.  I don't know how to make it works.  


PFont font; //store font data
//int letterSize = 100;  
char letter = 'a';  
//Declare variable letter of data type char and assign a to letter

int maxImages = 26; // Total # of images
int imageIndex = 1; // Initial image to be displayed is the first

PImage img; //images are stored in variables of the PImage data type

void setup()  
//the code inside setup() runs once when the program first starts
{
 size(500, 500);
 //the dislay window is 500 wide pixels and 500 high pixels
 smooth(); //smooth the edge of the font letter
 font = loadFont("Candara-Bold-80.vlw");  //load the font to the screen
 textFont(font); //set the current font
 textSize(100);  //increase the font size from 80 to 100
 
  for(int i = imageIndex; i < maxImages; i +=1)
  //for(int; test; update)
  //{ statement }
  //if the test is true, the statement will run continously.
  //int assign initial value to the variable used in the test
  //update is used to modify the variable after each iteration through the block.
 
   {
     img = loadImage( "letter1.png" );
     imageMode(CENTER);
     image(img, 264, 298);
//
//      img = loadImage("letter2.png");
//      imageMode(CENTER);
//      image(img, 300, 294);
//    
//      img = loadImage("letter3.png");
//      imageMode(CENTER);
//      image(img, 300, 268);
    }

 
}

void draw()
//the code inside draw() run continuously.
//One image frame is drawn to the display window at the end of each loop
{
   background(173, 180, 216);  //light blue bkg color
   text(letter, 30, 90);  
   //draw characters to the screen, text(data, x, y)
   //only place the variable name in the data parameter
   //and not the value because the value will show on the screen
   //the variable name in the data parametter
   // will carry the char() to  the keyPressed()
   
   
}

void keyPressed()  
//The keyPressed() function is called once every time a key is pressed.
//The key that was pressed is stored in the key variable.
//Use keyPressed in a conditional operation.
// The variable "key" always contains the value of the most recent key pressed.
// If the key is an upper or lowercase letter between 'A' and 'z',
//then run the 2 statements
{

     if(key >= 'A' && key <= 'z')
      {
       letter = char(key);
       println(key);
      }


}
//    

Re: need help big time
Reply #1 - Oct 19th, 2009, 3:47pm
 
You haven't yet understood the basic structure of a sketch:

Code:
//declare global variables

// setup is usually run just once at startup
void setup() {
 // Size should always be the first call inside setup:
 size(500,500);
 //frameRate is optional
 frameRate(30);
}

// draw() loops at however many frames per second is set in frameRate
void draw() {
 // do stuff at 30 fps...
}


In your case you draw the image to the screen once in setup.  However as soon as setup has finished draw starts running, and the first thing you do in draw is background() which covers over the image.

You would normally load images in setup and call image() in draw.

You have one other notable error in your code: when loading a single image there's no reason to call loadImage in a for loop; though I guess you may be intending to adjust the filename in order to load multiple images...
Re: need help big time
Reply #2 - Oct 20th, 2009, 3:01pm
 
Quote:
Thanks for the comments, but I still cannot get each key pressed letter to correspondent to each image.

PFont font;
char letter = 'a';  
int maxImages = 26; // Total # of images
int imageIndex = 1; // Initial image to be displayed is the first
PImage img;

void setup()  
{
size(500, 500);
smooth();
font = loadFont("Candara-Bold-80.vlw");  
textFont(font);
textSize(100);  

img = loadImage("image1.gif");
img = loadImage("image2.gif");
img = loadImage("image3.gif");
img = loadImage("image4.gif");
img = loadImage("image5.gif");
img = loadImage("image6.gif");
img = loadImage("image7.gif");
img = loadImage("image8.gif");
img = loadImage("image9.gif");
img = loadImage("image10.gif");
img = loadImage("image11.gif");
img = loadImage("image12.gif");
img = loadImage("image13.gif");
img = loadImage("image14.gif");
img = loadImage("image15.gif");
img = loadImage("image16.gif");
img = loadImage("image17.gif");
img = loadImage("image18.gif");
img = loadImage("image19.gif");
img = loadImage("image20.gif");
img = loadImage("image21.gif");
img = loadImage("image22.gif");
img = loadImage("image23.gif");
img = loadImage("image24.gif");
img = loadImage("image25.gif");
img = loadImage("image26.gif");
}

void draw()
{
  background(173, 180, 216);  
  text(letter, 30, 90);  
 
  for(int i = imageIndex; i < maxImages; i +=1)
  {
    imageMode(CENTER);
    image(img, 264, 298);
   }    
}

void keyPressed()  
{
  if(key >= 'A' && key <= 'z')
    {
     letter = char(key);
     println(key);
    }
}


Don't send responses in PMs - it rather tends to limit the number of people who get to see them and means solutions don't get shared Wink

You could have loaded all those images using a loop to save some typing, but the important point is that you've loaded them all into the same variable: 'img'.  Consider the following:

Code:
int myInt;

myInt = 0;
myInt = 1;
myInt = 2;
myInt = 3;
myInt = 4;
println(myInt);


What do you get when you print 'myInt'  One variable can normally only hold one value: the last value assigned to it.  PImage variables aren't any different.  You could create lots of individual PImage variables, or better still create an array of images...  Then you'd need to set up a condition so a different image gets displayed depending on the key pressed...  In situations where there are lots of possible conditions involved switch is your friend.
Page Index Toggle Pages: 1