package komplexe_zahlen;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Button;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Berechnungsprogramm extends Frame implements WindowListener, MouseListener, MouseMotionListener, KeyListener, ActionListener{
double a;
double b;
double c;
double d;
double e;
double f;
double g;
double h;
double n;
double Potenz;
String operationa;
String operationb;
String operationh;
String operationp;
Dimension bildschirmgroesse=new Dimension();
int breitef, hoehef;
int abstandO, abstandL;
Label hilfetext;
Label anzeige;
Label Klammer1;
Label i1;
Label Klammer2;
Label Klammer3;
Label i2;
Label Klammer4;
Label gleich;
Label resultat;
Label potenzentext;
Label Klammer5;
Label i3;
Label Klammer6;
Label gleichp;
Label resultatp;
Label radtext;
Label wurzel;
Label Klammer7;
Label cis;
Label Klammer8;
Label gleichr;
Label resultatr;
Label k1;
TextField real1;
TextField operation1;
TextField imag1;
TextField operation;
TextField real2;
TextField operation2;
TextField imag2;
TextField realp;
TextField operationpotenz;
TextField imagp;
TextField exponent;
TextField nrad;
TextField radiusr;
TextField winkelr;
TextField kwert;
Button knopfn;
Button knopfp;
Button knopfr;
Button hilfe;
Label drag;
Label knopftext;
Font schrift=new Font ("Arial", Font.BOLD,13);
Font schrift2=new Font ("Arial", Font.BOLD,18);
Font schriftrechnung=new Font("Arial", Font.BOLD,25);
public Berechnungsprogramm(){
super("Berechnungsprogramm für komplexe Zahlen");
addWindowListener(this);
addMouseMotionListener(this);
addMouseListener(this);
addKeyListener(this);
setLayout(null);
//label einfügen
hilfetext=new Label("hilfetext");
hilfetext.setSize(500,25);
hilfetext.setLocation(750,50);
hilfetext.setFont(schrift);
hilfetext.setText("Lesen Sie bitte zuerst die Hilfe durch!");
add(hilfetext);
anzeige=new Label("AnzeigeText");
anzeige.setSize(500,25);
anzeige.setLocation(10,30);
anzeige.setFont(schrift);
anzeige.setText("Geben Sie bitte die gewünschten komplexen Zahlen ein!");
add(anzeige);
knopftext=new Label();
knopftext.setSize(100,25);
knopftext.setLocation(10,200);
knopftext.setFont(schrift);
add(knopftext);
Klammer1=new Label("(");
Klammer1.setSize(10,30);
Klammer1.setLocation(10,105);
Klammer1.setFont(schriftrechnung);
Klammer1.setText("(");
add(Klammer1);
i1=new Label("i");
i1.setSize(10,30);
i1.setLocation(265,110);
i1.setFont(schriftrechnung);
i1.setText("i");
add(i1);
Klammer2=new Label(")");
Klammer2.setSize(10,30);
Klammer2.setLocation(275,105);
Klammer2.setFont(schriftrechnung);
Klammer2.setText(")");
add(Klammer2);
Klammer3=new Label("(");
Klammer3.setSize(10,30);
Klammer3.setLocation(325,105);
Klammer3.setFont(schriftrechnung);
Klammer3.setText("(");
add(Klammer3);
i2=new Label("i");
i2.setSize(10,30);
i2.setLocation(580,110);
i2.setFont(schriftrechnung);
i2.setText("i");
add(i2);
Klammer4=new Label(")");
Klammer4.setSize(10,30);
Klammer4.setLocation(590,105);
Klammer4.setFont(schriftrechnung);
Klammer4.setText(")");
add(Klammer4);
gleich=new Label("=");
gleich.setSize(50,30);
gleich.setLocation(600,107);
gleich.setFont(schriftrechnung);
gleich.setText("=");
add(gleich);
resultat=new Label("Anzeige text");
resultat.setSize(300,30);
resultat.setLocation(10,175);
resultat.setFont(schrift);
resultat.setText("");
add(resultat);
//potenzlabels
potenzentext=new Label("AnzeigeText");
potenzentext.setSize(300,30);
potenzentext.setLocation(10,275);
potenzentext.setFont(schrift);
potenzentext.setText("Geben Sie die gewünschte Potenz ein!");
add(potenzentext);
Klammer5=new Label("(");
Klammer5.setSize(10,30);
Klammer5.setLocation(10,335);
Klammer5.setFont(schriftrechnung);
Klammer5.setText("(");
add(Klammer5);
i3=new Label("i");
i3.setSize(10,30);
i3.setLocation(265,340);
i3.setFont(schriftrechnung);
i3.setText("i");
add(i3);
Klammer6=new Label(")");
Klammer6.setSize(10,30);
Klammer6.setLocation(275,335);
Klammer6.setFont(schriftrechnung);
Klammer6.setText(")");
add(Klammer6);
gleichp=new Label("=");
gleichp.setSize(50,30);
gleichp.setLocation(315,337);
gleichp.setFont(schriftrechnung);
gleichp.setText("=");
add(gleichp);
resultatp=new Label("Anzeige text");
resultatp.setSize(300,30);
resultatp.setLocation(10,400);
resultatp.setFont(schrift);
resultatp.setText("");
add(resultatp);
wurzel=new Label("√");
wurzel.setSize(20,30);
wurzel.setLocation(30,570);
wurzel.setFont(schriftrechnung);
wurzel.setText("√");
add(wurzel);
Klammer7=new Label("(");
Klammer7.setSize(10,30);
Klammer7.setLocation(45,567);
Klammer7.setFont(schriftrechnung);
Klammer7.setText("(");
add(Klammer7);
cis=new Label("cis");
cis.setSize(50,30);
cis.setLocation(170,570);
cis.setFont(schriftrechnung);
cis.setText("cis");
add(cis);
Klammer8=new Label(")");
Klammer8.setSize(10,30);
Klammer8.setLocation(315,567);
Klammer8.setFont(schriftrechnung);
Klammer8.setText(")");
add(Klammer8);
gleichr=new Label("=");
gleichr.setSize(20,30);
gleichr.setLocation(330,570);
gleichr.setFont(schriftrechnung);
gleichr.setText("=");
add(gleichr);
resultatr=new Label("");
resultatr.setSize(1000,30);
resultatr.setLocation(365,570);
resultatr.setFont(schrift);
resultatr.setText("");
add(resultatr);
radtext=new Label("");
radtext.setSize(800,30);
radtext.setLocation(10,500);
radtext.setFont(schrift);
radtext.setText("Bitte geben Sie die gewünschte Zahl in der Polarform ein um sie zu radizieren!");
add(radtext);
k1=new Label("k=");
k1.setSize(30,30);
k1.setLocation(150,605);
k1.setFont(schrift2);
k1.setText("k=");
add(k1);
//textfeld einfügen
real1=new TextField("");
real1.setSize(100,25);
real1.setLocation(25, 110);
real1.setFont(schrift);
add(real1);
operation1=new TextField("");
operation1.setSize(30,25);
operation1.setLocation(130, 110);
operation1.setFont(schrift);
add(operation1);
imag1=new TextField("");
imag1.setSize(100,25);
imag1.setLocation(165, 110);
imag1.setFont(schrift);
add(imag1);
operation=new TextField("");
operation.setSize(30,25);
operation.setLocation(290, 110);
operation.setFont(schrift);
add(operation);
real2=new TextField("");
real2.setSize(100,25);
real2.setLocation(340, 110);
real2.setFont(schrift);
add(real2);
operation2=new TextField("");
operation2.setSize(30,25);
operation2.setLocation(445, 110);
operation2.setFont(schrift);
add(operation2);
imag2=new TextField("");
imag2.setSize(100,25);
imag2.setLocation(480, 110);
imag2.setFont(schrift);
add(imag2);
//textfelder potenz
realp=new TextField("");
realp.setSize(100,25);
realp.setLocation(25, 340);
realp.setFont(schrift);
add(realp);
operationpotenz=new TextField("");
operationpotenz.setSize(30,25);
operationpotenz.setLocation(130, 340);
operationpotenz.setFont(schrift);
add(operationpotenz);
imagp=new TextField("");
imagp.setSize(100,25);
imagp.setLocation(165, 340);
imagp.setFont(schrift);
add(imagp);
exponent=new TextField("");
exponent.setSize(25,25);
exponent.setLocation(285, 320);
exponent.setFont(schrift);
add(exponent);
//texties rad
nrad=new TextField("");
nrad.setSize(25,25);
nrad.setLocation(10, 550);
nrad.setFont(schrift);
add(nrad);
radiusr=new TextField("");
radiusr.setSize(100,25);
radiusr.setLocation(60, 570);
radiusr.setFont(schrift);
add(radiusr);
winkelr=new TextField("");
winkelr.setSize(100,25);
winkelr.setLocation(210, 570);
winkelr.setFont(schrift);
add(winkelr);
kwert=new TextField("");
kwert.setSize(50,25);
kwert.setLocation(185, 605);
kwert.setFont(schrift);
add(kwert);
//knöpfe
knopfn=new Button();
knopfn.setSize(100,20);
knopfn.setLocation(10,150);
knopfn.setFont(schrift);
knopfn.setLabel("Berechnen");
knopfn.addActionListener(this);
knopfn.setActionCommand("grundoperationen");
add(knopfn);
knopfp=new Button();
knopfp.setSize(100,20);
knopfp.setLocation(10,380);
knopfp.setFont(schrift);
knopfp.setLabel("Berechnen");
knopfp.addActionListener(this);
knopfp.setActionCommand("potenz");
add(knopfp);
knopfr=new Button();
knopfr.setSize(100,20);
knopfr.setLocation(10,610);
knopfr.setFont(schrift);
knopfr.setLabel("Berechnen");
knopfr.addActionListener(this);
knopfr.setActionCommand("rad");
add(knopfr);
hilfe=new Button();
hilfe.setSize(100,20);
hilfe.setLocation(900,80);
hilfe.setFont(schrift);
hilfe.setLabel("Hilfe");
hilfe.addActionListener(this);
hilfe.setActionCommand("hilfee");
add(hilfe);
//fenster grösse und position festlegen
bildschirmgroesse=Toolkit.getDefaultToolkit().getScreenSize();
abstandO=(int)(bildschirmgroesse.height/10);
abstandL=(int)(bildschirmgroesse.width/10);
breitef=bildschirmgroesse.width-2*abstandL;
hoehef=bildschirmgroesse.height-2*abstandO;
setSize(breitef,hoehef);
setLocation(abstandL,abstandO);
setVisible(true);
}
void hilfefenster(){
int bildschirmgrösse;
int abstandO;
int abstandL;
int breitef;
int hoehef;
Font schriftta=new Font ("Arial",Font.PLAIN,13);
JTextArea ta = new JTextArea(" hilfedialog
//TextAreaErzeugung mit Text
ta.setEditable(false); //heißt, dass niemand in deinem Hilfefenster rumschreiben kann
ta.setLocation(100,10);
ta.setFont(schriftta);
JFrame hilfefenster=new JFrame();
hilfefenster.getContentPane().add(ta); //ta wird in Hilfefenster geaddet
bildschirmgroesse=Toolkit.getDefaultToolkit().getScreenSize();
abstandO=(int)(bildschirmgroesse.height/10);
abstandL=(int)(bildschirmgroesse.width/10);
breitef=bildschirmgroesse.width-4*abstandL;
hoehef=bildschirmgroesse.height-4*abstandO;
hilfefenster.setSize(breitef,hoehef);
hilfefenster.setLocation(abstandL,abstandO);
hilfefenster.setVisible(true);
}
void einlesen(){
a = Double.parseDouble(real1.getText());
operationa=operation1.getText();
b = Double.parseDouble(imag1.getText());
operationh = operation.getText();
c = Double.parseDouble(real2.getText());
operationb = operation2.getText();
d = Double.parseDouble(imag2.getText());
if(operationh.equals("+")){
addition();
}
if(operationh.equals("-")){
subtraktion();
}
if(operationh.equals("*")){
multiplikation();
}
if(operationh.equals("/")){
division();
}
}
void addition(){
double real;
double imag;
if(operationa.equals("-")){
b=b*(-1);
}
if(operationb.equals("-")){
d=d*(-1);
}
real = a + c;
imag = b + d;
if(imag>0){
resultat.setText(" = " + real + " + " + imag + "i");
}
else{
resultat.setText(" = " + real + " - " + Math.abs(imag) + "i");
}
}
void subtraktion(){
double real;
double imag;
if(operationa.equals("-")){
b=b*(-1);
}
if(operationb.equals("-")){
d=d*(-1);
}
real = a - c;
imag = b - d;
if(imag>0){
resultat.setText(" = " + real + " + " + imag + "i");
}
else{
resultat.setText(" = " + real + " - " + Math.abs(imag) + "i");
}
}
void multiplikation(){
double real;
double imag;
if(operationa.equals("-")){
b=b*(-1);
}
if(operationb.equals("-")){
d=d*(-1);
}
real = (a*c) - (b*d);
imag = (a*d) + (b*c);
if(imag>0){
resultat.setText(" = " + real + " + " + imag + "i");
}
else{
resultat.setText(" = " + real + " - " + Math.abs(imag) + "i");
}
}
void division(){
double real;
double imag;
double nennera;
double nennerb;
double nenner;
double zählerreal;
double zählerimag;
double test = c+d;
String test2 = String.valueOf(test);
if(test2.equals("0")){
resultat.setText("Die Division durch null ist nicht definiert");
}
if(operationa.equals("-")){
b=b*(-1);
}
if(operationb.equals("-")){
d=d*(-1);
}
zählerreal = (a*c) + (b*d);
if(d<0){
zählerimag = (b*c) + (a*Math.abs(d));
}
else{
zählerimag = (b*c) - (a*Math.abs(d));
}
nennera = c*c;
nennerb = d*d;
nenner = nennera + nennerb;
real = zählerreal/nenner;
imag = zählerimag/nenner;
if(imag>0){
resultat.setText(" = " + real + " + " + imag + "i");
}
else{
resultat.setText(" = " + real + " - " + Math.abs(imag) + "i");
}
}
void potenzieren(){
double real;
double imag;
e = Double.parseDouble(realp.getText());
f = Double.parseDouble(imagp.getText());
operationp = operationpotenz.getText();
Potenz = Double.parseDouble(exponent.getText());
if(operationp.equals("-")){
f=f*(-1);
}
double rquadrata = e*e;
double rquadratb = f*f;
double rquadrat = rquadrata + rquadratb;
double r = Math.sqrt(rquadrat);
double alpharad = Math.atan(f/e);
double alpha = alpharad*(180/Math.PI);
double rn = Math.pow(r, Potenz);
double alphan = Potenz*alpha;
/*double cos = Math.cos(alphan);
double cosgrad = cos*(180/Math.PI);
real = rn*cosgrad;
double sin = Math.sin(alphan);
double singrad = sin*(180/Math.PI);
imag=rn*singrad;
if(imag>0){
resultatp.setText(real + " + " + imag + "i");
}
else{
resultatp.setText(real + " - " + Math.abs(imag) + "i");
}
*/
resultatp.setText(" = " + rn + " * " + "cis" + alphan + "°");
}
void radizieren(){
double nr = Double.parseDouble(nrad.getText());
double radius = Double.parseDouble(radiusr.getText());
double winkel = Double.parseDouble(winkelr.getText());
double k = Double.parseDouble(kwert.getText());
radius=Math.pow(radius, (1/nr));
winkel=winkel/nr + k/nr*360;
if(k>=nr){
resultatr.setText("Der k-Wert muss kleiner sein als der Wert der nten Wurzel!");
}
resultatr.setText(radius + " cis " + winkel + "°");
}
public static void main(String[] args) {
Berechnungsprogramm prg=new Berechnungsprogramm();
}
//windowlistener
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
public void windowActivated(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
//mouselistener
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent arg0) { }
public void mouseExited(MouseEvent arg0) { }
public void mousePressed(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
//mousemotionlistener
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
//keylistener
public void keyPressed(KeyEvent arg0) {}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
public void actionPerformed(ActionEvent e ){
if ("grundoperationen".equals(e.getActionCommand())){
einlesen();
}
if("potenz".equals(e.getActionCommand())){
potenzieren();
}
if ("rad".equals(e.getActionCommand())){
radizieren();
}
if("hilfee".equals(e.getActionCommand())){
hilfefenster();
}
}
}