Adjust the size of the window dynamically in the JavaFX window to adjust the window size.

I have a task. I want to create four circles with four triangles inside, which is similar to a fan. I create a clock. Each time I run the program, I will randomly change its time and create an executioner image I've finished all these numbers, but the last step is to let all the numbers I create resize dynamically when the window is resized I don't know how to complete this dynamic resizing, but I believe it involves binding. I really don't understand how to implement it I would appreciate it if someone could help me This is my code as follows@ H_ 502_ 7@

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.geometry.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import java.util.*;
import javafx.scene.text.*;
import javafx.scene.control.*;
import java.lang.*;
import javafx.beans.binding.*;

public class test2 extends Application{

   @Override
   public void start(Stage primoStage){

      GridPane gridCircles = new GridPane();
      gridCircles.setPadding(new Insets(10));
      gridCircles.setHgap(5);
      gridCircles.setVgap(5);
      CirclePane cPane1 = new CirclePane();
      CirclePane cPane2 = new CirclePane();
      CirclePane cPane3 = new CirclePane();
      CirclePane cPane4 = new CirclePane();

      gridCircles.add(cPane1,0);
      gridCircles.add(cPane2,1,0);
      gridCircles.add(cPane3,1);
      gridCircles.add(cPane4,1);

      GridPane gridClock = new GridPane();
      gridClock.setPadding(new Insets(10));
      gridClock.setHgap(5);
      gridClock.setVgap(5);
      ClockPane clock = new ClockPane();
      String timeString = clock.getHour() + ":" + clock.getMinute() + ":" + clock.getSecond() + clock.getMeridiem();
      Label randomTime = new Label(timeString);
      gridClock.add(clock,0);
      GridPane.setHalignment(clock,HPos.CENTER);
      GridPane.setValignment(clock,VPos.CENTER);
      gridClock.add(randomTime,1);
      GridPane.setHalignment(randomTime,HPos.CENTER);
      GridPane.setValignment(randomTime,VPos.CENTER);

      GridPane gridHangMan = new GridPane();
      gridHangMan.setPadding(new Insets(10));
      gridHangMan.setHgap(5);
      gridHangMan.setVgap(5);
      hangingManPane hangMan = new hangingManPane();
      gridHangMan.add(hangMan,0);
      gridHangMan.setHalignment(hangMan,HPos.CENTER);
      gridHangMan.setValignment(hangMan,VPos.CENTER);

      GridPane gridPane = new GridPane();
      gridPane.setPadding(new Insets(10));
      gridPane.setHgap(5);
      gridPane.setVgap(5);
      gridPane.add(gridCircles,0);
      gridPane.add(gridClock,0);
      gridPane.add(gridHangMan,2,0);

      Scene scene = new Scene(gridPane,1500,500);
      primoStage.setTitle("Assignment 6 Test");
      primoStage.setScene(scene);
      primoStage.show();
   }

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

class CirclePane extends Pane {
  private Circle circle = new Circle(100,100,100);
  private Arc arc1 = new Arc(100,85,30,30);
  private Arc arc2 = new Arc(100,120,30);
  private Arc arc3 = new Arc(100,210,30);
  private Arc arc4 = new Arc(100,300,30);

   public CirclePane(){
      circle.setstroke(Color.BLACK);
      circle.setFill(Color.WHITE);
      arc1.setFill(Color.BLACK);
      arc1.setType(ArcType.ROUND);
      arc2.setFill(Color.BLACK);
      arc2.setType(ArcType.ROUND);
      arc3.setFill(Color.BLACK);
      arc3.setType(ArcType.ROUND);
      arc4.setFill(Color.BLACK);
      arc4.setType(ArcType.ROUND);
      getChildren().addAll(circle,arc1,arc2,arc3,arc4);
   }
}

class ClockPane extends Pane {
  private int hour;
  private int minute;
  private int second;
  private String meridiem;
  private double w = 400;
  private double h = 400;
  Random rand = new Random();

  public ClockPane(){
    setRandomTime();
  }

  public ClockPane(int hour,int minute,int second){
    this.hour = hour;
    this.minute = minute;
    this.second = second;
    paintClock();
  }

  public String getHour() {
    if (this.hour < 10){
      return "0"+hour;
    }
    return String.valueOf(hour);
  }

  public String getMinute() {
    if (this.minute <10){
      return "0"+minute;
    }
    return String.valueOf(minute);
  }

  public String getSecond() {
    if (this.second < 10){
      return "0"+second;
    }
    return String.valueOf(second);
  }

  public void setMinute(int minute) {
    this.minute = minute;
    paintClock();
  }

  public void setSecond(int second) {
    this.second = second;
  }

  public void setHour(int hour) {
    this.hour = hour;
  }

  public double getW() {
    return w;
  }

  public void setW(double w) {
    this.w = w;
    paintClock();
  }

