Canvas画布

  • 创建画布Canvas canvas = new Canvas(600, 600);

  • 获取画笔(画布内描述物体超出画布部分会被裁切)GraphicsContext graphicsContext2D = canvas.getGraphicsContext2D();

  • 设置画笔填充颜色graphicsContext2D.setFill(Color.RED);

  • 设置画笔边框颜色graphicsContext2D.setStroke(Color.BLUE);

  • 设置画笔边框宽度graphicsContext2D.setLineWidth(4);

  • 画矩形graphicsContext2D.fillRect(100, 100, 100, 100);

  • 画矩形边框graphicsContext2D.strokeRect(200, 200, 100, 100);

  • 画线graphicsContext2D.strokeLine(300, 200, 300, 100);

  • 画多边形graphicsContext2D.fillPolygon(new double[]{400, 300, 500}, new double[]{100, 300, 300}, 3);

  • 设置字体大小graphicsContext2D.setFont(Font.font(20));

  • 画文字graphicsContext2D.fillText("Hello World", 200, 350);

  • 画带边框文字graphicsContext2D.strokeText("Hello World", 100, 350);

  • 清除画布graphicsContext2D.clearRect(0,0,200,200);

  • 旋转画布graphicsContext2D.rotate(45);

  • 开始绘制graphicsContext2D.beginPath();

  • 设置路径graphicsContext2D.appendSVGPath("m100,100 c50,-50,150,50,200,0 z");

  • 设置线条连接点为圆形graphicsContext2D.setLineJoin(StrokeLineJoin.ROUND);

  • 绘制边框graphicsContext2D.stroke();

  • 绘制填充graphicsContext2D.fill();

  • 绘制二次贝塞尔曲线graphicsContext2D.quadraticCurveTo(200, 300, 300, 200);

  • 图片绘制(图片路径,从图片(0,0)点开始取 宽100 高100 画到画布上(10,10)点画 宽200 高200的图片(缩放))

graphicsContext2D.drawImage(new Image("file:E://Users//86158//图片//0ea0dc59c3c01450e33a1d2131f1c6f.jpg"),0,0,100,100,10,10,200,200);
  • 像素绘制
PixelWriter pixelWriter = graphicsContext2D.getPixelWriter();  
for (int i = 0; i < 100; i++) {  
    for (int j = 0; j < 100; j++) {  
        pixelWriter.setColor(i, j, Color.BLACK);  
    }  
}
  • 设置画笔透明度graphicsContext2D.setGlobalAlpha(0.5);

  • 保存画布状态graphicsContext2D.save();

  • 设置画笔阴影graphicsContext2D.setEffect(new DropShadow());

  • 恢复画布状态graphicsContext2D.restore();