I think you want the mouse to behave as a toggle, right? That is, it shows no trees and when you click it, a tree appears and stays, say, until you click again or do something else or maybe you want it to stay there until the program ends. You have misunderstood how processing works under-the-bonnet, but basically, it could be seen as something like the following C-ish pseudo-code:
main() {
setup(); // --> this is the setup() method in processing
mainLoop();
}
mainLoop() {
while (program_is_not_being_shut_down) {
LISTENERS();
if (drawFlag) {
draw(); // --> this is the draw() method in processing
}
}
}
This means setup() only gets called once, when the program starts, and draw() is called once at each iteration of the endless-loop (which continues until program is shut down) given the fact that drawFlag is true. That drawFlag is a little boolean variable that one can set to false to stop draw() from being executed, and set to true to make it be executed in the next iteration. That is controlled by methods like noLoop(), loop() and redraw() in Processing (see the reference). About your problem, that LISTENERS() function is something that gets called continuously while your sketch is running and always probing for events such as keystrokes and mouse being pressed or released, etc. It keeps running which each iteration of the loop, and that is very fast, so it will detect those events as soon as they happen. What you are doing by drawing your tree in mousePressed() is to only draw it while the mouse is being pressed, that is, when that LISTENERS() I made up is running. What you seem to want is to tell the program that a tree should be displayed, in mousePressed(), but display it in draw().
Therefore you need to make mousePressed() communicate with draw(). You can do that using a flag, just like that drawFlag up there. As I assume you would need more than one tree, you would need one flag for each of those trees, and that gets complicated to accomplish without the use of classes. You could create a tree class, which encapsulated all the methods and variables you wanted (including that flag, so each tree would have its flag). The problem with not redrawing the background is that not only your tree stays: everything else stays. You probably don't want that as that will make your sketch look like the screen you get when you win a M$ freecell game... May I suggest an idea for a more general approach that lets you choose whether you want the trees to stay without compromising the rest:
ArrayList listOfTrees;
void setup() {
size(500, 500);
listOfTrees = new ArrayList(0);
}
void draw() {
background(color(0, 0, 0)); // I made up a colour for background just to illustrate
int i;
for (i = 0; i < listOfTrees.size(); i++) {
tree temp = (tree) listOfTrees.get(i);
temp.drawTree();
}
}
void mousePressed() {
float k = round(random(100,400));
tree newTree = new tree(k,k,k);
listOfTrees.add(newTree);
}
void keyPressed() {
int i;
boolean found = false;
switch(key) {
case 'r': // removes last tree from memory
listOfTrees.remove(listOfTrees.size() - 1);
break;
case 'h': // hides last visible tree
for (i = listOfTrees.size()-1; i >= 0 && !found; i--) { // search list backwards testing for hidden trees
tree temp = (tree) listOfTrees.get(i);
if (temp.amIvisible) {
tree temp2 = new tree(temp.x, temp.y, temp.x2); // copies the found tree
temp2.amIvisible = false; // SETS IT TO HIDDEN
listOfTrees.set(i, temp2 ); // replace the visible tree with the same tree only hidden
found = true; // last visible tree found, stop the search
}
}
break;
case 's': // shows last hidden tree
for (i = listOfTrees.size()-1; i >= 0 && !found; i--) { // search list backwards testing for hidden trees
tree temp = (tree) listOfTrees.get(i);
if (!temp.amIvisible) {
tree temp2 = new tree(temp.x, temp.y, temp.x2); // copies the found tree
temp2.amIvisible = true; // SETS IT TO VISIBLE
listOfTrees.set(i, temp2 ); // replace the hidden tree with the same tree only visible
found = true; // last visible tree found, stop the search
}
}
break;
}
}
class tree {
boolean amIvisible;
float x, y, x2;
tree(float _x, float _y, float _x2) { // constructor that creates the tree
x = _x;
y = _y;
x2 = _x2;
amIvisible = true; // by default, show the tree
}
void drawTree() { // displays the tree conditionally
if (amIvisible) { // check if tree should be displayed
strokeWeight(20);
stroke(39,7,7);
line(x,y, x2,height);
noStroke();
fill(0,255,0);
ellipse(x, y, 200,200);
}
}
}
the arraylist is like a vector, but with memory allocated as the program runs, and with this approach, trees stay in memory until you remove them with the 'r' key. There is extensive reference on arraylists in the internet and in processing's reference. Notice the difference between what happens if you press 'r' and then 's' or 'h' and then 's'. Of course, nothing happens if no trees are hidden and you press 's'. The keyboard stuff is to demonstrate how you can control the trees just by setting some flags / variables. As trees are kept in memory, information about what has been already drawn is not lost, so you can still manipulate it or do things with it, such as, say, to only place trees randomly in the space where there are still no trees. I have just tested it in processing 1.5 and it compiles and runs quite all-right.
-----------------------------------------------------------
Johnny-boy Floyd sends his regards