画布不绘制平滑线条 [英] Canvas does not draw smooth lines

查看:149
本文介绍了画布不绘制平滑线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javafx画布中进行徒手绘制时,我无法获得anitialiazed线。
以下是代码......

I am not able to get anitialiazed lines when doing freehand drawing in javafx canvas. Following is the code ...

import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.scene.shape.*;
import javafx.stage.*;

public class Test2 extends Application {
  GraphicsContext gc;

  public static void main(String[] args) { launch(args); }

  private class MouseDragged implements EventHandler<MouseEvent> {
      public void handle( MouseEvent e ) {
        gc.lineTo( e.getX(), e.getY() );
        gc.stroke();
      }
  }

  private class MousePressed implements EventHandler<MouseEvent> {
      public void handle( MouseEvent e ) {
        gc.moveTo( e.getX(), e.getY() );
      }
  }

  @Override
  public void start(Stage stage) {
    Canvas canvas = new Canvas(500, 500);
    canvas.setOnMouseDragged( new MouseDragged() );
    canvas.setOnMousePressed( new MousePressed() );

    gc = canvas.getGraphicsContext2D();
    gc.setLineCap( StrokeLineCap.ROUND );
    gc.setLineJoin( StrokeLineJoin.ROUND );
    gc.setLineWidth( 0.1 );

    Group root = new Group(canvas);
    Scene scene = new Scene(root);
    stage.setTitle("Test2");
    stage.setScene(scene);
    stage.show();
  }
}

如果我创建一个swing应用,我可以提供渲染提示使其平滑...

While if I create a swing app I can provide rendering hints to smoothen it out ...

g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );

如何在画布中为图形上下文提供渲染命中?

How do I provide rendering hits to the graphics context in canvas?

推荐答案

尝试将此效果添加到 GraphicsContext

BoxBlur blur = new BoxBlur();
blur.setWidth(1);
blur.setHeight(1);
blur.setIterations(1);
gc.setEffect(blur);

所以(例如)你的开始方法现在看起来像这样:

So (as an example) your start method would now look like this:

  @Override
  public void start(Stage stage) {
    Canvas canvas = new Canvas(500, 500);
    canvas.setOnMouseDragged( new MouseDragged() );
    canvas.setOnMousePressed( new MousePressed() );

    gc = canvas.getGraphicsContext2D();
    gc.setLineCap( StrokeLineCap.ROUND );
    gc.setLineJoin( StrokeLineJoin.ROUND );
    gc.setLineWidth( 1 );

    BoxBlur blur = new BoxBlur();
    blur.setWidth(1);
    blur.setHeight(1);
    blur.setIterations(1);
    gc.setEffect(blur);

    Group root = new Group(canvas);
    Scene scene = new Scene(root);
    stage.setTitle("Test2");
    stage.setScene(scene);
    stage.show();
  }

这不完美......但我发现最好的东西远。

It's not exactly perfect... but the best thing I've found so far.

这篇关于画布不绘制平滑线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