Skip to content

Instantly share code, notes, and snippets.

@kishida kishida/LoomGame
Last active Aug 1, 2018

Embed
What would you like to do?
Loom sample with continuation for a game
package kis.loom;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* need Project Loom
* http://mail.openjdk.java.net/pipermail/loom-dev/2018-July/000061.html
*/
public class LoomGame {
public static void main(String[] args) throws InterruptedException {
var image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
var label = new JLabel(new ImageIcon(image));
var f = new JFrame("Loom");
f.add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500, 400);
f.setVisible(true);
var s1 = new ContinuationScope() {
int x = 30;
int y = 50;
void run() {
for(;;) {
for (x = 30; x < 370; x += 3) {
Continuation.yield(this);
}
for (x = 370; x > 30; x -= 3) {
Continuation.yield(this);
}
}
}
};
var s2 = new ContinuationScope() {
int x = 200;
int y = 30;
void run() {
for(;;) {
for (int i = 0; i < 360; ++i) {
Continuation.yield(this);
x = (int)(Math.cos(Math.PI * 2 * i / 360) * 120) + 200;
y = 150 - (int)(Math.sin(Math.PI * 2 * i / 360) * 120);
}
}
}
};
var c1 = new Continuation(s1, s1::run);
var c2 = new Continuation(s2, s2::run);
var g = image.createGraphics();
for (;;) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 400, 300);
c1.run();
c2.run();
g.setColor(Color.BLUE);
g.fillRect(s1.x, s1.y, 30, 30);
g.setColor(Color.RED);
g.fillOval(s2.x, s2.y, 30, 30);
label.repaint();
Thread.sleep(50);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.