我有这种方法使用canvas在JavaFX中绘制笛卡尔平面
public class Grafics extends StackPane {
private Canvas canvas;
public void Grafics(){
GridPane grid = new GridPane();
grid.setPadding(new Insets(5));
grid.setHgap(10);
grid.setVgap(10);
canvas = new Canvas();
canvas.setHeight(500);
canvas.setWidth(700);
GridPane.setHalignment(canvas, HPos.CENTER);
grid.add(canvas, 0, 2);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setFill(Color.WHITE);
gc.fillRect(1, 1, canvas.getWidth() - 2, canvas.getHeight() - 2);
drawAxesXY(gc); //call the method drawAxes
getChildren().addAll(grid);// add an gridpane in stackpane
}
private void drawAxesXY(GraphicsContext gc1) {
gc1 = canvas.getGraphicsContext2D().getPixelWriter();
PixelWriter pixelWriter = gc1.getPixelWriter();
gc1.setFill(Color.BLACK);
gc1.setStroke(Color.BLACK);
gc1.setLineWidth(1.5);
gc1.strokeText("Y", 350, …Run Code Online (Sandbox Code Playgroud) 我必须做一些projet在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;
}
}
Run Code Online (Sandbox Code Playgroud)
我有这个圆圈类,我怀疑使方法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 = …Run Code Online (Sandbox Code Playgroud)