欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Java语言程序设计基础篇_编程练习题*15.23 (自动改变大小的停止标识)

Java语言程序设计基础篇_编程练习题*15.23 (自动改变大小的停止标识)

2024/10/24 18:19:22 来源:https://blog.csdn.net/2301_78998594/article/details/140656055  浏览:    关键词:Java语言程序设计基础篇_编程练习题*15.23 (自动改变大小的停止标识)
*15.23 (自动改变大小的停止标识)

重写编程练习题14.15, 当窗体改变大小的时候,停止标识的宽度和髙度自动改变大小

  • 习题思路
  1. 新建一个Polygon()绘出八边形,new一个Label表示文字STOP
  2. 新建一个方法,被调用时重新绘制八边形,并重新设置文字位置
  3. 监听Scene,当大小改变时,调用第二步新建的方法

代码示例:编程练习题15_23AutoChangeSizeStop.java

package chapter_15;import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;public class 编程练习题15_23AutoChangeSizeStop extends Application{Polygon polygon = new Polygon();Label text = new Label("STOP");@Overridepublic void start(Stage primaryStage) throws Exception {Pane pane = new StackPane();pane.getChildren().add(polygon);polygon.setFill(Color.RED);//polygon.setStroke(Color.BLACK);ObservableList<Double> list = polygon.getPoints();final double WIDTH = 200, HEIGHT = 200;double centerX = WIDTH / 2, centerY = HEIGHT / 2;double radius = Math.min(WIDTH, HEIGHT)*0.4;for(int i = 0;i < 8;i++) {list.add(centerX + radius * Math.cos(34.95+2 * i* Math.PI / 8));list.add(centerY -radius * Math.sin(34.95+2 * i * Math.PI / 8));}text.setTextFill(Color.WHITE);text.setFont(Font.font("Times New Roman", FontWeight.BOLD, 30));pane.getChildren().add(text);Scene scene = new Scene(pane, WIDTH, HEIGHT);scene.widthProperty().addListener((observable, oldValue, newValue) -> resizeComponents(newValue.doubleValue(), scene.getHeight()));scene.heightProperty().addListener((observable, oldValue, newValue) -> resizeComponents(scene.getWidth(), newValue.doubleValue()));primaryStage.setTitle("编程练习题15_23AutoChangeSizeStop");primaryStage.setScene(scene);primaryStage.show();}public static void main(String[] args) {Application.launch(args);}private void resizeComponents(double width, double height) {double centerX = width / 2;double centerY = height / 2;double radius = Math.min(width, height) * 0.4;// Update polygon pointsObservableList<Double> points = polygon.getPoints();points.clear();for (int i = 0; i < 8; i++) {points.add(centerX + radius * Math.cos(34.95+2 * i* Math.PI / 8));points.add(centerY -radius * Math.sin(34.95+2 * i * Math.PI / 8));}// Update label propertiesdouble fontSize = radius / 3; // Adjust font size based on radiustext.setLayoutX(centerX - text.getLayoutBounds().getWidth() / 2);text.setLayoutY(centerY + text.getLayoutBounds().getHeight() / 2);text.setFont(Font.font("Times New Roman", FontWeight.BOLD, fontSize));}
}
  • 结果展示

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com