import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Vektor2d extends JFrame implements ActionListener
{
int a;
int b;
int c;
int[] zahla = new int[3];
int[] zahlb = new int[3];
int gcd;
JTextField eins;
JTextField zwei;
JButton skalar;
JButton plus;
JButton minus;
JButton gleich;
JButton clear;
JPanel panel;
JPanel text;
String antwort = new String();
public void seta(int x)
{
this.a = x;
}
public void setb(int y)
{
this.b = y;
}
public void setc(int z)
{
this.c = z;
}
public int geta()
{
return this.a;
}
public int getb()
{
return this.b;
}
public int getc()
{
return this.c;
}
public void add(int x, int y, int z, int n)
{
this.a = x*n + z*y;
this.b = y*n;
}
public void subtract(int x, int y, int z, int n)
{
this.a = x*n - z*y;
this.b = y*n;
}
public void skalar(int x, int y, int z, int n)
{
this.a = x*z;
this.b = y*n;
this.c = this.a + this.b;
}
public void clear(JTextField x, JTextField y)
{
x.setText("");
y.setText("");
}
public int greatestcommonDivisor(int x, int y)
{
while(x != 0.0)
{
int temp = y % x;
y = x;
x = temp;
}
return y;
}
public Vektor2d()
{
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
dispose();
System.exit(0);
}
});
menuFile.add(menuFileExit);
menuBar.add(menuFile);
Container content = getContentPane();
content.setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(new FlowLayout());
plus = new JButton("+");
plus.addActionListener(this);
//plus.setPreferredSize(new Dimension(20,20));
panel.add(plus);
minus = new JButton("-");
minus.addActionListener(this);
//minus.setPreferredSize(new Dimension(20,20));
panel.add(minus);
gleich = new JButton("=");
gleich.addActionListener(this);
//gleich.setPreferredSize(new Dimension(20,20));
panel.add(gleich);
clear = new JButton("C");
clear.addActionListener(this);
//clear.setPreferredSize(new Dimension(20,20));
panel.add(clear);
skalar = new JButton("skalar");
skalar.addActionListener(this);
panel.add(skalar);
content.add(panel, BorderLayout.NORTH);
text = new JPanel();
text.setLayout(new BorderLayout());
eins = new JTextField();
eins.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e33)
{
int keycode = e33.getKeyCode();
if(keycode == KeyEvent.VK_ENTER)
{
zwei.requestFocus();
}
}
});
text.add(eins, BorderLayout.NORTH);
zwei = new JTextField();
zwei.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e77)
{
int keycode = e77.getKeyCode();
if(keycode == KeyEvent.VK_ENTER)
{
eins.requestFocus();
}
}
});
text.add(zwei, BorderLayout.SOUTH);
content.add(text, BorderLayout.SOUTH);
this.a = 40;
this.b = 20;
}
public Vektor2d(int x, int y)
{
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
dispose();
System.exit(0);
}
});
menuFile.add(menuFileExit);
menuBar.add(menuFile);
Container content = getContentPane();
content.setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(new FlowLayout());
plus = new JButton("+");
plus.addActionListener(this);
//plus.setPreferredSize(new Dimension(20,20));
panel.add(plus);
minus = new JButton("-");
minus.addActionListener(this);
//minus.setPreferredSize(new Dimension(20,20));
panel.add(minus);
gleich = new JButton("=");
gleich.addActionListener(this);
//gleich.setPreferredSize(new Dimension(20,20));
panel.add(gleich);
clear = new JButton("C");
clear.addActionListener(this);
//clear.setPreferredSize(new Dimension(20,20));
panel.add(clear);
skalar = new JButton("skalar");
skalar.addActionListener(this);
panel.add(skalar);
content.add(panel, BorderLayout.NORTH);
text = new JPanel();
text.setLayout(new BorderLayout());
eins = new JTextField();
eins.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e33)
{
int keycode = e33.getKeyCode();
if(keycode == KeyEvent.VK_ENTER)
{
zwei.requestFocus();
}
}
});
text.add(eins, BorderLayout.NORTH);
zwei = new JTextField();
zwei.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e77)
{
int keycode = e77.getKeyCode();
if(keycode == KeyEvent.VK_ENTER)
{
eins.requestFocus();
}
}
});
text.add(zwei, BorderLayout.SOUTH);
content.add(text, BorderLayout.SOUTH);
this.a = x;
this.b = y;
}
public void actionPerformed(ActionEvent event)
{
Object quelle = event.getSource();
if(quelle == plus)
{
antwort = "+";
try
{
zahla[0] = Integer.parseInt(eins.getText());
zahlb[0] = Integer.parseInt(zwei.getText());
seta(zahla[0]);
setb(zahlb[0]);
}
catch(NumberFormatException e11)
{
System.out.println("Das war keine Zahl.\n Bitte geben sie erneut ein.");
zahla[0] = 0;
zahlb[0] = 0;
seta(zahla[0]);
setb(zahlb[0]);
clear(eins, zwei);
}
clear(eins, zwei);
}
else if(quelle == minus)
{
antwort = "-";
try
{
zahla[0] = Integer.parseInt(eins.getText());
zahlb[0] = Integer.parseInt(zwei.getText());
seta(zahla[0]);
setb(zahlb[0]);
}
catch(NumberFormatException e11)
{
System.out.println("Das war keine Zahl.\n Bitte geben sie erneut ein.");
zahla[0] = 0;
zahlb[0] = 0;
seta(zahla[0]);
setb(zahlb[0]);
clear(eins, zwei);
}
clear(eins, zwei);
}
else if(quelle == clear)
{
zahla[2] = 0;
zahlb[2] = 0;
zahla[1] = 0;
zahlb[1] = 0;
zahla[0] = 0;
zahlb[0] = 0;
seta(0);
setb(0);
clear(eins, zwei);
}
else if(quelle == skalar)
{
antwort = "s";
try
{
zahla[0] = Integer.parseInt(eins.getText());
zahlb[0] = Integer.parseInt(zwei.getText());
seta(zahla[0]);
setb(zahlb[0]);
}
catch(NumberFormatException e11)
{
System.out.println("Das war keine Zahl.\n Bitte geben sie erneut ein.");
zahla[0] = 0;
zahlb[0] = 0;
seta(zahla[0]);
setb(zahlb[0]);
clear(eins, zwei);
}
clear(eins,zwei);
}
if(quelle == gleich)
{
try
{
zahla[1] = Integer.parseInt(eins.getText());
zahlb[1] = Integer.parseInt(zwei.getText());
}
catch(NumberFormatException e11)
{
System.out.println("Das war keine Zahl.\n Bitte geben sie erneut ein.");
zahla[1] = 0;
zahlb[1] = 0;
seta(zahla[1]);
setb(zahlb[1]);
zahla[0] = 0;
zahlb[0] = 0;
seta(zahla[0]);
setb(zahlb[0]);
clear(eins, zwei);
}
if(antwort.equals("+"))
{
add(zahla[0], zahlb[0], zahla[1], zahlb[1]);
zahla[2] = geta();
zahlb[2] = getb();
gcd = greatestcommonDivisor(zahla[2], zahlb[2]);
zahla[2] = geta()/gcd;
zahlb[2] = getb()/gcd;
eins.setText(new Integer(zahla[2]).toString());
zwei.setText(new Integer(zahlb[2]).toString());
for(int i = 0; i <= 2; i++)
{
zahla[i] = 0;
zahlb[i] = 0;
}
seta(0);
setb(0);
}
if(antwort.equals("-"))
{
subtract(zahla[0], zahlb[0], zahla[1], zahlb[1]);
zahla[2] = geta();
zahlb[2] = getb();
gcd = greatestcommonDivisor(zahla[2], zahlb[2]);
zahla[2] = geta()/gcd;
zahlb[2] = getb()/gcd;
eins.setText(new Integer(zahla[2]).toString());
zwei.setText(new Integer(zahlb[2]).toString());
for(int i = 0; i <= 2; i++)
{
zahla[i] = 0;
zahlb[i] = 0;
}
seta(0);
setb(0);
}
if(antwort.equals("s"))
{
skalar(zahla[0], zahlb[0], zahla[1], zahlb[1]);
zahla[2] = getc();
eins.setText(new Integer(zahla[2]).toString());
zwei.setText("");
for(int i = 0; i <= 2; i++)
{
zahla[i] = 0;
zahlb[i] = 0;
}
seta(0);
setb(0);
setc(0);
}
}
}
public static void main(String[] args)
{
// Create application frame.
Vektor2d fridolin = new Vektor2d();
fridolin.setSize(300, 200);
fridolin.setLocation(100, 100);
fridolin.setTitle("Vektor2d");
fridolin.setVisible(true);
}
}