首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javafx中带有画布的DrawCircle (GraphicsContext gc)

Javafx中带有画布的DrawCircle (GraphicsContext gc)
EN

Stack Overflow用户
提问于 2014-05-27 21:19:27
回答 2查看 19.2K关注 0票数 4

我不得不在JavaFX中用画布绘制规则多边形,我也怀疑如何用GraphicsContext设计一个带画布的圆圈。

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

代码语言:javascript
复制
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;
  }
}

我有这个循环类,我对公共无效drawCircle(GraphicsContext gc)方法表示怀疑。

代码语言:javascript
复制
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中

代码语言:javascript
复制
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);
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-27 22:09:50

抚摸:

代码语言:javascript
复制
getGraphicsContext2D().strokeOval(center.x-radius, center.y-radius, radius, radius);

填充:

代码语言:javascript
复制
getGraphicsContext2D().fillOval(center.x-radius, center.y-radius, radius, radius);
票数 2
EN

Stack Overflow用户

发布于 2016-12-07 02:54:05

抚摸:

getGraphicsContext2D().strokeOval(center.x-radius,中心.y-半径,半径* 2,半径* 2);

填充:

getGraphicsContext2D().fillOval(center.x-radius,中心.y-半径,半径* 2,半径* 2);

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

代码语言:javascript
复制
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. 
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23899135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档