java绘制图形代码
时间: 2024-06-24 22:01:37 浏览: 117
在Java中,我们可以使用Swing或JavaFX库来创建图形用户界面(GUI)并绘制各种形状和元素。这里简单介绍如何使用JavaFX绘制基本图形:
1. 导入所需的库:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
```
2. 创建一个继承自`Application`的类,并覆盖`start`方法:
```java
public class ShapeDrawingApp extends Application {
@Override
public void start(Stage primaryStage) {
//...
}
}
```
3. 在`start`方法中,设置场景和绘制图形:
```java
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(new StackPane(), 400, 400); // 设置舞台大小
// 绘制一个圆
Circle circle = new Circle(50, Color.BLUE);
circle.setTranslateX(150); // 设置圆的位置
scene.getChildren().add(circle);
// 绘制一条线
Line line = new Line(50, 50, 300, 300);
line.setStroke(Color.BLACK);
line.setStrokeWidth(2);
scene.getChildren().add(line);
// 显示主舞台
primaryStage.setScene(scene);
primaryStage.
阅读全文
相关推荐














