import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Component; import java.awt.Dialog; import java.awt.Event; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Insets; import java.awt.Label; import java.awt.Panel; import java.awt.TextArea; import java.awt.TextField; import net.sourceforge.matex.CalculateException; import net.sourceforge.matex.Parser; /* * Created on 20.12.2003 * * Copyright 2003, by Josef Lüth * */ /** * @author Josef Lüth * * @version 1.0 * * A software that can be used to calculate terms. * It uses Matex to calculate the terms. * */ public class MatexRechnerPCE extends Applet { TextArea results = new TextArea(); TextField term = new TextField(20); Object anchorpoint; /** Build a GUI */ public void init() { setLayout(new BorderLayout()); //add a panel where the user can enter his term Panel dialogue = new Panel(); add("North", dialogue); dialogue.setLayout(new FlowLayout(FlowLayout.CENTER)); dialogue.add(new Label("Term")); dialogue.add(term); dialogue.add(new Button("Berechnen")); dialogue.add(new Button("Reset")); dialogue.add(new Button("Hilfe")); dialogue.add(new Button("Info")); //add a panel where the calculated terms will show Panel result = new Panel(); add("Center", result); //add a textarea where the result will show result.add(results); //get the frame of the applet (need to open new frames) anchorpoint = getParent(); while (!(anchorpoint instanceof Frame)) anchorpoint = ((Component) anchorpoint).getParent(); } /** * Calculate the term and show it in the TextArea */ public void calculate(String term) { if (!term.equals("")) { //get the result of the term from Matex String result; try { result = Parser.parse(term).eval(); } catch (CalculateException e) { result = "Fehler bei der Berechnung: " + e.getMessage(); } //show to the user the result results.setText(results.getText() + term + " = " + result + "\n"); } } /** * calculate the term, if the user press Enter or click a button */ public boolean handleEvent(Event evt) { if (evt.id == Event.KEY_PRESS && evt.key == Event.ENTER) { calculate(term.getText()); term.setText(""); return true; } if (evt.target instanceof Button && evt.arg != null) { if (evt.arg.equals("Berechnen")) { calculate(term.getText()); term.setText(""); return true; } if (evt.arg.equals("Reset")) { term.setText(""); results.setText(""); return true; } if (evt.arg.equals("Info")) { // start info gue ShowInfo si = new ShowInfo((Frame) anchorpoint, "Info", true); si.resize(350, 150); si.show(); return true; } if (evt.arg.equals("Hilfe")) { // start info gue Help helpDialogue = new Help((Frame) anchorpoint, "Info", true); helpDialogue.resize(500, 500); helpDialogue.show(); return true; } } return false; } class ShowInfo extends Dialog { ShowInfo(Frame parent, String title, boolean modal) { super(parent, title, modal); setBackground(Color.white); setLayout(new GridLayout(5, 1)); add(new Label("Entwickelt von Josef Lüth")); add(new Label("Download unter www.MatexSoft.com")); add( new Label("Kopieren und weitergeben der Software ist genehmigt")); add(new Label("Basiert auf dem Matex Modul (www.MatexSoft.com)")); add(new Button("Schließen")); } public Insets insets() { return new Insets(30, 10, 10, 10); } public boolean handleEvent(Event evt) { // if the user press enter if (evt.key == Event.ENTER) { hide(); return true; } else //if the user wants to close the dialog if (evt.id == Event.WINDOW_DESTROY) { hide(); return true; } else if (evt.target instanceof Button && evt.arg != null) { if (evt.arg.equals("Schließen")) { hide(); return true; } } return false; } } class Help extends Dialog { Help(Frame parent, String title, boolean modal) { super(parent, title, modal); setBackground(Color.white); setLayout(new GridLayout(11, 1)); add(new Label("Ausdrücke für die Eingabe von Termen:")); add(new Label("Addieren +")); add(new Label("Subtrahieren -")); add(new Label("Multiplizieren *")); add(new Label("Dividieren /")); add(new Label("Potenzierem ^")); add(new Label("Radizieren(Wurzelziehen) ^(1/Radikand)")); add(new Label("Klammern ( )")); add(new Label("Pi !pi")); add(new Label("e !e")); add(new Button("Schließen")); } public Insets insets() { return new Insets(30, 10, 10, 10); } public boolean handleEvent(Event evt) { // if the user press enter if (evt.key == Event.ENTER) { hide(); return true; } else //if the user wants to close the dialog if (evt.id == Event.WINDOW_DESTROY) { hide(); return true; } else if (evt.target instanceof Button && evt.arg != null) { if (evt.arg.equals("Schließen")) { hide(); return true; } } return false; } } }