  public double getH() {
    return h;
  }

  public void setH(double h) {
    this.h = h;
    paintClock();
  }

  public String getMeridiem() {
    if (Integer.valueOf(this.meridiem) > 12){
      return "pm";
    }
    return "am";
  }

  public void setMeridiem(String meridiem) {
    this.meridiem = meridiem;
  }

  public void setRandomTime() {
    Calendar calendar = new GregorianCalendar();

    this.hour = rand.nextInt(12 - 0 + 1) + 0;
    this.minute = rand.nextInt(60 - 0 + 1) + 0;
    this.second = rand.nextInt(60 - 0 + 1) + 0;
    this.meridiem = String.valueOf(rand.nextInt(24 - 0 +1) + 0);

    paintClock();
  }

  protected void paintClock() {
    double clockRadius = Math.min(w,h) * 0.8 * 0.5;
    double centerX = w/2;
    double centerY = h/2;

    Circle circle = new Circle(centerX,centerY,clockRadius);
    circle.setFill(Color.WHITE);
    circle.setstroke(Color.BLACK);
    Text t1 = new Text(centerX - 5,centerY - clockRadius + 14,"12");
    Text t2 = new Text(centerX - clockRadius + 3,centerY + 5,"9");
    Text t3 = new Text(centerX + clockRadius - 10,centerY + 3,"3");
    Text t4 = new Text(centerX - 3,centerY + clockRadius - 3,"6");

    double sLength = clockRadius * 0.8;
    double secondX = centerX + sLength * Math.sin(second * (2 * Math.PI / 60));
    double secondY = centerY - sLength * Math.cos(second * (2 * Math.PI / 60));
    Line sLine = new Line(centerX,secondX,secondY);
    sLine.setstroke(Color.RED);

    double mLength = clockRadius * 0.65;
    double xMinute = centerX + mLength * Math.sin(minute * (2 * Math.PI / 60));
    double minuteY = centerY - mLength * Math.cos(minute * (2 * Math.PI / 60));
    Line mLine = new Line(centerX,xMinute,minuteY);
    mLine.setstroke(Color.BLUE);

    double hLength = clockRadius * 0.5;
    double hourX = centerX + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
    double hourY = centerY - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
    Line hLine = new Line(centerX,hourX,hourY);
    hLine.setstroke(Color.GREEN);

    getChildren().clear();
    getChildren().addAll(circle,t1,t2,t3,t4,sLine,mLine,hLine);
  }
}

class hangingManPane extends Pane {
  private Arc arc1 = new Arc(100,450,45,90);
  private Line line1 = new Line(100,50,350);
  private Line line2 = new Line(100,50);
  private Line line3 = new Line(300,100);
  private Circle circle = new Circle(300,130,30);
  private Line line4 = new Line(285,160,225,250);
  private Line line5 = new Line(315,375,250);
  private Line line6 = new Line(300,280);
  private Line line7 = new Line(300,280,250,360);
  private Line line8 = new Line(300,350,360);

  public hangingManPane() {
    arc1.setFill(Color.TRANSPARENT);
    arc1.setType(ArcType.OPEN);
    arc1.setstroke(Color.BLACK);
    arc1.setstrokeWidth(5);
    line1.setstroke(Color.BLACK);
    line1.setstrokeWidth(5);
    line2.setstroke(Color.BLACK);
    line2.setstrokeWidth(5);
    line3.setstroke(Color.BLACK);
    line3.setstrokeWidth(5);
    circle.setstroke(Color.BLACK);
    circle.setFill(Color.TRANSPARENT);
    circle.setstrokeWidth(5);
    line4.setstroke(Color.BLACK);
    line4.setstrokeWidth(5);
    line5.setstroke(Color.BLACK);
    line5.setstrokeWidth(5);
    line6.setstroke(Color.BLACK);
    line6.setstrokeWidth(5);
    line7.setstroke(Color.BLACK);
    line7.setstrokeWidth(5);
    line8.setstroke(Color.BLACK);
    line8.setstrokeWidth(5);
    getChildren().addAll(arc1,line1,line2,line3,circle,line4,line5,line6,line7,line8);
  }
}

Solution

You can simply scale the gridpane to the appropriate size Since you may want to keep the initial aspect ratio, you can wrap it in stackpane to align the content correctly after zooming: @ h_ 502_ 7@

// keep gridPane at original size
gridPane.setMinSize(1500,500);
gridPane.setMaxSize(1500,500);

StackPane root = new StackPane(gridPane);
// root.setAlignment(Pos.TOP_LEFT);

// use gridPane size to determine the factor to scale by
NumberBinding maxScale = Bindings.min(root.widthproperty().divide(1500),root.heightproperty().divide(500));
gridPane.scaleXproperty().bind(maxScale);
gridPane.scaleYproperty().bind(maxScale);

Scene scene = new Scene(root,500);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>