Wednesday, January 30, 2013

Learning about Buttons


Today, I learned how to place buttons on my simulations and how to track the position of my mouse.

My project today

Fourth exercise


For this exercise we will work with adding buttons to the simulation. And this will allow us to control the simulation better.
1) Go over the two examples and try to learn how they work. For reference, see the Processing documentation about buttons.
http://processing.org/reference/
http://processing.org/learning/topics/button.html

2) Make a copy of button_exercise_2 and rename it. Modify this new version so that it prints to the window (using the Text stuff you learned last week) the x and y position of the cursor. 


3) Modify the code so that when the button is pressed it displays some new text. Pick whatever text you like!  :)


4) Place some text next to the button that explains what it does.


Dr. Bellis told me to try to figure out what each codes mean by myself, and understand it more as I create one of my own.

It was really confusing at first but as I tried changing the numbers for each code I got the gist of what the codes represent on the simulation. 
For the second question where it asks me to make the window display the x and y positions of the cursor. I used the string method that I did for last internship and put in all the correct codes. The problem is, the texts did not show up on my simulation. I asked Dr. Bellis for help and we found that texts I want to display on the simulation window have to be written under void draw (), whereas before I just wrote the codes on the very bottom of the series of codes.

The outcome is:
Codes for cursor tracking:
PFont f;
 f= createFont("Times New Romans", 16, true);
 textFont (f, 24);
 fill (83,130,229);
 String MouseX_text= "MouseX:"+ nf(mouseX,1,2);          //for X-coordinates
 text (MouseX_text, 10, 400); fill (83, 130, 229);
 String MouseY_text= "MouseY:"+ nf(mouseY,1,2);          //for Y-coordinates
 text (MouseY_text, 10, 420);
The screenshot eliminated my cursor automatically so you can see it, but the mouseX and mouseY shows the coordinates where the cursor is located. 


When the button (the box) is clicked by the cursor, it turns purple. (yea, I had fun with choosing the colors XD)
After this I found that the box button is also draggable... which I think was an interesting unintentional outcome...
As for the second project I added texts to the button so that when one clicks it, it shows "hello" and click it again it shows "bye". I had a little trouble with that too, the same thing with placement, but is was easily solved.

Codes for stimulating a button with words showing:


int windowHeight = 640;              //This sets the size of the window. 
int windowWidth = 700;

float bx;                                        // button x-position
float by;                                       // button y-position
int boxSize = 75;                         //set the size for the box (square with 75 on each side)

boolean overBox = false;            // This tracks whether or not the mouse is over the box.
boolean locked = false;               // This tracks whether or not the box is ``locked". (I was not sure what        "locked means, I am going to find it out on our next meeting)
float xOffset = 0.0;
float yOffset = 0.0;

int background_color = 0;
int display_text = 0;

void setup()
{
    size(640, 360);
    size(windowWidth, windowHeight);  // Size must be the first statement
    bx = 100;
    by = 100;
    //rectMode(RADIUS); //
}

////////////////////////////////////////////////////////////////////////////////
// This is always the part where we deal with all the drawing.
////////////////////////////////////////////////////////////////////////////////
void draw()
{
    background(background_color);

    // Test if the cursor is over the box
    if (mouseX > bx-boxSize && mouseX < bx+boxSize && mouseY > by-boxSize && mouseY < by+boxSize) {               //A command to tell the computer that if the cursor is within the box range, and is clicked the "if()" commands will show. 
        overBox = true;
        if(!locked) {
            stroke(255);        // If we are over the box, change the colors.
            fill(153);
        }
    } else {
        stroke(153);
        fill(153);
        overBox = false;
    }

    // Draw the box
    rect(bx, by, boxSize, boxSize);
    PFont f;
    if (display_text==1)
    {
     f= createFont ("Times New Romans", 16, true);                   //Here is where I added the texts and set the text to display_text= 1. 
      textFont (f, 24);
      fill (224,97,97);
      text ("HELLO~", 100, 240);                                                //Hello will show up when the button is pressed. 
    }
     else if (display_text==2)
     {
            f= createFont ("Times New Romans", 16, true);
            textFont (f, 24);
            fill (56,183,214);
            text ("BYE", 100, 240);                                                  //Same idea here. 
     }
}

////////////////////////////////////////////////////////////////////////////////
// Here is where we tell the program how to deal with:
//   1) When the mouse button is pressed.
////////////////////////////////////////////////////////////////////////////////

// This code tells us what to do when the box is pressed.
void mousePressed() {

    if(overBox) {
        locked = true;
        fill(19, 230, 128);
        if ( background_color == 0 )
        {
                   background_color = 100;
         display_text = 1;                                           //display_text= 1 is the command for calling up the "hello" when the button is pressed. 
      }
        else if ( background_color == 100 )             //When it is clicked again, if background color is 100 (gray) it will change to 0 (black) as written below+ call up display_text= 2 which is the command for "bye". 
     
        {
            background_color = 0;
            display_text = 2;
         
          }
    } else {
        locked = false;
   
    }
    //xOffset = mouseX-bx;
    //yOffset = mouseY-by;

}

Clicked once
Click back. 
For the next meeting, Dr. Bellis and I are going to start discussing our Dark Matter simulations and he is also working with the computer science department to see if we can upload the simulations that I have done on the internet for people to play with. :)



Saturday, January 19, 2013

1/16/2012


Today's shuttle to Siena College was canceled due to the large amount of snow. However, I met with my mentor through GChat and e-mail. It was really hard, because I have more than five windows open on my laptop and questions that took a long time to answer.

The homework I did for this meeting was to change units of my simulation to pixels to make it look more realistic. Before we had to change gravity to a small value to make it fit in the screen and make the ball drop slower, but with the conversion gravity can be 9.8 m/s^2 and would still look the same. To do this I had to add two more variables to my simulation:
float pixels2meters=___;
float meters2pixels=___;
In my case my one pixel is equal to 10 meters, and one meter equals to .1 pixel. This did not work so well after all. I sent my simulation to Mr. Bellis so that he could check it for me. We ended up putting more variables in:
float meters2pixels = 10;
float pixels2meters = 1/meters2pixels;
int frameRateSpeed = 30; // frames per second

size(windowWidth, windowHeight);
   noStroke();     
   frameRate(frameRateSpeed);   //set frameRat to the same as frameRate Speed

t = t+(1.0/frameRateSpeed); // Also changed the time so that time increases when one frame goes through (1/30 sec= one frame)

Another homework was to find how to add text to my simulation. It was really fun, because I kept changing the text color to the colors I want. The things I typed to make text appear was:

PFont f;
    f = createFont("Microsoft Sans Serif", 16, true);   //first one was font type (I looked at the font types in my files for word and feel like this is the best one, second one is font size, and third one means anti-aliasing on)
    textFont (f, 24);
    fill (255);
    text("Yvonne Yen", 10, 240);        //text, size, and location (y-coordinate)
    text("Free Fall", 10, 270); 
During the meeting today, I learned how to add timer on my simulation.

String time_text = "time: "+nf(t,1,2);
   text (time_text, 10, 580);
I took this as an example and created a height measurer on my simulation too! :)

String Height_text= "Height:"+ nf(-y+y0,1,2);    //it is -y+y because I want to know how far the ball traveled as it drops so I set the "y" as the negative value (dropping) and add the initial position to exclude it from the measurement. 
   text (Height_text, 10, 610);

I also changed the color of the texts for height and time too~:)

HERE IS THE END PRODUCT:

Looking at this we can know how much the height has dropped in how many sections. I will check if this simulation is accurate later...

Next week, I will have to communicate with my mentor through e-mail again, because it is Emma's MLK service day.

:)







Wednesday, January 9, 2013

Mentor Out of Town


School commenced today, however, Dr. Bellis was out of town so we will be having our meeting next week 1/16.

For the missing week, Dr. Bellis has assigned me to new assignments:
"I've added a ``Third Exercise" to our shared Google Folder. I've given you a small assignment as a Part 1 on the document. I've also assigned some reading in Part 2, as well as an additional challenge *only* if you have the time. This second part is a bit harder, so feel free to wait until we get together to tackle it. I think you can do Part 1 next Wed, during your research time, even if you are at Emma Willard and I am not around. "

I'll be doing them later this week. :)

During break I learned another new concept called Mirror Stage by Jacques Lacan (a French psychoanalyst) through a novel that I was reading (I was sick for the first week of break, so other than doing college apps I had a lot of free time). The concept became really fascinating as I did some research to understand it more. It states that Mirror stage is a stage in infancy (around one and a half years old) when babies become able to recognized the image in the mirror as themselves, which sets up their 'ego' and their understanding of 'I'. Before then, they think that the their image in the mirror is an ordinary object. The image in the mirror helps babies to acknowledge their existence, however, according to Lacan, their understanding of self is often twisted by the interaction with other people. The image they see in the mirror is shaped in the way they think other people view themselves, or it can also be the idealized version of what we want to look like. At times when we are confident and successful, the image of ourself in the mirror is often prettier/more handsome than before. And at times we are down, we see a worse looking self in the mirror. :)
A picture of Santa Barbara Beach (LA) that I took while we were drive.