Created
August 26, 2012 12:34
-
-
Save joa/3478572 to your computer and use it in GitHub Desktop.
HW example
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
import hiddenwood.concurrent.*; | |
import hiddenwood.display.*; | |
import hiddenwood.gl.*; | |
import hiddenwood.http.*; | |
class App { | |
private static final String URL = | |
"http://www.random.org/integers/?num=1&min=1&max=100000&col=1&base=10&format=plain&rnd=new"; | |
public static void main(String[] args) { | |
Display.newBuilder(). | |
width(1024).height(768). | |
onSuccess(App::onDisplayBuilt). | |
build(); | |
} | |
public static void onDisplayBuilt(Display display) { | |
// download a random number from the interwebs | |
// parse the data into an integer | |
// test wether or not this is a prime number (in the "background", so ui is not blocked) | |
// print the result | |
Future<HttpResponse> response = | |
Http. | |
get(URL). | |
map(response -> response.data.toString()). | |
map(Integer::parseInt). | |
map(App::isPrime, Dispatchers.BACKGROUND). | |
onSuccess(b -> System.out::println); | |
// create a display list | |
DisplayList displayList = DisplayList.newBuilder(). | |
backgroundColor(0x000000). | |
display(display). | |
build(); | |
// create a solid plane 512x512px (not that you would have to do it this way) | |
Canvas canvas = new Canvas(512.0f, 512.0f, gl -> { gl.clearColor(1.0f, 0.0f, 1.0f, 1.0f); }); | |
// center the canvas pivot and move to center of display | |
canvas. | |
centerPivot(). | |
moveTo(512.0f, 384.0f); | |
// add to root of display list | |
displayList.root().addChild(canvas); | |
// rotate the canvas each frame | |
display.onEnterFrame.attach(d -> { canvas.rotateBy(0.001f); }); | |
} | |
public static boolean isPrime(final int value) { | |
for(int i = 2; i < value; ++i) { | |
if(value % i == 0) return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment