Last active
March 27, 2019 05:48
-
-
Save sampottinger/45f63c1360a6622e9cb59bea978d18eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Demonstration of Java 8 language features within Processing IDE. | |
* | |
* Sketch to demonstrate Java 8 language features within the Processing development environment | |
* including type inference and lambdas. | |
*/ | |
import java.util.*; | |
List<ColoredCircle> circles = new ArrayList<>(); // Type inference | |
/** | |
* Add a new circle to the global circles list. | |
* | |
* @param x The x coordinate for the new circle. | |
* @param y The y coordinate for the new circle. | |
* @param colorGen The strategy to use to generate colors. | |
*/ | |
void addCircle(int x, int y, ColorGen colorGen) { | |
circles.add(new ColoredCircle(x, y, colorGen)); | |
} | |
/** | |
* Processing-standard settings call to size sketch. | |
*/ | |
void settings() { | |
size(200,200,FX2D); | |
smooth(); | |
} | |
/** | |
* Initalize this sketch along with its colored circles. | |
*/ | |
void setup() { | |
final color from = color(255, 255, 255); | |
final color to = color(0, 102, 153); | |
// Lambdas | |
addCircle(75, 75, (sec) -> { return lerpColor(from, to, abs(sin(sec))); }); | |
addCircle(75, 125, (sec) -> { return lerpColor(from, to, abs(cos(sec))); }); | |
addCircle(125, 125, (sec) -> { return lerpColor(from, to, abs(cos(sec / 2))); }); | |
addCircle(125, 75, (sec) -> { return lerpColor(from, to, abs(sin(sec / 2))); }); | |
} | |
/** | |
* Draw the circles within this sketch. | |
*/ | |
void draw() { | |
background(255); | |
// New iteration | |
circles.forEach((x) -> {x.drawCircle();}); | |
} |
This file contains hidden or 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
Copyright 2019 Data Driven Empathy LLC | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | |
associated documentation files (the "Software"), to deal in the Software without restriction, | |
including without limitation the rights to use, copy, modify, merge, publish, distribute, | |
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or | |
substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | |
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains hidden or 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
/** | |
* Structures related to demonstration of Java 8 language features within Processing IDE. | |
*/ | |
/** | |
* Interface for strategy which generates a color dynamically. | |
*/ | |
interface ColorGen { | |
/** | |
* Get a dynamically generated colors. | |
* | |
* @param seconds The number of seconds for which the sketch has been running. | |
* @return Newly generated color. | |
*/ | |
int getColor(float seconds); | |
} | |
/** | |
* Class representing a colored circle with a dynamically generated color. | |
*/ | |
class ColoredCircle { | |
private final ColorGen colorGen; | |
private final int x; | |
private final int y; | |
/** | |
* Create a new colored circle. | |
* | |
* @param newX The x position of this colored circle. | |
* @param newY The y position of this colored circle. | |
* @param newGen The color generator to use to color this circle. | |
*/ | |
public ColoredCircle(int newX, int newY, ColorGen newGen) { | |
x = newX; | |
y = newY; | |
colorGen = newGen; | |
} | |
/** | |
* Draw this circle. | |
*/ | |
public void drawCircle() { | |
pushStyle(); | |
noStroke(); | |
fill(colorGen.getColor(millis() / 1000.0)); | |
ellipseMode(CENTER); | |
ellipse(x, y, 30, 30); | |
popStyle(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment