Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- javaButtonExample
- Nathan Rowbottom
- Apr 15 2018
- An example of a button
- TODO:
- 1 Add comments
- 2 Move the button detection into a boolean function
- 3 Make the button do something else on hover or on clicking
- 4 In another sketch, make your own two buttons that does something (increases/decreases the shade of the background?)
- */
- //Global variables
- PVector mouse;
- PVector buttonPos, buttonSize;
- color activeCol = color(200,200,50);//yellow
- color inactiveCol = color(255);//white
- void setup(){
- size(800, 600);
- strokeWeight(4);
- rectMode(CENTER);
- textAlign(CENTER, CENTER);
- textSize(10);
- init();
- }
- //used to set up anything that needs to be set up
- void init(){
- mouse = new PVector();
- buttonPos = new PVector(width/2, height/2);
- buttonSize = new PVector(80, 30);
- }
- //the draw function
- void draw(){
- background(50);
- //update stuff
- mouse.set(mouseX, mouseY);
- //check stuff
- //is the mouse over the button?
- if (abs(mouse.x - buttonPos.x) < buttonSize.x/2 && abs(mouse.y - buttonPos.y) < buttonSize.y/2){
- fill(activeCol);
- text("mouse over", width/2, 500);
- //is the mouse button pressed?
- if (mousePressed){
- text("button clicked!", width/2, 100);
- }
- }
- else{
- fill(inactiveCol);
- }
- //draw stuff
- rect(buttonPos.x, buttonPos.y, buttonSize.x, buttonSize.y);
- }
Add Comment
Please, Sign In to add comment