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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
package sample; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.BorderPane; import javafx.scene.shape.ArcType; import javafx.stage.Stage; public class _tori extends Application { public static void main(String... args) { Application.launch(args); } private static final int VIEW_W = 400, VIEW_H = 300; private GraphicsContext gc; @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); gc = view.getGraphicsContext2D(); draw(); stage.show(); } private static final double HEAD = 80; private void draw() { gc.setTransform(1, 0, 0, -1, 50, 100); gc.setLineWidth(3); double x1 = HEAD - HEAD / Math.sqrt(2), y1 = HEAD / Math.sqrt(2), x2 = 2 * HEAD, y2 = 0, x3 = 4 * HEAD, y3 = -1.5 * HEAD, x4 = 2 * HEAD, y4 = -2 * HEAD, x5 = HEAD / 2, y5 = 0, x6 = x5 - 2, y6 = HEAD / 4, x7 = 0, y7 = -HEAD / 2; my_arc(x1, y1, HEAD, HEAD, x2, y2); my_arc(x2, y2, x2 + 3, y2 - 1, x3, y3); my_arc(x2, y2, x2 + 1, y2 - 3, x3, y3); my_arc(x3, y3, x4, y4, x5, y5); gc.strokeLine(x4, y4, x4 - 20, y4 - 20); my_arc(x5, y5, x6, y6, x1, y1); // くちばし my_arc(x1, y1, x1 - 5, y1 - 2, x7, y7); my_arc(x6, y6, x6 - 5, y6 - 1, x7, y7); my_arc(x5, y5, x5 - 5, y5 + 2, x7, y7); // 目 gc.strokeOval(0.75 * HEAD, HEAD / 3, 10, 10); } private static final double DEG = 180 / Math.PI; private void my_arc(double x1, double y1, double x2, double y2, double x3, double y3) { double nx1 = x2 - x1, ny1 = y2 - y1, nx2 = x3 - x2, ny2 = y3 - y2; double l1 = (nx1 * (x1 + x2) + ny1 * (y1 + y2)) / 2, l2 = (nx2 * (x2 + x3) + ny2 * (y2 + y3)) / 2; double det = nx1 * ny2 - nx2 * ny1; double cx = (ny2 * l1 - ny1 * l2) / det, cy = (-nx2 * l1 + nx1 * l2) / det; double r = Math.sqrt(Math.pow(x1 - cx, 2) + Math.pow(y1 - cy, 2)); double a1 = Math.atan2(y1 - cy, x1 - cx) * DEG, a2 = Math.atan2(y3 - cy, x3 - cx) * DEG, da = a2 - a1; int sign = det > 0 ? 1 : -1; if ((sign > 0) != (da > 0)) da += sign * 360; gc.strokeArc(cx - r, cy - r, 2 * r, 2 * r, -a1, -da, ArcType.OPEN); } }