Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Razionalizzato layout schermata Eratostene
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio Nave committed Dec 11, 2018
1 parent fb72991 commit 24af7c4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 38 deletions.
99 changes: 77 additions & 22 deletions resources/Eratostene.fxml
Original file line number Diff line number Diff line change
@@ -1,33 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="615.0" prefWidth="792.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.nave.math.controller.EratosteneController">
<children>
<Label layoutX="265.0" layoutY="14.0" text="Crivello di Eratostene" textFill="RED">
<font>
<Font size="28.0" />
</font>
</Label>
<Button fx:id="setaccia" layoutX="255.0" layoutY="488.0" mnemonicParsing="false" onAction="#setaccia" prefHeight="91.0" prefWidth="190.0" text="SETACCIA" />
<Button fx:id="indietro" layoutX="534.0" layoutY="488.0" mnemonicParsing="false" onAction="#backToHome" prefHeight="91.0" prefWidth="190.0" text="INDIETRO" />
<ScrollPane fx:id="scrollpane" layoutX="237.0" layoutY="80.0" maxHeight="363.0" maxWidth="1000.0">
<content>
<GridPane fx:id="gridpane">
<ScrollPane fitToHeight="true" fitToWidth="true" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.nave.math.controller.EratosteneController">
<content>
<GridPane hgap="15.0" prefHeight="600.0" prefWidth="750.0" vgap="15.0">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" />
</columnConstraints>
<rowConstraints>
<RowConstraints valignment="CENTER" vgrow="SOMETIMES" />
<RowConstraints valignment="CENTER" vgrow="SOMETIMES" />
<RowConstraints valignment="CENTER" vgrow="SOMETIMES" />
<RowConstraints valignment="CENTER" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="45.0" prefWidth="280.0" text="Crivello di Eratostene" textFill="RED" GridPane.columnSpan="2147483647">
<font>
<Font size="30.0" />
</font>
</Label>
<VBox alignment="CENTER" GridPane.rowIndex="1">
<children>
<Label maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="165.0" text="Numero intero positivo">
<font>
<Font size="16.0" />
</font>
</Label>
<TextField fx:id="input" maxHeight="-Infinity" maxWidth="400.0" minHeight="-Infinity" minWidth="100.0" prefHeight="25.0" promptText="Numero intero positivo" text="50" />
</children>
</VBox>
<Button fx:id="setaccia" maxHeight="100.0" maxWidth="200.0" minHeight="39.0" minWidth="103.0" mnemonicParsing="false" onAction="#setaccia" text="SETACCIA" GridPane.rowIndex="3">
<font>
<Font size="18.0" />
</font>
</Button>
<Button fx:id="indietro" maxHeight="100.0" maxWidth="200.0" minHeight="39.0" minWidth="103.0" mnemonicParsing="false" onAction="#backToHome" text="INDIETRO" GridPane.columnIndex="1" GridPane.rowIndex="3">
<font>
<Font size="18.0" />
</font>
</Button>
<VBox alignment="CENTER" GridPane.rowIndex="2">
<children>
<Label maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="94.0" text="Numeri primi">
<font>
<Font size="16.0" />
</font>
</Label>
<TextArea fx:id="output" maxWidth="400.0" minHeight="100.0" minWidth="100.0" VBox.vgrow="ALWAYS" />
</children>
</VBox>
<ScrollPane fx:id="scrollpane" fitToHeight="true" fitToWidth="true" GridPane.columnIndex="1" GridPane.rowIndex="1" GridPane.rowSpan="2">
<content>
<GridPane fx:id="gridpane" prefWidth="500.0">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" percentWidth="10.0" />
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" percentWidth="10.0" />
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" percentWidth="10.0" />
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" percentWidth="10.0" />
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" percentWidth="10.0" />
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" percentWidth="10.0" />
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" percentWidth="10.0" />
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" percentWidth="10.0" />
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" percentWidth="10.0" />
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" percentWidth="10.0" />
</columnConstraints>
</GridPane>
</content>
</ScrollPane>
</children>
<padding>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
</padding>
</GridPane>
</content>
</ScrollPane>
<Label layoutX="25.0" layoutY="107.0" text="Numero intero positivo" />
<Label layoutX="64.0" layoutY="215.0" text="Numeri primi" />
<TextField fx:id="input" layoutX="25.0" layoutY="132.0" prefHeight="25.0" prefWidth="124.0" promptText="Numero intero positivo" text="50" />
<TextArea fx:id="output" layoutX="13.0" layoutY="244.0" prefHeight="200.0" prefWidth="200.0" />
</children>
</AnchorPane>
</content>
</ScrollPane>
40 changes: 24 additions & 16 deletions src/it/nave/math/controller/EratosteneController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;

