Advertisement
Mastermindzh

Moduleopgave4

Sep 17th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. // Rick van Lieshout (en I1E-n) Donderdag 04 September.
  2. // Moduleopdracht 4
  3.  
  4. //We hebben de volgende variabelen nodig:
  5. // marge       | Type int    | De marge tussen de lampjes die altijd aangehouden moet worden
  6. // circlewidth | Type int    | De diameter van een ledje
  7. // windowWidth | Type int    | De grootte van het window.
  8. // windowHeight| Type int    | De hoogte van het window.
  9. // status      | Type boolean| Deze houd bij of de lampen aan of uit zijn.
  10. // buttonHeight| Type int    | Deze houd de hoogte van de button bij
  11. // time        | Type int    | Tijd in miliseconde
  12. // wait        | Type int    | Wachtijd tussen knipper van de lamp (als je dit niet doet en werkt met een delay kan je de knop niet indrukken zonder multithreading)
  13. // onoff       | Type boolean| Deze variabele houdt bij of de led aan of uit staat
  14. // buttony     | type int    | Het y coordinaat van de button
  15. // buttonx     | type int    | Het x coordinaat van de button
  16. // buttonwidth | type int    | de width van de button
  17.  
  18. //we hebben de volgende methodes nodig (naast de stnadaard draw en setup)
  19. // drawButton() | type void | Deze tekent de knop met de tekst op het scherm
  20. // mouseClicked() | type void | Deze verwerkt de mouseclicks
  21. // createLED()   | Type void | Deze methode tekent een lamp op het scherm
  22.  
  23. //Op welke events moet het programma reageren.
  24. // Een mouseclick.
  25.  
  26. int marge = 10;
  27. int circlewidth = 0;
  28. boolean status = false;
  29. boolean onoff = false;
  30. int windowWidth = 400;
  31. int windowHeight = 300;
  32.  
  33. int buttonX = 0 +marge;
  34. int buttonY = windowHeight - 100 - marge;
  35. int buttonWidth = windowWidth - (marge * 2);
  36. int time = 0;
  37. int buttonHeight = 100;
  38.  
  39.  
  40. void setup() {
  41.   size(windowWidth, windowHeight);
  42.   ellipseMode(CORNER);
  43. }
  44.  
  45. void draw() {
  46.   int ledBottom = 120; // led bottom line
  47.   background(0);
  48.   drawButton(#FFF28E, buttonX, buttonY, buttonWidth, buttonHeight);
  49.   createLED(#760707, #FF3636, 50, ledBottom);
  50.   createLED(#1E9332, #05FF30, 175, ledBottom);
  51.   createLED(#005B8B, #00A8FF, 300, ledBottom);
  52. }
  53.  
  54. void createLED(int offcolour, int oncolour, int x, int y) {
  55.   int wait = 500; // time between blinking
  56.   int rectSize = 50;
  57.   int ellipseSize = 90;
  58.   fill(100);
  59.   rect(x, y, rectSize, rectSize);
  60.   if (status) {
  61.     if (onoff) { //if light is on
  62.       fill(oncolour);
  63.     } else {
  64.       fill(offcolour);
  65.     }
  66.     if (millis() - time >= wait) {
  67.       onoff = !onoff;
  68.       time = millis();
  69.     }
  70.   } else {
  71.     fill(offcolour);
  72.   }
  73.   ellipse(x-20, y-70, ellipseSize, ellipseSize);
  74. }
  75.  
  76. void drawButton(color c, int x, int y, int buttonwidth, int buttonheight) {
  77.   String str = "";
  78.   fill(c);
  79.   rect(x, y, buttonwidth, buttonheight);
  80.   textAlign(CENTER);
  81.   if (status) {
  82.     //lampjes staan aan, dus print zet uit.
  83.     str = "Zet uit";
  84.   } else {
  85.     str = "Zet aan";
  86.   }
  87.   fill(0);
  88.   textSize(28);
  89.   text(str, width/2, height -(buttonHeight - (buttonHeight /2)));
  90. }
  91.  
  92. void mouseClicked() {//check to see wether a click happened inside the button
  93.   if (mouseY > buttonY && mouseY < buttonY + buttonHeight) {
  94.     if (mouseX > buttonX && mouseX < buttonX+buttonWidth) {
  95.       status = !status;
  96.       time = 0; //reset timer so the lights will turn on immediately
  97.     }
  98.   }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement