Guest User

Untitled

a guest
Apr 24th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. /*
  2. javaButtonExample
  3. Nathan Rowbottom
  4. Apr 15 2018
  5. An example of a button
  6.  
  7. TODO:
  8. 1 Add comments
  9. 2 Move the button detection into a boolean function
  10. 3 Make the button do something else on hover or on clicking
  11. 4 In another sketch, make your own two buttons that does something (increases/decreases the shade of the background?)
  12. */
  13.  
  14.  
  15. //Global variables
  16. PVector mouse;
  17. PVector buttonPos, buttonSize;
  18.  
  19. color activeCol = color(200,200,50);//yellow
  20. color inactiveCol = color(255);//white
  21.  
  22. void setup(){
  23. size(800, 600);
  24. strokeWeight(4);
  25. rectMode(CENTER);
  26. textAlign(CENTER, CENTER);
  27. textSize(10);
  28. init();
  29. }
  30.  
  31. //used to set up anything that needs to be set up
  32. void init(){
  33. mouse = new PVector();
  34. buttonPos = new PVector(width/2, height/2);
  35. buttonSize = new PVector(80, 30);
  36. }
  37.  
  38. //the draw function
  39. void draw(){
  40. background(50);
  41.  
  42. //update stuff
  43. mouse.set(mouseX, mouseY);
  44.  
  45. //check stuff
  46. //is the mouse over the button?
  47. if (abs(mouse.x - buttonPos.x) < buttonSize.x/2 && abs(mouse.y - buttonPos.y) < buttonSize.y/2){
  48. fill(activeCol);
  49. text("mouse over", width/2, 500);
  50. //is the mouse button pressed?
  51. if (mousePressed){
  52. text("button clicked!", width/2, 100);
  53. }
  54. }
  55. else{
  56. fill(inactiveCol);
  57. }
  58.  
  59. //draw stuff
  60. rect(buttonPos.x, buttonPos.y, buttonSize.x, buttonSize.y);
  61. }
Add Comment
Please, Sign In to add comment