1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class HyperFocale {
public static void main(String[] args) {
JFrame frame = new JFrame("Hyperfocale");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
Insets insets = new Insets(3,3,3,3);
GridBagConstraints constraintsLabel = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.NONE, insets, 0, 0);
GridBagConstraints constraintsField = new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0);
String[] labels = {"Focale : ", "Ouverture : ", "Capteur : ", "Hyperfocale : "};
for(String label : labels) {
panel.add(new JLabel(label), constraintsLabel);
constraintsLabel.gridy++;
}
JTextField focaleField = new JTextField();
panel.add(focaleField, constraintsField);
constraintsField.gridy++;
JTextField ouvertureField = new JTextField();
panel.add(ouvertureField, constraintsField);
constraintsField.gridy++;
Capteur[] capteurs = {new Capteur("Full Frame",0.030), new Capteur("Nikon APS-C",0.020), new Capteur("Canon APS-C",0.019)};
JComboBox<Capteur> capteurField = new JComboBox<>(capteurs);
panel.add(capteurField, constraintsField);
constraintsField.gridy++;
JLabel resultatLabel = new JLabel("");
panel.add(resultatLabel, constraintsField);
constraintsField.gridy++;
DocumentListener documentListener = new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
update(focaleField,ouvertureField,capteurField,resultatLabel);
}
@Override
public void insertUpdate(DocumentEvent e) {
update(focaleField,ouvertureField,capteurField,resultatLabel);
}
@Override
public void changedUpdate(DocumentEvent e) {
update(focaleField,ouvertureField,capteurField,resultatLabel);
}
};
focaleField.getDocument().addDocumentListener(documentListener);
ouvertureField.getDocument().addDocumentListener(documentListener);
capteurField.addActionListener(e->update(focaleField,ouvertureField,capteurField,resultatLabel));
resultatLabel.setText("Saisissez la focale...");
frame.add(panel);
frame.pack();
frame.setSize(600,frame.getHeight());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void update(JTextField focaleField, JTextField ouvertureField,
JComboBox<Capteur> capteurField, JLabel resultatLabel) {
if ( isEmpty(focaleField) ) {
resultatLabel.setText("Saisissez la focale...");
}
else if ( isKo(focaleField) ) {
resultatLabel.setText("La focale doit être un nombre...");
}
else if ( isEmpty(ouvertureField) ) {
resultatLabel.setText("Saisissez l'ouverture...");
}
else if ( isKo(ouvertureField) ) {
resultatLabel.setText("L'ouverture doit être un nombre...");
}
else {
double focale = getValue(focaleField);
double ouverture = getValue(ouvertureField);
double capteur = getValue(capteurField);
double ans = (focale * focale) / (ouverture * capteur) / 1000; //Calcul hyperfocale
resultatLabel.setText(String.format("%f0.0", ans));
}
}
private static double getValue(JComboBox<Capteur> capteurField) {
Capteur capteur = (Capteur)capteurField.getSelectedItem();
return capteur.capt;
}
private static double getValue(JTextField field) {
try{
Number number = NumberFormat.getNumberInstance().parse(field.getText());
return number.doubleValue();
}
catch(ParseException e) {
return 0;
}
}
static final String DECIMAL_PATTERN = "\\d+("+DecimalFormatSymbols.getInstance().getDecimalSeparator()+"\\d*)?";
private static boolean isKo(JTextField field) {
return !field.getText().matches(DECIMAL_PATTERN);
}
private static boolean isEmpty(JTextField field) {
return field.getText().trim().isEmpty();
}
private static class Capteur {
private final String name;
private final double capt;
public Capteur(String name, double capt) {
this.name=name;
this.capt=capt;
}
@Override
public String toString() {
return name;
}
}
} |
Partager