在Javafx中使用Canvas绘制DrawCircle(GraphicsContext gc)

时间:2014-05-27 21:19:28

标签: javafx javafx-8

我必须做一些projet在JavaFX中使用Canvas绘制正多边形,我怀疑如何使用GraphicsContext设计带有画布的圆

我有这个包含两个轴(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);
    }
}

2 个答案:

答案 0 :(得分:4)

描边:

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

填料:

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

答案 1 :(得分:2)

描边:

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

填料:

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

注意第3和第4个参数是直径而不是半径。我在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.