Skip to content

Commit

Permalink
Switched from AWT/Swing to Javafx
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Hein committed Oct 31, 2016
1 parent b8b6ed8 commit 2583fa6
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 213 deletions.
170 changes: 84 additions & 86 deletions PPI.java
Original file line number Diff line number Diff line change
@@ -1,93 +1,91 @@
package ppi;

import ppi.Number;

public class PPI {

private double x, y, z;
private String width, height, size;
public boolean debug = false;


public PPI(String breite, String hoehe, String groesse){

width = String.valueOf(removeAllNonNumber(String.valueOf(breite), "int"));
height = String.valueOf(removeAllNonNumber(String.valueOf(hoehe), "int"));
size = String.valueOf(removeAllNonNumber(String.valueOf(groesse), "double"));
private double x, y, z;

private String width, height, size;

public boolean debug = false;

public PPI(String breite, String hoehe, String groesse) {

width = String.valueOf(removeAllNonNumber(String.valueOf(breite), "int"));
height = String.valueOf(removeAllNonNumber(String.valueOf(hoehe), "int"));
size = String.valueOf(removeAllNonNumber(String.valueOf(groesse), "double"));

try {
x = Double.parseDouble(width);
} catch (NumberFormatException n) {
System.out.println(n.getMessage());
x = 0;
}
try {
y = Double.parseDouble(height);
if (y == 9000) {
debug = true;
y = 1080.0;
}
} catch (NumberFormatException n) {
System.out.println(n.getMessage());
y = 0;
}
try {
z = Double.parseDouble(size);
} catch (NumberFormatException n) {
System.out.println(n.getMessage());
z = 1;
}
}

public double berechnen() throws IllegalArgumentException {

if (x <= 0) {
if (x < 1 || width == null) {
throw new IllegalArgumentException("Die Breite kann nicht kleiner als 1 sein.");
} else {
x = x * (-1);
x = Double.parseDouble(x + "");
x = Math.abs(x);
}
}

if (y <= 0) {
if (y < 1 || height == null) {
throw new IllegalArgumentException("Die Höhe kann nicht kleiner als 1 sein.");
} else {
y = y * (-1);
y = Double.parseDouble(y + "");
y = Math.abs(y);
}
}

if (z <= 0) {
if (z <= 0) {
throw new ArithmeticException("Die Größe des Displays darf nicht 0 sein.");
} else {
z = z * (-1);
z = Double.parseDouble(z + "");
z = Math.abs(z);
}
}

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

try {
x = Double.parseDouble(width);
} catch (NumberFormatException n) {
System.out.println(n.getMessage());
x = 0;
}
try {
y = Double.parseDouble(height);
if(y == 9000) {
debug = true;
y = 1080.0;
}
} catch (NumberFormatException n) {
System.out.println(n.getMessage());
y = 0;
}
try {
z = Double.parseDouble(size);
} catch (NumberFormatException n){
System.out.println(n.getMessage());
z = 1;
}
}

public double berechnen() throws IllegalArgumentException {

if(x <= 0) {
if(x < 1 || width == null) {
throw new IllegalArgumentException("Die Breite kann nicht kleiner als 1 sein.");
} else {
x = x * (-1);
x = Double.parseDouble(x + "");
x = Math.abs(x);
}
}

if(y <= 0) {
if(y < 1 || height == null) {
throw new IllegalArgumentException("Die Höhe kann nicht kleiner als 1 sein.");
} else {
y = y * (-1);
y = Double.parseDouble(y + "");
y = Math.abs(y);
}
}

if(z <= 0) {
if(z <= 0) {
throw new ArithmeticException("Die Größe des Displays darf nicht 0 sein.");
} else {
z = z * (-1);
z = Double.parseDouble(z + "");
z = Math.abs(z);
}
}

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

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


public static double removeAllNonNumber(String str, String type) {
try {
if("int".equals(type)) {
return Double.parseDouble(Number.formatLikeInt(str));
} else {
return Double.parseDouble(Number.formatLikeDouble(str));
}
} catch (NumberFormatException n) {
return 0;
}
}
public static double removeAllNonNumber(String str, String type) {
try {
if ("int".equals(type)) {
return Double.parseDouble(Number.formatLikeInt(str));
} else {
return Double.parseDouble(Number.formatLikeDouble(str));
}
} catch (NumberFormatException n) {
return 0;
}
}
}
152 changes: 25 additions & 127 deletions PPICalculator.java
Original file line number Diff line number Diff line change
@@ -1,135 +1,33 @@
package ppi;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class PPICalculator extends Frame implements ActionListener, WindowListener, FocusListener {

private Label breite_l, hoehe_l, groesse_l, ergebnis_l;
private TextField breite_f, hoehe_f, groesse_f;
private TextField ergebnis_f;
private Button berechnen;
private Frame frame;
import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class PPICalculator extends Application {

public PPICalculator() {
frame = new Frame();
frame.setLayout(new FlowLayout());

breite_l = new Label("Breite des Displays (in px):");
frame.add(breite_l);

breite_f = new TextField("0", 15);
breite_f.setEditable(true);
breite_f.addFocusListener(this);
frame.add(breite_f);

hoehe_l = new Label("Höhe des Displays (in px):");
frame.add(hoehe_l);

hoehe_f = new TextField("0", 15);
hoehe_f.setEditable(true);
hoehe_f.addFocusListener(this);
frame.add(hoehe_f);

groesse_l = new Label("Größe des Displays (in Zoll):");
frame.add(groesse_l);

groesse_f = new TextField("0", 15);
groesse_f.setEditable(true);
groesse_f.addFocusListener(this);
frame.add(groesse_f);

ergebnis_l = new Label("Ergebnis der Rechnung (PPI):");
frame.add(ergebnis_l);

ergebnis_f = new TextField("Kein Ergebnis", 15);
ergebnis_f.setEditable(false);
frame.add(ergebnis_f);

berechnen = new Button("Berechnen");
frame.add(berechnen);

berechnen.addActionListener(this);

frame.addWindowListener(this);

frame.setTitle("PPI Rechner");
frame.setSize(350, 200);
Dimension size = new Dimension(350, 200);
frame.setMinimumSize(size);
frame.setMaximumSize(size);
frame.setPreferredSize(size);
frame.setResizable(false);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("ppi.jpg")));
frame.setVisible(true);

}

public void infoBox(String infoMessage) {
ergebnis_f.setText("Kein Ergebnis");
JOptionPane.showMessageDialog(frame, infoMessage, "Fehler",
JOptionPane.ERROR_MESSAGE);
}

public void focusGained(FocusEvent fe) {
// Get what textfield got focus
TextField t = (TextField) fe.getSource();
t.setBackground(Color.LIGHT_GRAY);
t.select(0, t.getText().length());
}

public void focusLost(FocusEvent fe) {
// Get what textfield lost focus
TextField t = (TextField) fe.getSource();
t.setBackground(Color.WHITE);
}

public void calc() throws Exception {
PPI display = new PPI(breite_f.getText(), hoehe_f.getText(), groesse_f.getText());
double ergebnis = display.berechnen();
ergebnis_f.setText(Math.round(ergebnis) + " (" + ergebnis + ")");
}

@Override
public void actionPerformed(ActionEvent evt) {
try {
calc();
} catch (IllegalArgumentException e) {
infoBox(e.getMessage());
} catch (ArithmeticException e) {
infoBox(e.getMessage());
} catch (Exception e) {
infoBox(e.getMessage());
public void start(Stage primaryStage) throws Exception {
try {
Pane root = FXMLLoader.load(getClass().getResource("ppi.fxml"));
Scene scene = new Scene(root, 519, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("PPI Rechner");
primaryStage.getIcons().add(new Image("/ppi/ppi.jpg"));
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}

}

@Override
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
System.exit(1);
}

@Override
public void windowActivated(WindowEvent arg0) { }

@Override
public void windowClosed(WindowEvent arg0) { }

@Override
public void windowDeactivated(WindowEvent arg0) { }

@Override
public void windowDeiconified(WindowEvent arg0) { }

@Override
public void windowIconified(WindowEvent arg0) { }

@Override
public void windowOpened(WindowEvent arg0) { }


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

}
45 changes: 45 additions & 0 deletions PPIController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ppi;

import java.net.URL;
import java.util.ResourceBundle;

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

public class PPIController implements Initializable {

@FXML
private TextField width, height, size, ergebnis;

public void clearAction() {
width.clear();
height.clear();
size.clear();
ergebnis.setText("Bitte Werte angeben.");
}

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

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

@Override
public void initialize(URL url, ResourceBundle rb) {
ergebnis.setText("Bitte Werte angeben.");
}

}
Loading

0 comments on commit 2583fa6

Please sign in to comment.