import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.Line2D;
import java.awt.event.*;
//
// Lösen von Differentialgleichungen mittels Euler´sche Verfahren
class Differential extends JFrame {
public JTextField Feld1,Feld2,Feld3,Feld4,Feld5,Feld6,Feld7,Feld8,Feld9,Feld10,Feld11;
public JButton Berechnen,Einlesen,Speichern,Grafik,Schließen;
static double[] t;
static double[] y;
static double a0,ai,bi,lamda,h;
public int Schritte;
public static JFrame zeichnung;
public double untere,obere,y0;
//Eingabefenster
public Differential()
{
this.setTitle("EULER VERFAHREN VON SERKAN UND IVANA");
this.getContentPane().setLayout(null);
//Textfelder
Feld1= new JTextField("0");
Feld1.setBounds(270,40,120,23);
this.getContentPane().add(Feld1);
Feld2= new JTextField("26");
Feld2.setBounds(270,80,120,23);
this.getContentPane().add(Feld2);
Feld3= new JTextField("1");
Feld3.setBounds(270,120,120,23);
this.getContentPane().add(Feld3);
Feld4= new JTextField("100");
Feld4.setBounds(270,160,120,23);
this.getContentPane().add(Feld4);
Feld5= new JTextField("0");
Feld5.setBounds(150,230,30,23);
this.getContentPane().add(Feld5);
Feld6= new JTextField("20");
Feld6.setBounds(240,230,30,23);
this.getContentPane().add(Feld6);
Feld8 = new JTextField("1");
Feld8.setBounds(150,270,30,23);
this.getContentPane().add(Feld8);
Feld9= new JTextField("1");
Feld9.setBounds(240,270,30,23);
this.getContentPane().add(Feld9);
//Labels
JLabel label2 = new JLabel ("Untere Grenze:");
label2.setBounds(170,40,120,23);
this.getContentPane().add(label2);
JLabel label3 = new JLabel ("Obere Grenze:");
label3.setBounds(170,80,120,23);
this.getContentPane().add(label3);
JLabel label4 = new JLabel ("Y_0:");
label4.setBounds(225,120,120,23);
this.getContentPane().add(label4);
JLabel label5 = new JLabel ("Schritte:");
label5.setBounds(200,160,120,23);
this.getContentPane().add(label5);
JLabel label6 = new JLabel ("a(0)=");
label6.setBounds(120,230,30,23);
this.getContentPane().add(label6);
JLabel label8 = new JLabel ("a(i)=");
label8.setBounds(210,230,50,23);
this.getContentPane().add(label8);
JLabel label10 = new JLabel ("b(i)=");
label10.setBounds(120,270,30,23);
this.getContentPane().add(label10);
JLabel label11 = new JLabel ("lamda=");
label11.setBounds(190,270,50,23);
this.getContentPane().add(label11);
//Buttons
Berechnen = new JButton ("Berechnen");
Berechnen.setSize(130,22);
Berechnen.setLocation (10,40);
Berechnen.addActionListener(new KnopfAktion());
this.getContentPane().add(Berechnen);
Speichern = new JButton ("Speichern");
Speichern.setSize(130,22);
Speichern.setLocation (10,80);
Speichern.addActionListener(new KnopfAktion());
this.getContentPane().add(Speichern);
Einlesen =new JButton ("Einlesen");
Einlesen.setSize(130,22);
Einlesen.setLocation (10,120);
Einlesen.addActionListener(new KnopfAktion());
this.getContentPane().add(Einlesen);
Grafik =new JButton ("Grafik");
Grafik.setSize(130,20);
Grafik.setLocation (10,160);
Grafik.addActionListener(new KnopfAktion()); //Quelle,Ereignisse
this.getContentPane().add(Grafik);
Schließen =new JButton ("Schließen");
Schließen.setSize(130,20);
Schließen.setLocation(10,200);
Schließen.addActionListener(new KnopfAktion());
this.getContentPane().add(Schließen);
//Fenster schliessen
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public double f( double t, double y)
{
double ergebnis_b=0, ergebnis_a=0;
for(int i=1; i<=3; i=i+1)//b(t)
{
ergebnis_b=ergebnis_b+ai*Math.pow(t, i);
}
double bm=ergebnis_b*Math.exp(-lamda*t);
for(int i=1; i<=4; i=i+1)//a(t)
{
ergebnis_a=ergebnis_a+a0*Math.pow(t, i);
}
double am=ergebnis_a+bi*Math.cos(t);
return bm-am*y;
}
// Listener
class KnopfAktion implements ActionListener //OHR
{
public void actionPerformed(ActionEvent e)
{
//Rufe für alle Knöpfe die Funktion auf
berechnung(e);
}
}
public void berechnung (ActionEvent e)
{
if (e.getActionCommand()==Berechnen.getText())
{
//Strings werden in Double und Integer umgewandelt
a0= Double.parseDouble(Feld5.getText());
ai= Double.parseDouble(Feld6.getText());
bi= Double.parseDouble(Feld8.getText());
lamda= Double.parseDouble(Feld9.getText());
untere = Double.parseDouble (Feld1.getText());
obere = Double.parseDouble (Feld2.getText());
y0 = Double.parseDouble (Feld3.getText());
Schritte = Integer.parseInt (Feld4.getText());
// Berechnung und ausgabe
h = (obere-untere)/Schritte;
t= new double[Schritte+2];
y= new double[Schritte+2];
t[0]=untere;
y[0]=y0;
for (int i=0; i<=Schritte; i++)
{
t[i+1]=t[i] + h;
y[i+1]=y[i] + h*(f(t[i],y[i]));
System.out.println(" Punkt in X Achse: " + t[i]);
System.out.println(" Punkt in Y Achse: " + y[i]+" i="+i);
}
}
if(e.getActionCommand()==Schließen.getText())
{System.exit(0);}
if (e.getActionCommand()==Speichern.getText())
{
//Eingabewertespeichern
try
{ DataOutputStream dos = new DataOutputStream (
new FileOutputStream ("Einlesen.txt"));
dos.writeDouble (a0);
dos.writeDouble (ai);
dos.writeDouble (bi);
dos.writeDouble (lamda);
dos.writeDouble (untere);
dos.writeDouble (obere);
dos.writeDouble (y0);
dos.writeInt (Schritte);
dos.close( );
}
catch (IOException b)
{System.out.println("Fehler beim Erstellen der 2. Datei"); }
}
if (e.getActionCommand()==Einlesen.getText())
{
// Einlesen der Daten
try
{ DataInputStream dis = new DataInputStream (
new FileInputStream ("Einlesen.txt") );
Feld5.setText(String.valueOf(dis.readDouble()));
Feld6.setText(String.valueOf(dis.readDouble()));
Feld8.setText(String.valueOf(dis.readDouble()));
Feld9.setText(String.valueOf(dis.readDouble()));
Feld1.setText(String.valueOf(dis.readDouble()));
Feld2.setText(String.valueOf(dis.readDouble()));
Feld3.setText(String.valueOf(dis.readDouble()));
Feld4.setText(String.valueOf(dis.readInt()));
dis.close();
}
catch (IOException z)
{ System.out.println("Fehler beim Lesen der Datei"); }
}
// Grafik erzeugen
if (e.getActionCommand()==Grafik.getText())
{
zeichnung.setVisible(true);
}
}
class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D grafik=(Graphics2D)g;
for(int i=0; i<t.length-1; i++)
{grafik.draw(new Line2D.Double(t[i]*50, (-1*(y[i]))+800,t[i+1]*50, (-1*(y[i+1]))+800));
}
}
}
//Fenster Grafik erstellen
public Differential(Integer best)
{
this.setTitle("Grafik");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
this.setSize(screenSize.width, screenSize.height);
DrawPanel panel = new DrawPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static void main(String[] args)
{
JFrame fenster = new Differential();
fenster.setSize(450,350);
fenster.setLocation(400,200);
fenster.setVisible(true);
fenster.setResizable(false);
zeichnung = new Differential(2);
}
}