当前位置:网站首页>6.gui (graphics, filling)

6.gui (graphics, filling)

2022-06-22 16:18:00 Xiaomurong

6 GUI
Write a program that can draw all kinds of graphics , As shown in the figure below , You use radio buttons to select drawings , Use the check button to specify whether to fill .
 Insert picture description here
 Insert picture description here

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class JavaFx extends Application {
    
	private double paneWidth = 550;
	private double paneHeight = 200;

	@Override
	public void start(Stage args) throws Exception {
    
		StackPane pane = new StackPane();

		Circle circle = new Circle(20, 20, 60);
		circle.setStroke(Color.BLACK);//  edge 
		circle.setFill(Color.WHITE);//  Default initial white 

		Rectangle rectangle = new Rectangle(20, 20, 100, 60);
		rectangle.setStroke(Color.BLACK);

		Ellipse ellipse = new Ellipse(20, 20, 50, 70);
		ellipse.setStroke(Color.BLACK);

		pane.setStyle("-fx-border-color:black");//  Palette border color 
		pane.getChildren().add(circle);

		RadioButton btcircle = new RadioButton("Circle");//  Radio button   side   There is a text 
		btcircle.setTextFill(Color.BLACK);//  Black text text 
		btcircle.setSelected(true);//  Radio button   The initial state   Choose 
		btcircle.setLayoutX(5);//  Set the button position 
		btcircle.setLayoutY(270);
		btcircle.setPadding(new Insets(5, 5, 5, 5));//  Set each button ( Between ) padding 

		RadioButton btrectangle = new RadioButton("Rectangle");
		btrectangle.setTextFill(Color.BLACK);

		RadioButton btellipse = new RadioButton("Eclipse");
		btellipse.setTextFill(Color.BLACK);

		CheckBox btfill = new CheckBox("Fill");//  Check box ( If you have selected another line , This button is still available )
		btfill.setTextFill(Color.BLACK);

		ToggleGroup group = new ToggleGroup();
		btcircle.setToggleGroup(group);
		btcircle.setSelected(true);//  Choose... From the beginning 
		btrectangle.setToggleGroup(group);
		btellipse.setToggleGroup(group);

		HBox hbox = new HBox(5);// HBox  The layout panel can easily arrange all the controls in a row .
		hbox.setSpacing(50);//  Set the distance between the controls 
// hbox.setPadding(null);// Set control and  HBox  The distance between the edges 
		hbox.setAlignment(Pos.CENTER);
		hbox.getChildren().addAll(btcircle, btrectangle, btellipse, btfill);//  take 4 Put a button into the scene 
		hbox.setAlignment(Pos.CENTER);

		BorderPane borderPane = new BorderPane();
		borderPane.setCenter(pane);
		borderPane.setBottom(hbox);

		Scene scene = new Scene(borderPane, paneWidth, paneHeight + 40);
		args.setTitle(" curriculum design ");
		args.setScene(scene);
		args.show();

		btcircle.setOnAction(e -> {
    
			pane.getChildren().clear();//  Clear the previous screen 
			pane.getChildren().add(circle);
			if (btfill.isSelected()) {
    
				circle.setFill(Color.BLACK);
			} else {
    
				circle.setFill(Color.WHITE);
			}
		});
		btrectangle.setOnAction(e -> {
    
			pane.getChildren().clear();
			pane.getChildren().add(rectangle);
			if (btfill.isSelected()) {
    
				rectangle.setFill(Color.BLACK);
			} else {
    
				rectangle.setFill(Color.WHITE);
			}
		});
		btellipse.setOnAction(e -> {
    
			pane.getChildren().clear();
			pane.getChildren().add(ellipse);
			if (btfill.isSelected()) {
    
				ellipse.setFill(Color.BLACK);
			} else {
    
				ellipse.setFill(Color.WHITE);
			}
		});
		btfill.setOnAction(e -> {
    
			if (btfill.isSelected()) {
    
				circle.setFill(Color.BLACK);
				rectangle.setFill(Color.BLACK);
				ellipse.setFill(Color.BLACK);
			} else {
    
				circle.setFill(Color.WHITE);
				rectangle.setFill(Color.WHITE);
				ellipse.setFill(Color.WHITE);
			}
		});
	}

	public static void main(String[] args) {
    
		launch(args);
	}
}
原网站

版权声明
本文为[Xiaomurong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221502448817.html