javafx - DrawCircle (GraphicsContext gc) 与 Javafx 中的 Canvas

标签 javafx javafx-8

我必须做一些项目才能在 JavaFX 中使用 Canvas 绘制正多边形,并且我怀疑如何使用 GraphicsContext 设计带有 Canvas 的圆

我有这个点类包含两个轴 (x, y)

public class Point2D {

         private float mX;
         private float mY;

         public Point2D () {
            this (0,0);
         }

         public Point2D (float x, float y) {
             mX = x;
             mY = y;
         }

        public float getX() {
           return mX;
          }

       public float getY() {
          return mY;
  }
}

而且我有这个圈类,我怀疑制作方法 public void drawCircle(GraphicsContext gc)
public class Circle{

    private Point2D mCenter;
    private Color color;
    private float mRadius;

public Circle (Point2D center, Color color, float radius ) {
         this.mCenter = center;
         this.color = color;
         this.mRadius = radius;
     }

public void drawCircle(GraphicsContext gc) { // My Doubt is here
        Canvas canvas = new Canvas();
        gc = canvas .getGraphicsContext2D();
        gc.setFill(Color.WHITE);
        gc.setStroke(Color.BLACK);

    } 
}

在主 JavaFX
public class PaintGeometricoFX extends Application {

private BorderPane root;

 @Override
    public void start(Stage primaryStage) {

         Point2D p = new Point2D(0, 0);
         Float radius = 4.0f;

         Circle circle = new Circle(p.getX(), p.getY(),Color.BLACK,radius)

         Canvas canvas = new Canvas();
         GraphicsContext gc = imagem.getGraphicsContext2D();

         circle.drawCircle(gc);

          root.setCenter(canvas);


        Scene scene = new Scene(root, 1152, 800);

        primaryStage.setTitle("PAINT");
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

最佳答案

抚摸:

getGraphicsContext2D().strokeOval(center.x-radius, center.y-radius, radius * 2, radius * 2);

填充:

getGraphicsContext2D().fillOval(center.x-radius, center.y-radius, radius * 2, radius * 2);

注意第三个和第四个参数是直径而不是半径。我在 ScalaFx 和正确的 ScalaJs 输出之间存在差异。但我检查了 JavaFx 文档,它的工作原理是一样的:

fillOval

public void fillOval(double x,
                     double y,
                     double w,
                     double h)

Fills an oval using the current fill paint.

This method will be affected by any of the global common or fill attributes as specified in the Rendering Attributes Table.

Parameters:
    x - the X coordinate of the upper left bound of the oval.
    y - the Y coordinate of the upper left bound of the oval.
    w - the width at the center of the oval.
    h - the height at the center of the oval. 

关于javafx - DrawCircle (GraphicsContext gc) 与 Javafx 中的 Canvas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23899135/

相关文章:

Eclipse/Java9 : how to access internal javafx packages?

java - 访问 GridPane 节点时出现问题

javafx 将按钮添加到网格 Pane

java - 如何将 JavaFX Slider 设置为时间格式?

java - 在 HijrahChronology 中配置自定义变体以进行日期校正 jdk 8

java - 启动JavaFX程序时按钮为 "selected"

java - 如何从 JavaFx 中的任务发送 int?

JavaFx 列表中带有奇怪的边框

java - 绑定(bind)到标签时格式化整数

java - 传递到持久化的分离实体 - Hibernate + JavaFX