在 JavaFX 画布上绘制形状
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40729967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing shapes on JavaFX canvas
提问by swch
Let's say I have some shapes like:
假设我有一些形状,例如:
Shape s1 = new Rectangle(10, 10);
Shape s2 = new Circle(10);
etc. I would like to draw them on canvas. In Swing it was possible through Graphics2D.draw(Shape shape) method, but I don't see equivalent in JavaFX GraphicsContext. Is there something like this in JavaFX?
等等。我想在画布上绘制它们。在 Swing 中,可以通过 Graphics2D.draw(Shape shape) 方法,但我在 JavaFX GraphicsContext 中看不到等效项。JavaFX 中有这样的东西吗?
回答by D3181
Im not entirely sure its possible to draw the objects directly onto the canvas in the way you would expect, it is to draw it directly onto the stage node, such as the example from here:
我不完全确定是否可以按照您期望的方式将对象直接绘制到画布上,而是将其直接绘制到舞台节点上,例如此处的示例:
package javafx8_shape;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
*
* @web java-buddy.blogspot.com
*/
public class JavaFX8_Shape extends Application {
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 500, 500, Color.BLACK);
//Filled rectangle
Rectangle rect1 = new Rectangle(10, 10, 200, 200);
rect1.setFill(Color.BLUE);
//Transparent rectangle with Stroke
Rectangle rect2 = new Rectangle(60, 60, 200, 200);
rect2.setFill(Color.TRANSPARENT);
rect2.setStroke(Color.RED);
rect2.setStrokeWidth(10);
//Rectangle with Stroke, no Fill color specified
Rectangle rect3 = new Rectangle(110, 110, 200, 200);
rect3.setStroke(Color.GREEN);
rect3.setStrokeWidth(10);
root.getChildren().addAll(rect1, rect2, rect3);
primaryStage.setTitle("java-buddy.blogspot.com");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
however the canvas API is generally the way in which you draw the shapes, rectangle etc through method calls. So to recap you can even draw rectangle objects onto other nodes such as a HBOX:
然而,画布 API 通常是您通过方法调用绘制形状、矩形等的方式。所以回顾一下,您甚至可以将矩形对象绘制到其他节点上,例如 HBOX:
HBox root = new HBox(rectangle);
But drawing it onto the canvas is usually done like this:
但是将它绘制到画布上通常是这样完成的:
gc.setFill(Color.WHITESMOKE);
gc.fillRect(gc.getCanvas().getLayoutX(),
gc.getCanvas().getLayoutY(),
gc.getCanvas().getWidth(),
gc.getCanvas().getHeight());
gc.setFill(Color.GREEN);
gc.setStroke(Color.BLUE);
The best alternative would be to develop methods, which you pass your objects to and then use the API to draw using the dimensions of your object onto the canvas...
最好的选择是开发方法,将对象传递给这些方法,然后使用 API 将对象的尺寸绘制到画布上......
private void drawRectangle(GraphicsContext gc,Rectangle rect){
gc.setFill(Color.WHITESMOKE);
gc.fillRect(rect.getX(),
rect.getY(),
rect.getWidth(),
rect.getHeight());
gc.setFill(Color.GREEN);
gc.setStroke(Color.BLUE);
return gc;
}