Skip to content

Instantly share code, notes, and snippets.

@sebastienblanc
Created November 9, 2020 08:34
Show Gist options
  • Save sebastienblanc/e5009b45ca2ba2a8f12cddd2058cf07f to your computer and use it in GitHub Desktop.
Save sebastienblanc/e5009b45ca2ba2a8f12cddd2058cf07f to your computer and use it in GitHub Desktop.
// code credit: https://www.youtube.com/watch?v=mjfgGJHAuvI
// References:
// - https://docs.oracle.com/javase/8/javafx/api/
//DEPS org.openjfx:javafx-controls:11.0.2:${os.detected.jfxname}
//DEPS org.openjfx:javafx-graphics:11.0.2:${os.detected.jfxname}
//DEPS io.fabric8:kubernetes-client:4.12.0
package jbang.snake;
import java.util.Random;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.paint.*;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
import javafx.application.Platform;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.LogWatch;
import static javafx.scene.input.KeyCode.*;
public class Shapes extends Application {
Group root = new Group();
int x = 150;
@Override
public void start(Stage stage) throws Exception {
Config config = new ConfigBuilder().withMasterUrl("https://192.168.64.2:6443").build();
KubernetesClient client = new DefaultKubernetesClient(config);
client.pods().inNamespace("default").watch(new Watcher<Pod>() {
Runnable updater = new Runnable() {
@Override
public void run() {
addSPhere();
}
};
@Override
public void eventReceived(Action action, Pod pod) {
//logger.log(Level.INFO, action.name() + " " + pod.getMetadata().getName());
switch (action.name()) {
case "ADDED":
System.out.println("pod added");
// Create a Sphere
Platform.runLater(updater);
// logger.log(Level.INFO, pod.getMetadata().getName() + "got added");
break;
case "DELETED":
// logger.log(Level.INFO, pod.getMetadata().getName() + "got deleted");
break;
case "MODIFIED":
//logger.log(Level.INFO, pod.getMetadata().getName() + "got modified");
break;
default:
//logger.log(Level.SEVERE, "Unrecognized event: " + action.name());
}
}
@Override
public void onClose(KubernetesClientException e) {
System.out.println("something goes wrong");
// logger.log(Level.INFO, "Closed");
// isWatchClosed.countDown();
}
});
// Create a Light
PointLight light = new PointLight();
light.setTranslateX(350);
light.setTranslateY(100);
light.setTranslateZ(150);
// Create a Camera to view the 3D Shapes
PerspectiveCamera camera = new PerspectiveCamera(false);
camera.setTranslateX(100);
camera.setTranslateY(-50);
camera.setTranslateZ(300);
// Create a Scene with depth buffer enabled
Scene scene = new Scene(root, 400, 200, true);
// Add the Camera to the Scene
scene.setCamera(camera);
// Add the Scene to the Stage
stage.setScene(scene);
scene.setOnKeyPressed(e -> {
switch(e.getCode()) {
case W:
addSPhere();
}
});
// Set the Title of the Stage
stage.setTitle("Pods as Spheres");
// Display the Stage
stage.show();
}
private void addSPhere(){
PhongMaterial mat_blue = new PhongMaterial();
mat_blue.setDiffuseColor(Color.BLUE);
mat_blue.setSpecularColor(Color.LIGHTBLUE);
mat_blue.setSpecularPower(10.0);
Sphere sphere = new Sphere(50);
sphere.setTranslateX(x);
sphere.setTranslateY(-5);
sphere.setTranslateZ(400);
sphere.setMaterial(mat_blue);
root.getChildren().add(sphere);
x = x + 110;
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment