使用JavaFX画哆啦A梦
时间: 2023-06-24 09:04:36 浏览: 48
好的,以下是一个简单的JavaFX程序,可以画出哆啦A梦的图案:
```java
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Doraemon extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// 设置舞台大小和标题
primaryStage.setTitle("哆啦A梦");
primaryStage.setWidth(500);
primaryStage.setHeight(500);
// 创建一个画布
Group root = new Group();
Scene scene = new Scene(root, Color.WHITE);
primaryStage.setScene(scene);
// 画身体
Circle body = new Circle(250, 250, 150);
body.setFill(Color.BLUE);
body.setStroke(Color.BLACK);
body.setStrokeWidth(3);
root.getChildren().add(body);
// 画腰带
Rectangle belt = new Rectangle(150, 300, 200, 30);
belt.setFill(Color.RED);
belt.setStroke(Color.BLACK);
belt.setStrokeWidth(3);
root.getChildren().add(belt);
// 画铃铛
Circle bell = new Circle(250, 315, 20);
bell.setFill(Color.YELLOW);
bell.setStroke(Color.BLACK);
bell.setStrokeWidth(3);
root.getChildren().add(bell);
// 画脸
Circle face = new Circle(250, 200, 100);
face.setFill(Color.WHITE);
face.setStroke(Color.BLACK);
face.setStrokeWidth(3);
root.getChildren().add(face);
// 画鼻子
Circle nose = new Circle(250, 215, 15);
nose.setFill(Color.BLACK);
root.getChildren().add(nose);
// 画眼睛
Circle leftEye = new Circle(200, 175, 30);
leftEye.setFill(Color.WHITE);
leftEye.setStroke(Color.BLACK);
leftEye.setStrokeWidth(3);
root.getChildren().add(leftEye);
Circle rightEye = new Circle(300, 175, 30);
rightEye.setFill(Color.WHITE);
rightEye.setStroke(Color.BLACK);
rightEye.setStrokeWidth(3);
root.getChildren().add(rightEye);
Circle leftPupil = new Circle(200, 175, 10);
leftPupil.setFill(Color.BLACK);
root.getChildren().add(leftPupil);
Circle rightPupil = new Circle(300, 175, 10);
rightPupil.setFill(Color.BLACK);
root.getChildren().add(rightPupil);
// 画嘴巴和胡须
Polygon mouth = new Polygon();
mouth.getPoints().addAll(new Double[]{
200.0, 250.0,
300.0, 250.0,
280.0, 270.0,
260.0, 270.0
});
mouth.setFill(Color.WHITE);
mouth.setStroke(Color.BLACK);
mouth.setStrokeWidth(3);
root.getChildren().add(mouth);
Polygon leftWhiskers = new Polygon();
leftWhiskers.getPoints().addAll(new Double[]{
165.0, 230.0,
160.0, 235.0,
150.0, 230.0
});
leftWhiskers.setFill(Color.WHITE);
root.getChildren().add(leftWhiskers);
Polygon rightWhiskers = new Polygon();
rightWhiskers.getPoints().addAll(new Double[]{
335.0, 230.0,
340.0, 235.0,
350.0, 230.0
});
rightWhiskers.setFill(Color.WHITE);
root.getChildren().add(rightWhiskers);
// 显示舞台
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
你可以运行这个程序,看看效果。当然,你也可以根据自己的喜好和想象,对这个程序进行修改和扩展,创作出更加丰富多彩的哆啦A梦图案。