有没有只对reactangle和椭圆形三角形的方法?
canvas.getGraphicsContext2D()
答案 0 :(得分:0)
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class BasicOpsTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Drawing Operations Test");
Group root = new Group();
Canvas canvas = new Canvas(300, 250);
GraphicsContext gc = canvas.getGraphicsContext2D();
drawShapes(gc);
root.getChildren().add(canvas);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
private void drawShapes(GraphicsContext gc) {
int firstX = 90;
int secondX = 190;
int thirdX = 10;
int firstY =30;
int secondY =170;
int thirdY =170;
gc.setFill(Color.GREEN);
gc.setStroke(Color.BLUE);
gc.fillPolygon(new double[]{firstX, secondX,thirdX},
new double[]{firstY, secondY, thirdY}, 3);
gc.strokePolygon(new double[]{firstX, secondX,thirdX},
new double[]{firstY, secondY, thirdY}, 3);
gc.setFill(javafx.scene.paint.Color.BLACK);
gc.fillText("1", firstX - 3, firstY - 4);
gc.fillText("2", secondX - 3, secondY - 4);
gc.fillText("3", thirdX - 3, thirdY - 4);
}
}