package floatingpoint;

//Titel:            Dialog-Fenster
//Version:
//Copyright:		Copyright (c) 2010
//Autor:
//Organisation:
//Beschreibung:


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

import java.io.*;

public class FloatingPoint extends JFrame {

  JButton BnSingle = new JButton("Single");
  JButton BnDouble = new JButton("Double");

    private JLabel lZahl= new JLabel("Zahl");
    private JLabel lErgebnis= new JLabel("Ergebnis");

    private JTextField TZahl= new JTextField("123,45");
    private JTextField TErgebnis= new JTextField("");


  public FloatingPoint() {
     setTitle("Single / Double / Extended Konvertierung");
     setSize(500,250);
     setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     setGUI();     
    }// FloatingPoint


    private void setGUI() {
      this.getContentPane().setLayout( new GridBagLayout() );
      lZahl.setFont( new Font("Arial", Font.BOLD,14)  );
      lErgebnis.setFont( new Font("Arial", Font.BOLD,14)  );
      TZahl.setFont( new Font("Arial", Font.BOLD,14)  );
      TErgebnis.setFont( new Font("Arial", Font.BOLD,14)  );

      BnSingle.setFont( new Font("Arial", Font.BOLD,14)  );
      BnDouble.setFont( new Font("Arial", Font.BOLD,14)  );
     

     BnSingle.setPreferredSize( BnDouble.getPreferredSize() );
    

      this.getContentPane().add(lZahl,
          new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
          GridBagConstraints.WEST, GridBagConstraints.NONE,
          new Insets(10,20,0,0), 0, 0));

      this.getContentPane().add(TZahl,
           new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(10,30,0,20), 00, 0));
      // -------------------------------

      this.getContentPane().add(lErgebnis,
          new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(20,20,0,0), 0, 0));

      this.getContentPane().add(TErgebnis,
           new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(20,30,0,20), 00, 0));
      // -------------------------------

      this.getContentPane().add(BnSingle,
          new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(20,20,0,20), 0, 0));

      this.getContentPane().add(BnDouble,
          new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(20,20,0,20), 0, 0));

      JLabel labelCopyright = new JLabel("Copyright (c) 2000,2010  by Michael Wilhelm");
      this.getContentPane().add(labelCopyright,
          new GridBagConstraints(0, 2, 3, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(40,20,0,0), 0, 0));
    
      BnSingle.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
          BnSingle_click();
        }
      });


      BnDouble.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
          BnDouble_click();
        }
      });     

  } // setGUI


  void BnSingle_click() {
    // check der Daten
    String sValue;
    float fValue;
    String sErg;    
    int i;
    int [] bits = new int[4];  // int statt byte, da byte Vorzeichenbehaftig
    try {
      sValue = TZahl.getText().trim();  // Leerstellen links rechts löschen
      sValue = sValue.replace(',','.');
      fValue = Float.parseFloat(sValue);
      FileOutputStream Fout = new FileOutputStream("file.sgl");
      DataOutputStream Dout = new DataOutputStream(Fout);
      Dout.writeFloat(fValue);
      Dout.close();
      Fout.close();

      FileInputStream Fin = new FileInputStream("file.sgl");
      DataInputStream Din = new DataInputStream(Fin);
      sErg = "";
      for (i=0; i<bits.length; i++) {
        bits[i] = Din.readByte();
        if (bits[i]<0) bits[i]+=256;
        sErg += Integer.toHexString(bits[i])+" ";
      }
      Din.close();
      Fin.close();
      TErgebnis.setText(sErg.toUpperCase() );
    }
    catch (IOException ee) {
           System.err.println("IOException: " + ee);
   }
    catch (NumberFormatException e) {
        // ErrorBox("Eingabe","Fehlerhafte Eingabe im Feld Alter");
        System.err.println("Fehlerhafte Eingabe");
        TZahl.requestFocus();  // erhaelt Fokus
    }
  }  // BnSingle_click


  void BnDouble_click() {
    // check der Daten
    String sValue;
    double fValue;
    String sErg;
    int i;
    int [] bits = new int[8];  // int statt byte, da byte Vorzeichenbehaftig
    try {
      sValue = TZahl.getText().trim();  // Leerstellen links rechts löschen
      sValue = sValue.replace(',','.');
      fValue = Double.parseDouble(sValue);
      FileOutputStream Fout = new FileOutputStream("file.sgl");
      DataOutputStream Dout = new DataOutputStream(Fout);
      Dout.writeDouble(fValue);
      Dout.close();
      Fout.close();

      FileInputStream Fin = new FileInputStream("file.sgl");
      DataInputStream Din = new DataInputStream(Fin);
      sErg = "";
      for (i=0; i<bits.length; i++) {
        bits[i] = Din.readByte();
        if (bits[i]<0) bits[i]+=256;
        sErg += Integer.toHexString(bits[i])+" ";
      }
      Din.close();
      Fin.close();
      TErgebnis.setText(sErg.toUpperCase() );
    }
    catch (IOException ee) {
           System.err.println("IOException: " + ee);
   }
    catch (NumberFormatException e) {
        // ErrorBox("Eingabe","Fehlerhafte Eingabe im Feld Alter");
        System.err.println("Fehlerhafte Eingabe");
        TZahl.requestFocus();  // erhaelt Fokus
    }
  } // BnDouble


  public static void main(String[] args) {
    FloatingPoint frame = new FloatingPoint();
    frame.setVisible(true);
 }

}  // FloatingPoint
