8

在 javafx 画布中进行手绘时,我无法获得已消除的线条。以下是代码...

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();
  }
}

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

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

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

4

1 回答 1

6

尝试将此效果添加到您的GraphicsContext

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

所以(作为一个例子)你的start方法现在看起来像这样:

  @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();
  }

它并不完全完美......但到目前为止我发现的最好的东西。

于 2013-05-31T22:07:02.320 回答