public class EratosteneController {
private static final int LABEL_SIZE = 50;
private static final int MIN_LABEL_SIZE = 25;
private static final int COLUMNS_NUM = 10;
private static final String STYLE = "-fx-border-style: solid inside;" + "-fx-border-insets: 1;"
+ "-fx-border-radius: 5;" + "-fx-border-color: black;";
Expand Down Expand Up @@ -155,13 +157,13 @@ private void markAsPrime(int x) {
output.appendText(", ");
}
output.appendText(Integer.toString(x));
Label label = (Label) gridpane.getChildren().get(x - 1);
label.setStyle(STYLE_PRIME);
VBox vbox = (VBox) gridpane.getChildren().get(x - 1);
vbox.setStyle(STYLE_PRIME);
}

private void markAsDelete(int x) {
Label label = (Label) gridpane.getChildren().get(x - 1);
label.setStyle(STYLE_DELETE);
VBox vbox = (VBox) gridpane.getChildren().get(x - 1);
vbox.setStyle(STYLE_DELETE);
}

private Timeline populateGridPane() {
Expand All @@ -175,27 +177,18 @@ private Timeline populateGridPane() {
indietro.setDisable(true);
});
Tool.addKeyFrame(timeline, countFrame++, buildFrameDuration, e -> gridpane.getChildren().clear());
VBox useless = new VBox();
useless.setStyle(STYLE);
useless.setMinSize(LABEL_SIZE, LABEL_SIZE);
useless.setPrefSize(LABEL_SIZE, LABEL_SIZE);
useless.setMaxSize(LABEL_SIZE, LABEL_SIZE);
VBox useless = buildVBox(false, -1);
Tool.addKeyFrame(timeline, countFrame++, buildFrameDuration, e -> {
gridpane.add(useless, 0, 0);
});
int column = 1;
int row = 0;
for (long i = 2; i <= n; i++) {
Label label = new Label(Long.toString(i));
label.setMinSize(LABEL_SIZE, LABEL_SIZE);
label.setPrefSize(LABEL_SIZE, LABEL_SIZE);
label.setMaxSize(LABEL_SIZE, LABEL_SIZE);
label.setAlignment(Pos.CENTER);
label.setStyle(STYLE);
VBox vbox = buildVBox(true, i);
final int columnFinal = column;
final int rowFinal = row;
Tool.addKeyFrame(timeline, countFrame++, buildFrameDuration, e -> {
gridpane.add(label, columnFinal, rowFinal);
gridpane.add(vbox, columnFinal, rowFinal);
});
column++;
if (column % COLUMNS_NUM == 0) {
Expand All @@ -210,6 +203,21 @@ private Timeline populateGridPane() {
return timeline;
}

private VBox buildVBox(boolean withLabel, long i) {
VBox result = new VBox();
result.setStyle(STYLE);
result.setMinSize(MIN_LABEL_SIZE, MIN_LABEL_SIZE);
result.setPrefSize(Region.USE_COMPUTED_SIZE, LABEL_SIZE);
result.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
if (withLabel) {
Label label = new Label(Long.toString(i));
label.setAlignment(Pos.CENTER);
result.getChildren().add(label);
result.setAlignment(Pos.CENTER);
}
return result;
}

private void ensureVisible(int num) {
Node node = gridpane.getChildren().get(num - 1);
double width = scrollpane.getContent().getBoundsInLocal().getWidth();
Expand Down

0 comments on commit 24af7c4

Please sign in to comment.