Last active
March 12, 2018 10:49
-
-
Save fritzvd/29c2183db70757c5536fb0a1e1173786 to your computer and use it in GitHub Desktop.
Radioknop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDoelwit { | |
public void schakel(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import processing.core.PApplet; | |
public abstract class Knop { | |
protected PApplet app; | |
protected float x, y, breedte, hoogte; | |
protected ArrayList<IDoelwit> doelwitten = new ArrayList<>(); | |
public Knop(PApplet app, float x, float y, float breedte, float hoogte) { | |
this.app = app; | |
this.x = x; | |
this.y = y; | |
this.breedte = breedte; | |
this.hoogte = hoogte; | |
} | |
public boolean isMuisOverKnop() { | |
if (app.mouseX >= x && app.mouseX < x + breedte && | |
app.mouseY >= y && app.mouseY < y + hoogte) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
public abstract void tekenKnop(); | |
public abstract void doeKnopActie(); | |
public void handelInteractieAf() { | |
doeKnopActie(); | |
} | |
public void voegDoelwitToe(IDoelwit doelwit) { | |
doelwitten.add(doelwit); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import processing.core.PApplet; | |
@SuppressWarnings("serial") | |
public class KnoppenApp extends PApplet { | |
public final int SIZE = 50; | |
public final int X = 60; | |
public final int Y = 60; | |
public static void main(String[] args) { | |
PApplet.main(new String[] {"KnoppenApp"}); | |
} | |
private ArrayList<Knop> knoppen = new ArrayList<>(); | |
public void setup() { | |
background(0); | |
// set 1 | |
RadioKnop radio = new RadioKnop(this, X, Y, SIZE, SIZE); | |
knoppen.add(radio); | |
RadioKnop radio2 = new RadioKnop(this, X + X, Y, SIZE, SIZE); | |
knoppen.add(radio2); | |
RadioKnop radio3 = new RadioKnop(this, X + X * 2, Y, SIZE, SIZE); | |
knoppen.add(radio3); | |
radio.voegDoelwitToe(radio2); | |
radio.voegDoelwitToe(radio3); | |
radio2.voegDoelwitToe(radio); | |
radio2.voegDoelwitToe(radio3); | |
radio3.voegDoelwitToe(radio); | |
radio3.voegDoelwitToe(radio2); | |
} | |
public void settings() { | |
size(400, 400); | |
} | |
public void draw() { | |
for (Knop k : knoppen) { | |
k.tekenKnop(); | |
} | |
} | |
public void mousePressed() { | |
for (Knop k : knoppen) { | |
if (k.isMuisOverKnop()) { | |
k.handelInteractieAf(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.core.PApplet; | |
public class RadioKnop extends Switch implements IDoelwit { | |
public RadioKnop(PApplet app, float x, float y, float breedte, float hoogte) { | |
super(app, x, y, breedte, hoogte); | |
} | |
@Override | |
public void schakel() { | |
for (IDoelwit dw: doelwitten) { | |
RadioKnop rk = (RadioKnop) dw; | |
rk.isAan = false; | |
} | |
this.isAan = true; | |
} | |
@Override | |
public void handelInteractieAf() { | |
schakel(); | |
} | |
@Override | |
public void tekenKnop() { | |
app.ellipseMode(PApplet.CENTER); | |
app.noStroke(); | |
app.fill(255); | |
app.ellipse(x, y, breedte, hoogte); | |
if (isAan) { | |
app.fill(0); | |
app.ellipse(x, y, breedte - breedte/4, hoogte - hoogte/4); | |
} | |
} | |
@Override | |
public boolean isMuisOverKnop() { | |
if (PApplet.dist(app.mouseX, app.mouseY, x, y) < breedte/2) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.core.PApplet; | |
public class Switch extends Knop { | |
protected boolean isAan; | |
public Switch(PApplet app, float x, float y, float breedte, float hoogte) { | |
super(app, x, y, breedte, hoogte); | |
isAan = false; | |
} | |
@Override | |
public void handelInteractieAf() { | |
isAan = !isAan; | |
doeKnopActie(); | |
} | |
public void zetAan() { | |
isAan = true; | |
} | |
public void zetUit() { | |
isAan = false; | |
} | |
//@Override | |
public void tekenKnop() { | |
tekenBasis(); | |
tekenSchuif(); | |
} | |
private void tekenBasis() { | |
int rand = 0; | |
app.noStroke(); | |
app.fill(0); | |
app.rect(x + rand, y + rand, breedte - 2 * rand, hoogte - 2 * rand); | |
rand += 1; | |
app.fill(95); | |
app.rect(x + rand, y + rand, breedte - 2 * rand, hoogte - 2 * rand); | |
rand += 2; | |
app.fill(0); | |
app.rect(x + rand, y + rand, breedte - 2 * rand, hoogte - 2 * rand); | |
rand += 2; | |
if (isAan) { | |
app.fill(0xFFA4C739); | |
} | |
else { | |
app.fill(0); | |
} | |
app.rect(x + rand, y + rand, breedte - 2 * rand, hoogte - 2 * rand); | |
} | |
private void tekenSchuif() { | |
app.noStroke(); | |
float schuifBreedte = breedte / 2; | |
float greepBreedte = schuifBreedte - 2; | |
if (isAan) { | |
app.fill(0); | |
app.rect(x + breedte - schuifBreedte, y, schuifBreedte, hoogte); | |
app.fill(255); | |
app.rect(x + breedte - greepBreedte, y, greepBreedte, hoogte); | |
} | |
else { | |
app.fill(0); | |
app.rect(x, y, schuifBreedte, hoogte); | |
app.fill(255); | |
app.rect(x, y, greepBreedte, hoogte); | |
} | |
} | |
@Override | |
public void doeKnopActie() { | |
if (isAan) { | |
zetUit(); | |
} else { | |
zetAan(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment