Skip to content

Commit

Permalink
Minor design changes and calculating when enter key is pressed is now…
Browse files Browse the repository at this point in the history
… working
  • Loading branch information
Marc Hein committed May 3, 2017
1 parent 2583fa6 commit 637a451
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 33 deletions.
9 changes: 6 additions & 3 deletions PPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class PPI {

private double x, y, z;
private double x, y, z, ppi;

private String width, height, size;

Expand Down Expand Up @@ -38,7 +38,7 @@ public PPI(String breite, String hoehe, String groesse) {
}
}

public double berechnen() throws IllegalArgumentException {
public double calc() throws IllegalArgumentException {

if (x <= 0) {
if (x < 1 || width == null) {
Expand Down Expand Up @@ -70,11 +70,14 @@ public double berechnen() throws IllegalArgumentException {
}
}

ppi = (double) Math.round(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) / z * 100000) / 100000;

if (debug) {
System.out.println("x = " + x + "\n" + "y = " + y + "\n" + "z = " + z);
System.out.println("PPI: " + ppi);
}

return (double) Math.round(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) / z * 100000) / 100000;
return ppi;
}

public static double removeAllNonNumber(String str, String type) {
Expand Down
3 changes: 2 additions & 1 deletion PPICalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public class PPICalculator extends Application {
public void start(Stage primaryStage) throws Exception {
try {
Pane root = FXMLLoader.load(getClass().getResource("ppi.fxml"));
Scene scene = new Scene(root, 519, 200);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("PPI Rechner");
primaryStage.getIcons().add(new Image("/ppi/ppi.jpg"));
primaryStage.resizableProperty().set(false);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
Expand Down
24 changes: 16 additions & 8 deletions PPIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TextField;

public class PPIController implements Initializable {
Expand All @@ -21,19 +23,25 @@ public void clearAction() {

public void calculateAction() {
try {
calc();
} catch (IllegalArgumentException e) {
ergebnis.setText(e.getMessage());
} catch (ArithmeticException e) {
ergebnis.setText(e.getMessage());
calcutePPI();
} catch (Exception e) {
ergebnis.setText(e.getMessage());
showError(e.getMessage());
}
}

public void calc() throws Exception {
public void showError(String error) {
Alert alert = new Alert(AlertType.ERROR);
String title = new String("Fehler");
String message = new String(error);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}

public void calcutePPI() throws Exception {
PPI display = new PPI(width.getText(), height.getText(), size.getText());
double ergebnisNumber = display.berechnen();
double ergebnisNumber = display.calc();
ergebnis.setText(Math.round(ergebnisNumber) + " (" + ergebnisNumber + ")");
}

Expand Down
42 changes: 21 additions & 21 deletions ppi.fxml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.FlowPane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="519.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ppi.PPIController">
<children>
<Button layoutX="433.0" layoutY="161.0" mnemonicParsing="false" onAction="#calculateAction" text="Berechnen" />
<Button layoutX="14.0" layoutY="161.0" mnemonicParsing="false" onAction="#clearAction" text="Zurücksetzen" />
<Label layoutX="14.0" layoutY="14.0" text="Breite des Displays (in px):" />
<Label layoutX="14.0" layoutY="50.0" text="Höhe des Displays (in px):" />
<Label layoutX="14.0" layoutY="86.0" text="Größe des Displays (in Zoll):" />
<Label layoutX="14.0" layoutY="122.0" text="Ergebnis der Berechnung (PPI):" />
<TextField fx:id="width" layoutX="195.0" layoutY="10.0" prefHeight="25.0" prefWidth="315.0" promptText="Breite" />
<TextField fx:id="height" layoutX="195.0" layoutY="46.0" prefHeight="25.0" prefWidth="315.0" promptText="Höhe" />
<TextField fx:id="size" layoutX="195.0" layoutY="82.0" prefHeight="25.0" prefWidth="315.0" promptText="Größe" />
<TextField fx:id="ergebnis" editable="false" layoutX="195.0" layoutY="118.0" prefHeight="25.0" prefWidth="315.0" promptText="Ergebnis" />
</children>
</Pane>
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="199.0" prefWidth="542.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ppi.PPIController">
<children>
<Button layoutX="446.0" layoutY="161.0" mnemonicParsing="false" onAction="#calculateAction" text="Berechnen" />
<Button layoutX="14.0" layoutY="161.0" mnemonicParsing="false" onAction="#clearAction" text="Zurücksetzen" />
<Label layoutX="14.0" layoutY="14.0" text="Breite des Displays (in px):" />
<Label layoutX="14.0" layoutY="50.0" text="Höhe des Displays (in px):" />
<Label layoutX="14.0" layoutY="86.0" text="Größe des Displays (in Zoll):" />
<Label layoutX="14.0" layoutY="122.0" text="Ergebnis der Berechnung (PPI):" />
<TextField fx:id="width" layoutX="215.0" layoutY="9.0" onAction="#calculateAction" prefHeight="25.0" prefWidth="315.0" promptText="Breite" />
<TextField fx:id="height" layoutX="215.0" layoutY="45.0" onAction="#calculateAction" prefHeight="25.0" prefWidth="315.0" promptText="Höhe" />
<TextField fx:id="size" layoutX="215.0" layoutY="81.0" onAction="#calculateAction" prefHeight="25.0" prefWidth="315.0" promptText="Größe" />
<TextField fx:id="ergebnis" editable="false" layoutX="215.0" layoutY="117.0" prefHeight="25.0" prefWidth="315.0" promptText="Ergebnis" />
</children>
</Pane>

0 comments on commit 637a451

Please sign in to comment.