import javafx.application.Application; import javafx.geometry.Pos; import javafx.geometry.VPos; import javafx.scene.Scene; import javafx.scene.SnapshotParameters; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.control.Button; import javafx.scene.effect.BlendMode; import javafx.scene.effect.DropShadow; import javafx.scene.effect.Reflection; import javafx.scene.image.ImageView; import javafx.scene.image.PixelWriter; import javafx.scene.image.WritableImage; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.paint.CycleMethod; import javafx.scene.paint.ImagePattern; import javafx.scene.paint.LinearGradient; import javafx.scene.paint.RadialGradient; import javafx.scene.paint.Stop; import javafx.scene.shape.ArcType; import javafx.scene.shape.StrokeLineCap; import javafx.scene.shape.StrokeLineJoin; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontSmoothingType; import javafx.scene.text.FontWeight; import javafx.scene.text.TextAlignment; import javafx.scene.transform.Rotate; import javafx.stage.Stage; public class CanvasEx extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) throws Exception { VBox root = new VBox(15); root.setAlignment(Pos.CENTER); Button button1 = new Button("Canvas"); Button button2 = new Button("Фон и обводка"); Button button3 = new Button("Рисование пикселей и линий"); Button button4 = new Button("Рисование траектории"); Button button5 = new Button("Рисование фигур"); Button button6 = new Button("Вывод текста"); Button button7 = new Button("Вывод изображения"); Button button8 = new Button("Эффекты и трансформации"); Button button9 = new Button("Вращение вокруг центра"); Button button10 = new Button("applyEffect"); Button button11 = new Button("Рисование мышью"); button1.setOnAction(event -> { newWindow(); }); button2.setOnAction(event -> { newWindow2(); }); button3.setOnAction(event -> { newWindow3(); }); button4.setOnAction(event -> { newWindow4(); }); button5.setOnAction(event -> { newWindow5(); }); button6.setOnAction(event -> { newWindow6(); }); button7.setOnAction(event -> { newWindow7(); }); button8.setOnAction(event -> { newWindow8(); }); button9.setOnAction(event -> { newWindow9(); }); button10.setOnAction(event -> { newWindow10(); }); button11.setOnAction(event -> { newWindow11(); }); root.getChildren().addAll(button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11); Scene scene = new Scene(root, 400, 500); stage.setTitle("Canvas"); stage.setScene(scene); stage.show(); } public void newWindow() { Stage window = new Stage(); Pane pane = new Pane(); Canvas canvas = new Canvas(); canvas.setWidth(400.0); canvas.setHeight(150.0); GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(Color.GREEN); gc.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); canvas.relocate(20.0, 20.0); System.out.println(canvas.getWidth()); System.out.println(canvas.getHeight()); Canvas canvas2 = new Canvas(400.0, 150.0); GraphicsContext gc2 = canvas2.getGraphicsContext2D(); gc2.setFill(Color.WHITE); gc2.setStroke(Color.BLACK); gc2.fillRect(0.0, 0.0, canvas2.getWidth(), canvas2.getHeight()); gc2.strokeRect(1.0, 1.0, canvas2.getWidth() - 2.0, canvas2.getHeight() - 2.0); canvas2.relocate(20.0, 200.0); canvas.setEffect(new DropShadow()); pane.getChildren().addAll(canvas, canvas2); Scene scene = new Scene(pane, 500.0, 400.0); window.setScene(scene); window.setTitle("Canvas"); window.show(); } public void newWindow2() { Stage window = new Stage(); Pane pane = new Pane(); Canvas canvas = new Canvas(400.0, 300.0); GraphicsContext gc = canvas.getGraphicsContext2D(); System.out.println(gc.getFill()); // 0x000000ff gc.setFill(Color.WHITE); gc.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); System.out.println(gc.getFill()); gc.setFill(Color.BLACK); gc.fillRect(10.0, 10.0, 40.0, 40.0); Stop[] stops = new Stop[] { new Stop(0, Color.BLACK), new Stop(1, Color.WHITE) }; gc.setFill(new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops)); gc.fillRect(60.0, 10.0, 40.0, 40.0); gc.setFill(new RadialGradient(240.0, 0.7, 0.5, 0.5, 0.5, true, CycleMethod.REFLECT, stops)); gc.fillRect(110.0, 10.0, 40.0, 40.0); WritableImage im = new WritableImage(10, 10); PixelWriter pw = im.getPixelWriter(); for (int x = 0, w = (int) im.getWidth(); x < w; x++) { for (int y = 0, h = (int) im.getHeight(); y < h; y++) { if (x > 3 && x < 8) pw.setColor(x, y, Color.WHITE); else pw.setColor(x, y, Color.CHOCOLATE); } } gc.setFill(new ImagePattern(im, 0.0, 0.0, im.getWidth(), im.getHeight(), false)); gc.fillRect(160.0, 10.0, 40.0, 40.0); gc.setStroke(Color.RED); gc.strokeRect(10.0, 60.0, 40.0, 40.0); gc.setStroke(Color.BLUE); gc.setLineWidth(5.0); gc.strokeRect(60.0, 60.0, 40.0, 40.0); gc.setLineWidth(10.0); gc.strokeRect(110.0, 60.0, 40.0, 40.0); gc.setStroke(Color.WHITE); gc.setLineWidth(1.0); gc.strokeRect(110.0, 60.0, 40.0, 40.0); gc.setStroke(Color.BLUE); gc.setLineWidth(8.0); gc.setLineDashes(15.0, 40.0); gc.strokeLine(20.0, 120.0, 300.0, 120.0); gc.setLineDashOffset(40.0); gc.strokeLine(20.0, 140.0, 300.0, 140.0); gc.setLineDashOffset(0.0); gc.setLineDashes(); gc.strokeLine(20.0, 160.0, 300.0, 160.0); gc.setStroke(Color.BLACK); gc.setLineWidth(15.0); gc.setLineCap(StrokeLineCap.ROUND); gc.strokeLine(20.0, 180.0, 300.0, 180.0); gc.setLineCap(StrokeLineCap.SQUARE); gc.strokeLine(20.0, 200.0, 300.0, 200.0); gc.setLineCap(StrokeLineCap.BUTT); gc.strokeLine(20.0, 220.0, 300.0, 220.0); gc.setStroke(Color.WHITE); gc.setLineWidth(3.0); gc.strokeLine(20.0, 180.0, 300.0, 180.0); gc.strokeLine(20.0, 200.0, 300.0, 200.0); gc.strokeLine(20.0, 220.0, 300.0, 220.0); gc.setStroke(Color.RED); gc.setLineWidth(15.0); gc.setLineJoin(StrokeLineJoin.MITER); gc.strokePolyline(new double[] {100.0, 30.0, 100.0}, new double[] {250.0, 290.0, 290.0}, 3); gc.setLineJoin(StrokeLineJoin.ROUND); gc.strokePolyline(new double[] {200.0, 120.0, 200.0}, new double[] {250.0, 290.0, 290.0}, 3); gc.setLineJoin(StrokeLineJoin.BEVEL); gc.strokePolyline(new double[] {300.0, 220.0, 300.0}, new double[] {250.0, 290.0, 290.0}, 3); gc.setStroke(Color.CHOCOLATE); gc.setLineJoin(StrokeLineJoin.MITER); gc.setMiterLimit(3.0); gc.strokePolyline(new double[] {300.0, 220.0, 300.0}, new double[] {50.0, 90.0, 90.0}, 3); gc.setStroke(Color.WHITE); gc.setLineWidth(3.0); gc.setLineJoin(StrokeLineJoin.MITER); gc.strokePolyline(new double[] {100.0, 30.0, 100.0}, new double[] {250.0, 290.0, 290.0}, 3); gc.strokePolyline(new double[] {200.0, 120.0, 200.0}, new double[] {250.0, 290.0, 290.0}, 3); gc.strokePolyline(new double[] {300.0, 220.0, 300.0}, new double[] {250.0, 290.0, 290.0}, 3); gc.strokePolyline(new double[] {300.0, 220.0, 300.0}, new double[] {50.0, 90.0, 90.0}, 3); canvas.relocate(50.0, 50.0); //System.out.println(gc.getCanvas()); SnapshotParameters sp = new SnapshotParameters(); sp.setFill(Color.TRANSPARENT); WritableImage wim = canvas.snapshot(sp, null); ImageView iv =new ImageView(wim); iv.relocate(50.0, 400.0); pane.getChildren().addAll(canvas, iv); Scene scene = new Scene(pane, 500.0, 750.0); window.setScene(scene); window.setTitle("Canvas"); window.show(); } public void newWindow3() { Stage window = new Stage(); Pane pane = new Pane(); Canvas canvas = new Canvas(400.0, 450.0); GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(Color.WHITE); gc.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); PixelWriter pw = gc.getPixelWriter(); for (int x = 20; x < 61; x++) { for (int y = 20; y < 61; y++) { pw.setColor(x, y, Color.RED); } } gc.setStroke(Color.BLUE); gc.setLineWidth(5.0); gc.strokeLine(80.0, 20.0, 350.0, 20.0); gc.setLineDashes(15.0, 40.0); gc.strokeLine(80.0, 30.0, 350.0, 30.0); gc.setLineDashOffset(40.0); gc.strokeLine(80.0, 40.0, 350.0, 40.0); gc.setLineDashes(); gc.setStroke(Color.RED); gc.setLineWidth(5.0); gc.setLineCap(StrokeLineCap.ROUND); gc.strokeLine(80.0, 50.0, 80.0, 50.0); gc.setStroke(Color.BLACK); gc.setLineWidth(3.0); gc.setLineJoin(StrokeLineJoin.MITER); gc.strokePolyline(new double[] {100.0, 20.0, 100.0}, new double[] {80.0, 120.0, 120.0}, 3); gc.setStroke(Color.DARKGREEN); gc.setLineWidth(3.0); gc.strokeArc(10.0, 150.0, 100.0, 100.0, 45.0, 270.0, ArcType.OPEN); gc.strokeArc(150.0, 150.0, 100.0, 100.0, 45.0, 270.0, ArcType.CHORD); gc.strokeArc(300.0, 150.0, 100.0, 100.0, 45.0, 270.0, ArcType.ROUND); gc.setStroke(Color.RED); gc.setLineWidth(5.0); gc.setLineCap(StrokeLineCap.ROUND); gc.strokeLine(10.0, 150.0, 10.0, 150.0); gc.strokeLine(150.0, 150.0, 150.0, 150.0); gc.strokeLine(300.0, 150.0, 300.0, 150.0); gc.setFill(Color.KHAKI); gc.fillArc(10.0, 300.0, 100.0, 100.0, 45.0, 270.0, ArcType.OPEN); gc.fillArc(150.0, 300.0, 100.0, 100.0, 45.0, 270.0, ArcType.CHORD); gc.fillArc(300.0, 300.0, 100.0, 100.0, 45.0, 270.0, ArcType.ROUND); canvas.relocate(50.0, 50.0); pane.getChildren().addAll(canvas); Scene scene = new Scene(pane, 500.0, 550.0); window.setScene(scene); window.setTitle("Canvas"); window.show(); } public void newWindow4() { Stage window = new Stage(); Pane pane = new Pane(); Canvas canvas = new Canvas(400.0, 450.0); GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(Color.WHITE); gc.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); /* // Создание маски gc.beginPath(); gc.rect(100.0, 100.0, 200.0, 200.0); gc.clip(); gc.stroke();*/ gc.setFill(Color.DARKGREEN); gc.setStroke(Color.RED); gc.setLineWidth(3.0); gc.beginPath(); gc.moveTo(20.0, 20.0); gc.lineTo(100.0, 20.0); gc.lineTo(100.0, 80.0); gc.closePath(); gc.stroke(); gc.fill(); gc.setStroke(Color.BLACK); gc.setLineWidth(5.0); gc.beginPath(); gc.moveTo(100.0, 100.0); gc.arcTo(200.0, 0.0, 300.0, 100.0, 100.0); gc.stroke(); gc.setStroke(Color.RED); gc.setLineWidth(1.0); gc.strokeLine(100.0, 100.0, 200.0, 0.0); gc.strokeLine(200.0, 0.0, 300.0, 100.0); gc.strokeOval(100.0, 41.0, 200.0, 200.0); gc.setLineWidth(5.0); gc.setLineCap(StrokeLineCap.ROUND); gc.strokeLine(100.0, 100.0, 100.0, 100.0); gc.strokeLine(200.0, 0.0, 200.0, 0.0); gc.strokeLine(300.0, 100.0, 300.0, 100.0); gc.setLineCap(StrokeLineCap.SQUARE); gc.setStroke(Color.BLACK); gc.setLineWidth(5.0); gc.beginPath(); gc.moveTo(10.0, 200.0); gc.arc(50.0, 200.0, 40.0, 40.0, 180.0, 180.0); gc.arc(130.0, 200.0, 40.0, 40.0, 180.0, -180.0); gc.stroke(); gc.setStroke(Color.RED); gc.setLineWidth(5.0); gc.setLineCap(StrokeLineCap.ROUND); gc.strokeLine(50.0, 200.0, 50.0, 200.0); gc.strokeLine(130.0, 200.0, 130.0, 200.0); gc.setLineCap(StrokeLineCap.SQUARE); gc.setStroke(Color.BLACK); gc.setLineWidth(5.0); gc.beginPath(); gc.moveTo(50.0, 300.0); gc.bezierCurveTo(200.0, 400.0, 200.0, 200.0, 350.0, 300.0); gc.stroke(); gc.setStroke(Color.BLACK); gc.setLineWidth(5.0); gc.beginPath(); gc.moveTo(50.0, 400.0); gc.quadraticCurveTo(200.0, 300.0, 350.0, 400.0); gc.stroke(); gc.setStroke(Color.BLACK); gc.setLineWidth(5.0); gc.beginPath(); gc.moveTo(200.0, 370.0); gc.appendSVGPath("L 150 420 L 250 420"); gc.closePath(); //gc.appendSVGPath("M 200 370 L 150 420 L 250 420 Z"); gc.stroke(); canvas.relocate(50.0, 50.0); pane.getChildren().addAll(canvas); Scene scene = new Scene(pane, 500.0, 550.0); window.setScene(scene); window.setTitle("Canvas"); window.show(); } public void newWindow5() { Stage window = new Stage(); Pane pane = new Pane(); Canvas canvas = new Canvas(400.0, 300.0); GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(Color.WHITE); gc.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); gc.setFill(Color.BLUE); gc.fillRect(20.0, 20.0, 40.0, 40.0); gc.setFill(Color.GREEN); gc.fillRoundRect(80.0, 20.0, 40.0, 40.0, 20.0, 20.0); gc.setStroke(Color.RED); gc.strokeRect(140.0, 20.0, 40.0, 40.0); gc.setStroke(Color.PERU); gc.strokeRoundRect(200.0, 20.0, 40.0, 40.0, 20.0, 20.0); gc.setFill(Color.BLUE); gc.fillOval(20.0, 100.0, 100.0, 40.0); gc.save(); gc.setStroke(Color.RED); gc.setLineWidth(5.0); gc.setLineCap(StrokeLineCap.ROUND); gc.strokeLine(20.0, 100.0, 20.0, 100.0); gc.restore(); gc.setStroke(Color.RED); gc.setLineWidth(3.0); gc.strokeOval(200.0, 100.0, 40.0, 100.0); gc.save(); gc.setStroke(Color.RED); gc.setLineWidth(5.0); gc.setLineCap(StrokeLineCap.ROUND); gc.strokeLine(200.0, 100.0, 200.0, 100.0); gc.restore(); gc.setFill(Color.BLACK); gc.fillPolygon(new double[] {100.0, 20.0, 100.0}, new double[] {180.0, 220.0, 220.0}, 3); gc.setStroke(Color.RED); gc.setLineWidth(3.0); gc.strokePolygon(new double[] {100.0, 20.0, 100.0}, new double[] {250.0, 290.0, 290.0}, 3); canvas.relocate(50.0, 50.0); pane.getChildren().addAll(canvas); Scene scene = new Scene(pane, 500.0, 400.0); window.setScene(scene); window.setTitle("Canvas"); window.show(); } public void newWindow6() { Stage window = new Stage(); Pane pane = new Pane(); Canvas canvas = new Canvas(400.0, 400.0); GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(Color.WHITE); gc.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); System.out.println(gc.getFont()); // Font[name=System Regular, family=System, style=Regular, size=12.0] gc.setFill(Color.BLACK); gc.setFont(new Font(24.0)); gc.fillText("Font(24.0)", 20.0, 40.0); System.out.println(gc.getFontSmoothingType()); gc.setFontSmoothingType(FontSmoothingType.LCD); gc.fillText("Font(24.0)", 200.0, 40.0); gc.setFontSmoothingType(FontSmoothingType.GRAY); System.out.println(gc.getTextAlign()); gc.setFill(Color.BLACK); gc.setFont(Font.font("Verdana", FontWeight.NORMAL, FontPosture.REGULAR, 12.0)); gc.setTextAlign(TextAlignment.RIGHT); gc.fillText("TextAlignment.RIGHT", 150.0, 80.0); gc.setTextAlign(TextAlignment.CENTER); gc.fillText("TextAlignment.CENTER", 150.0, 100.0); gc.setTextAlign(TextAlignment.LEFT); gc.fillText("TextAlignment.LEFT", 150.0, 120.0); gc.save(); gc.setStroke(Color.RED); gc.setLineWidth(5.0); gc.setLineCap(StrokeLineCap.ROUND); gc.strokeLine(150.0, 80.0, 150.0, 80.0); gc.strokeLine(150.0, 100.0, 150.0, 100.0); gc.strokeLine(150.0, 120.0, 150.0, 120.0); gc.restore(); System.out.println(gc.getTextBaseline()); gc.setTextBaseline(VPos.TOP); gc.fillText("VPos.TOP", 50.0, 140.0); gc.setTextBaseline(VPos.CENTER); gc.fillText("VPos.CENTER", 50.0, 170.0); gc.setTextBaseline(VPos.BOTTOM); gc.fillText("VPos.BOTTOM", 50.0, 200.0); gc.setTextBaseline(VPos.BASELINE); gc.fillText("VPos.BASELINE", 50.0, 220.0); gc.save(); gc.setStroke(Color.RED); gc.setLineWidth(5.0); gc.setLineCap(StrokeLineCap.ROUND); gc.strokeLine(50.0, 140.0, 50.0, 140.0); gc.strokeLine(50.0, 170.0, 50.0, 170.0); gc.strokeLine(50.0, 200.0, 50.0, 200.0); gc.strokeLine(50.0, 220.0, 50.0, 220.0); gc.restore(); gc.setFill(Color.BLACK); gc.fillText("Текст", 20.0, 250.0); StringBuilder sb = new StringBuilder(); sb.append("Длинная строка"); for (int i = 1; i < 8; i++) { sb.append(" слово" + i); } String longStr = sb.toString(); gc.fillText(longStr, 20.0, 270.0, 350.0); gc.save(); gc.beginPath(); gc.rect(15.0, 280.0, 350.0, 30.0); gc.clip(); //gc.stroke(); gc.fillText(longStr, 20.0, 300.0); gc.restore(); gc.setStroke(Color.RED); gc.setLineWidth(3.0); gc.setFont(new Font(96.0)); gc.strokeText("OK", 20.0, 390.0); //gc.strokeText("OK", 20.0, 390.0, 200.0); gc.fillText("OK", 200.0, 390.0); canvas.relocate(50.0, 50.0); pane.getChildren().addAll(canvas); Scene scene = new Scene(pane, 500.0, 500.0); window.setScene(scene); window.setTitle("Canvas"); window.show(); } public void newWindow7() { Stage window = new Stage(); Pane pane = new Pane(); Canvas canvas = new Canvas(400.0, 400.0); GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(Color.WHITE); gc.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); WritableImage img = new WritableImage(100, 100); PixelWriter pw = img.getPixelWriter(); for (int x = 0, w = (int) img.getWidth(); x < w; x++) { for (int y = 0, h = (int) img.getHeight(); y < h; y++) { if ((x > 19 && x < 80) && (y > 19 && y < 80)) pw.setColor(x, y, Color.GAINSBORO); else pw.setColor(x, y, Color.CHOCOLATE); } } gc.drawImage(img, 20.0, 20.0); gc.drawImage(img, 20.0, 150.0, 50.0, 50.0); gc.drawImage(img, 20.0, 230.0, 50.0, 100.0); gc.drawImage(img, 20.0, 340.0, 100.0, 50.0); gc.drawImage(img, 0.0, 0.0, 50.0, 50.0, 200.0, 20.0, 50.0, 50.0); gc.drawImage(img, 0.0, 0.0, 50.0, 50.0, 200.0, 100.0, 100.0, 100.0); gc.drawImage(img, 0.0, 0.0, 100.0, 100.0, 200.0, 250.0, 100.0, 100.0); // Очистка всего холста //gc.clearRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); canvas.relocate(50.0, 50.0); pane.getChildren().addAll(canvas); Scene scene = new Scene(pane, 500.0, 500.0); window.setScene(scene); window.setTitle("Canvas"); window.show(); } public void newWindow8() { Stage window = new Stage(); Pane pane = new Pane(); Canvas canvas = new Canvas(400.0, 400.0); GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(Color.WHITE); gc.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); gc.setFill(Color.RED); gc.save(); gc.rotate(45.0); gc.fillRect(40.0, -20.0, 40.0, 40.0); gc.restore(); gc.save(); gc.scale(2.0, 2.0); gc.fillRect(10.0, 40.0, 40.0, 40.0); gc.restore(); gc.setFill(Color.RED); gc.fillRect(200.0, 200.0, 40.0, 40.0); gc.save(); gc.setStroke(Color.GREEN); gc.translate(200.0, 200.0); gc.rotate(45.0); gc.strokeRect(0, 0, 40.0, 40.0); gc.rotate(-180.0); gc.strokeRect(0, 0, 40.0, 40.0); gc.restore(); gc.setFill(Color.CHOCOLATE); gc.fillRect(300.0, 300.0, 40.0, 40.0); gc.save(); //gc.scale(2.0, 2.0); gc.transform(2.0, 0.0, 0.0, 2.0, 0.0, 0.0); gc.setStroke(Color.BLUE); gc.strokeRect(140.0, 140.0, 40.0, 40.0); gc.restore(); gc.save(); gc.setEffect(new DropShadow()); gc.setFill(Color.CORAL); gc.fillRect(50.0, 300.0, 40.0, 40.0); gc.restore(); gc.save(); Reflection reflection = new Reflection(); reflection.setFraction(0.7); gc.setEffect(reflection); gc.setFill(Color.CORAL); gc.fillRect(300.0, 50.0, 40.0, 40.0); gc.restore(); canvas.relocate(50.0, 50.0); pane.getChildren().addAll(canvas); Scene scene = new Scene(pane, 500.0, 500.0); window.setScene(scene); window.setTitle("Canvas"); window.show(); } public void newWindow9() { Stage window = new Stage(); Pane pane = new Pane(); Canvas canvas = new Canvas(400.0, 400.0); GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(Color.WHITE); gc.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); gc.setFill(Color.RED); gc.setStroke(Color.RED); gc.fillRect(200.0, 200.0, 40.0, 40.0); for (int i = 7; i <= 360; i+=7) { gc.save(); gc.rotate(i); gc.strokeRect(200.0, 200.0, 40.0, 40.0); gc.restore(); } gc.setFill(Color.GREEN); gc.setStroke(Color.RED); Rotate r2 = null; double x2 = 100.0, y2 = 100.0, width2 = 40, height2 = 40; System.out.println(gc.getGlobalAlpha()); gc.save(); gc.setGlobalAlpha(0.5); gc.fillRect(x2, y2, width2, height2); gc.restore(); for (int i = 30; i <= 360; i+=30) { gc.save(); r2 = new Rotate(i, x2 + width2 / 2, y2 + height2 / 2); gc.setTransform(r2.getMxx(), r2.getMyx(), r2.getMxy(), r2.getMyy(), r2.getTx(), r2.getTy()); gc.strokeRect(x2, y2, width2, height2); gc.restore(); } gc.save(); gc.setStroke(Color.RED); gc.setLineWidth(5.0); gc.setLineCap(StrokeLineCap.ROUND); gc.strokeLine(x2 + width2 / 2, y2 + height2 / 2, x2 + width2 / 2, y2 + height2 / 2); gc.restore(); gc.save(); gc.setFill(Color.BLUE); double x = 50.0, y = 50.0, width = 40.0, height = 40.0; Rotate r = new Rotate(45.0, x + width / 2, y + height / 2); gc.setTransform(r.getMxx(), r.getMyx(), r.getMxy(), r.getMyy(), r.getTx(), r.getTy()); System.out.println(gc.getTransform()); gc.fillRect(x, y, width, height); gc.restore(); System.out.println(gc.getTransform()); gc.save(); gc.setFill(Color.BLUE); gc.setGlobalAlpha(0.5); gc.fillRect(10.0, 10.0, 20.0, 20.0); gc.restore(); gc.setFill(Color.GREEN); gc.fillRect(300.0, 300.0, 40.0, 40.0); gc.save(); gc.setFill(Color.BLACK); gc.setGlobalBlendMode(BlendMode.OVERLAY); gc.fillRect(320.0, 280.0, 40.0, 40.0); gc.restore(); gc.setFill(Color.BLACK); System.out.println(gc.getGlobalBlendMode()); gc.fillRect(280.0, 320.0, 40.0, 40.0); canvas.relocate(50.0, 50.0); pane.getChildren().addAll(canvas); Scene scene = new Scene(pane, 500.0, 500.0); window.setScene(scene); window.setTitle("Canvas"); window.show(); } public void newWindow10() { Stage window = new Stage(); Pane pane = new Pane(); Canvas canvas = new Canvas(400.0, 400.0); GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(Color.GREEN); gc.fillRect(200.0, 200.0, 100.0, 100.0); gc.fillRect(50.0, 50.0, 100.0, 100.0); DropShadow dropShadow = new DropShadow(); dropShadow.setRadius(5.0); dropShadow.setOffsetX(6.0); dropShadow.setOffsetY(4.0); dropShadow.setColor(Color.BLACK); gc.applyEffect(dropShadow); canvas.relocate(50.0, 50.0); pane.getChildren().addAll(canvas); Scene scene = new Scene(pane, 500.0, 500.0); window.setScene(scene); window.setTitle("Canvas"); window.show(); } public void newWindow11() { Stage window = new Stage(); Pane pane = new Pane(); Canvas canvas = new Canvas(500.0, 500.0); GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setStroke(Color.RED); gc.setLineWidth(3.0); pane.getChildren().addAll(canvas); canvas.setOnMousePressed(event -> { gc.beginPath(); gc.lineTo(event.getX(), event.getY()); gc.stroke(); }); canvas.setOnMouseDragged(event -> { gc.lineTo(event.getX(), event.getY()); gc.stroke(); }); Scene scene = new Scene(pane, 500.0, 500.0); window.setScene(scene); window.setTitle("Нарисуйте что-нибудь мышью"); window.show(); } }