1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
package sample; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.image.PixelWriter; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.stage.Stage; public class _globe extends Application { public static void main(String... args) { Application.launch(args); } private static final int VIEW_W = 400, VIEW_H = 300; private static final int MAP_L = VIEW_H / 2, PITCH = VIEW_H / 10; @Override public void start(Stage stage) throws Exception { BorderPane pane = new BorderPane(); Scene scene = new Scene(pane); stage.setScene(scene); Canvas view = new Canvas(VIEW_W, VIEW_H); pane.setCenter(view); PixelWriter pw = view.getGraphicsContext2D().getPixelWriter(); int[][] map = new int[MAP_L][MAP_L]; for (int y = 0; y < MAP_L; y++) { double h = 1 - ((y + 0.5) / MAP_L); double r = Math.sqrt(1 - h * h); for (int x = 0; x < MAP_L; x++) { double t = 1 - ((x + 0.5) / MAP_L); map[y][x] = (t > r) ? -1 : (int)((1 - Math.asin(t / r) / (Math.PI / 2)) * MAP_L); } } for (int y = 0; y < 2 * MAP_L; y++) { int map_y = (y < MAP_L) ? y : 2 * MAP_L - y - 1; for (int x = 0; x < 2 * MAP_L; x++) { int map_x = (x < MAP_L) ? x : 2 * MAP_L - x - 1; int img_x = map[map_y][map_x]; if (img_x == -1) continue; if (x >= MAP_L) img_x = 2 * MAP_L - img_x; pw.setColor(x + 50, y, (img_x / PITCH % 2 != y / PITCH % 2) ? Color.BLACK : Color.TRANSPARENT); } } stage.show(); } }