import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.io.*; import java.io.BufferedReader ; import java.io.InputStreamReader; import java.net.URL ; import java.util.*; public class Converter extends Applet { public Color background; public Font font; public void init() { background=new Color(Integer.parseInt("bbddFF", 16)); font= new Font("SansSerif", Font.BOLD, 11); setFont(font); setBackground(background); this.setLayout(new BorderLayout()); Body body= new Body(this); this.add("Center",body); Button showWindow = new Button("Open In Window"); showWindow.setFont(new Font("SansSerif", Font.BOLD, 10)); showWindow.setBackground(background); showWindow.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { FloatWindow fw=new FloatWindow(Converter.this); fw.pack(); fw.show(); } }); Panel p=new Panel(); p.add(showWindow); this.add("South",p); } } class FloatWindow extends Frame { private Button ok; public FloatWindow(Converter converter) { //this.super("Universal Currency Converter"); super("Universal Currency Converter"); this.setLayout(new BorderLayout()); this.setLocation(400,200); setFont(converter.font); setBackground(converter.background); ok=new Button("Close"); ok.setBackground(converter.background); Panel p=new Panel(); p.add(ok); this.add("South",p); Body body=new Body(converter); this.add("Center",body); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { FloatWindow.this.close(); } }); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { FloatWindow.this.close(); } }); } public void close() { this.dispose(); this.setVisible(false); } } class Body extends Panel implements ActionListener, ItemListener { public TextField in, out; public Choice currency, currency2; public Label rate; private FloatWindow fw; private Brain brain; private GridBagLayout grid; private GridBagConstraints gbc; public Body(Converter converter) { brain=new Brain(this,converter); setBackground(converter.background); setFont(converter.font); in = new TextField("0",10); in.addActionListener(this); out = new TextField("0",10); out.setEditable(false); out.setBackground(Color.white); rate= new Label(" "); rate.setAlignment(Label.LEFT); currency2 = new Choice(); currency2.addItemListener(this); currency = new Choice(); currency.addItemListener(this); for(int i=0;brain.Currencies[i]!=null;i++) { currency.addItem(brain.Currencies[i]); currency2.addItem(brain.Currencies[i]); } currency.select(2); grid = new GridBagLayout(); gbc = new GridBagConstraints(); this.setLayout(grid); Label date = new Label("Updated: "+brain.getDate()); gbc.anchor=GridBagConstraints.WEST; gbc.fill=GridBagConstraints.HORIZONTAL; gbc.insets=new Insets(5,5,2,2); myadd(in, grid, gbc, 0, 0, 2, 1); myadd(currency2, grid, gbc, 2, 0, 2, 1); myadd(new Label("="), grid, gbc, 4, 0, 1, 1); myadd(out, grid, gbc, 5, 0, 2, 1); myadd(currency, grid, gbc, 7, 0, 2, 1); myadd(new Label("Rate:"), grid, gbc, 0, 1, 1, 1); myadd(rate, grid, gbc, 1, 1, 2, 1); myadd(date, grid, gbc, 6, 1, 3, 1); brain.calculate(); } private void myadd(Component c, GridBagLayout grid, GridBagConstraints gbc, int x, int y, int w, int h) { gbc.gridx=x; gbc.gridy=y; gbc.gridwidth=w; gbc.gridheight=h; grid.setConstraints(c,gbc); this.add(c); } public void actionPerformed(ActionEvent e) { brain.calculate(); } public void itemStateChanged(ItemEvent e) { brain.calculate(); } } class Brain { public String Currencies[],s; private float Rates[]; private Body parent; private URL codebase; private String date; public Brain(Body parent,Converter converter) { this.parent=parent; this.codebase = converter.getCodeBase(); Currencies = new String[20]; Rates= new float[20]; sortData(readFile("rates.data")); } public String getDate() { return date; } public void calculate() { float exchange=0; float pounds=0; exchange = Rates[parent.currency2.getSelectedIndex()] / Rates[parent.currency.getSelectedIndex()]; String rateString = ""+exchange; parent.rate.setText(rateString); try { Float f=Float.valueOf(parent.in.getText()); pounds=f.floatValue(); } catch(NumberFormatException e) { parent.in.setText("0"); Float f=Float.valueOf(parent.in.getText()); pounds=f.floatValue(); } float exactcents = pounds*exchange; int cents = Math.round(exactcents); parent.out.setText(cents+""); } private String readFile(String f) { String s = new String("") ; BufferedReader fis = null ; try { URL u = new URL( codebase, f ) ; fis = new BufferedReader( new InputStreamReader( u.openStream()) ) ; } catch( Exception e ) { s = "readFile : " + f + "\r\n --> Exception : " + e ; return s; } String sS = new String("") ; while(true) { try { sS = fis.readLine() ; if (sS != null) s = s + "\r\n" + sS ; else break ; } catch( Exception e ) { break ; } } return s; } private void sortData(String all) { all=all.replace('\r','\t'); all=all.replace('\n',' '); StringTokenizer st = new StringTokenizer(all,"\t"); date=st.nextToken(); boolean eof=false; for(int i=0;((st.hasMoreElements())&&(!eof)&&(i