Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Rick van Lieshout (en I1E-n) Donderdag 04 September.
- // Moduleopdracht 4
- //We hebben de volgende variabelen nodig:
- // marge | Type int | De marge tussen de lampjes die altijd aangehouden moet worden
- // circlewidth | Type int | De diameter van een ledje
- // windowWidth | Type int | De grootte van het window.
- // windowHeight| Type int | De hoogte van het window.
- // status | Type boolean| Deze houd bij of de lampen aan of uit zijn.
- // buttonHeight| Type int | Deze houd de hoogte van de button bij
- // time | Type int | Tijd in miliseconde
- // 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)
- // onoff | Type boolean| Deze variabele houdt bij of de led aan of uit staat
- // buttony | type int | Het y coordinaat van de button
- // buttonx | type int | Het x coordinaat van de button
- // buttonwidth | type int | de width van de button
- //we hebben de volgende methodes nodig (naast de stnadaard draw en setup)
- // drawButton() | type void | Deze tekent de knop met de tekst op het scherm
- // mouseClicked() | type void | Deze verwerkt de mouseclicks
- // createLED() | Type void | Deze methode tekent een lamp op het scherm
- //Op welke events moet het programma reageren.
- // Een mouseclick.
- int marge = 10;
- int circlewidth = 0;
- boolean status = false;
- boolean onoff = false;
- int windowWidth = 400;
- int windowHeight = 300;
- int buttonX = 0 +marge;
- int buttonY = windowHeight - 100 - marge;
- int buttonWidth = windowWidth - (marge * 2);
- int time = 0;
- int buttonHeight = 100;
- void setup() {
- size(windowWidth, windowHeight);
- ellipseMode(CORNER);
- }
- void draw() {
- int ledBottom = 120; // led bottom line
- background(0);
- drawButton(#FFF28E, buttonX, buttonY, buttonWidth, buttonHeight);
- createLED(#760707, #FF3636, 50, ledBottom);
- createLED(#1E9332, #05FF30, 175, ledBottom);
- createLED(#005B8B, #00A8FF, 300, ledBottom);
- }
- void createLED(int offcolour, int oncolour, int x, int y) {
- int wait = 500; // time between blinking
- int rectSize = 50;
- int ellipseSize = 90;
- fill(100);
- rect(x, y, rectSize, rectSize);
- if (status) {
- if (onoff) { //if light is on
- fill(oncolour);
- } else {
- fill(offcolour);
- }
- if (millis() - time >= wait) {
- onoff = !onoff;
- time = millis();
- }
- } else {
- fill(offcolour);
- }
- ellipse(x-20, y-70, ellipseSize, ellipseSize);
- }
- void drawButton(color c, int x, int y, int buttonwidth, int buttonheight) {
- String str = "";
- fill(c);
- rect(x, y, buttonwidth, buttonheight);
- textAlign(CENTER);
- if (status) {
- //lampjes staan aan, dus print zet uit.
- str = "Zet uit";
- } else {
- str = "Zet aan";
- }
- fill(0);
- textSize(28);
- text(str, width/2, height -(buttonHeight - (buttonHeight /2)));
- }
- void mouseClicked() {//check to see wether a click happened inside the button
- if (mouseY > buttonY && mouseY < buttonY + buttonHeight) {
- if (mouseX > buttonX && mouseX < buttonX+buttonWidth) {
- status = !status;
- time = 0; //reset timer so the lights will turn on immediately
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement