import java.applet.Applet;
import java.awt.*;
public class Stoss extends Applet
implements Runnable
{
Thread timer;
Kugel k[];
Kugel temp;
Panel p1;
Panel p2;
Panel p3;
Panel p4;
Panel p5;
Panel panels[];
Button Btn1;
Button Btn2;
Button Btn3;
TextField v[];
TextField m[];
Checkbox Spur;
Checkbox Vektoren;
Vektor possave[];
Vektor vsave[];
double msave[];
Point P[][];
Image Bild;
Graphics G;
public static boolean inside(double x, double y, Vektor a, Vektor b)
{
return x >= a.x && y >= a.y && x <= b.x && y <= b.y;
}
public void start()
{
if(timer == null)
{
timer = new Thread(this);
timer.start();
}
}
public void stop()
{
if(timer != null)
{
timer.stop();
timer = null;
}
}
public boolean keyUp(Event evt, int key)
{
if(evt.modifiers != 2)
{
temp = null;
}
return true;
}
void Drehen(Vektor a, Vektor b)
{
double btg = b.Betrag();
a.move((-a.x * b.x) / btg + (a.y * b.y) / btg, (-a.x * b.y) / btg - (a.y * b.x) / btg);
}
void zerlege(Vektor v, Vektor d, Vektor vn, Vektor vt)
{
vn.move(Norm(d).x, Norm(d).y);
mult(vn, (v.x * d.x + v.y * d.y) / d.Betrag());
vt.move(v.x - vn.x, v.y - vn.y);
}
public String abschneiden(String s, int biswo)
{
try
{
return s.substring(0, biswo);
}
catch(StringIndexOutOfBoundsException ex)
{
return s;
}
}
public Stoss()
{
k = new Kugel[2];
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p5 = new Panel();
panels = new Panel[2];
Btn1 = new Button("Stop");
Btn2 = new Button("Neu");
Btn3 = new Button("Reset");
v = new TextField[2];
m = new TextField[2];
Spur = new Checkbox("Spur");
Vektoren = new Checkbox("Vektoren");
possave = new Vektor[2];
vsave = new Vektor[2];
msave = new double[2];
P = new Point[2][2];
}
void subtra(Vektor vn, Vektor vt, Vektor v)
{
v.move(vn.x - vt.x, vn.y - vt.y);
}
public boolean mouseDown(Event evt, int x, int y)
{
int i = 0;
do
{
Vektor a = new Vektor(k[i].pos.x, k[i].pos.y);
Vektor b = new Vektor(k[i].pos.x + 50D, k[i].pos.y + 50D);
if(evt.modifiers != 2 && inside(x, y, a, b))
{
temp = k[i];
}
a.move((k[i].pos.x + 25D + k[i].v.x * 20D) - 10D, (k[i].pos.y + 25D + k[i].v.x * 20D) - 10D);
b.move(k[i].pos.x + 25D + k[i].v.x * 20D + 10D, k[i].pos.y + 25D + k[i].v.x * 20D + 10D);
if(evt.modifiers == 2 && inside(x, y, a, b))
{
temp = k[i];
}
} while(++i < 2);
return true;
}
void aktualisieren()
{
int i = 0;
do
{
String vx = new String(String.valueOf(k[i].v.x));
String vy = new String(String.valueOf(k[i].v.y));
String M = new String(String.valueOf(k[i].m));
vx = abschneiden(vx, 5);
vy = abschneiden(vy, 5);
M = abschneiden(M, 5);
v[i].setText("v = (" + vx + ", " + vy + ")");
m[i].setText("m = " + M);
} while(++i < 2);
}
void addieren(Vektor vn, Vektor vt, Vektor v)
{
v.move(vn.x + vt.x, vn.y + vt.y);
}
public boolean mouseDrag(Event evt, int x, int y)
{
if(evt.modifiers != 2 && temp != null)
{
temp.pos.move(x - 25, y - 25);
saveEingaben();
setSpur();
repaint();
}
if(evt.modifiers == 2)
{
temp.v.move(-((temp.pos.x + 25D) - (double)x) / 20D, -((temp.pos.y + 25D) - (double)y) / 20D);
saveEingaben();
aktualisieren();
setSpur();
repaint();
}
return true;
}
public void setSpur()
{
int i = 0;
do
{
int j = 0;
do
{
P[i][j] = null;
} while(++j < 2);
} while(++i < 2);
P[0][0] = new Point((int)k[0].pos.x + 25, (int)k[0].pos.y + 25);
P[1][0] = new Point((int)k[1].pos.x + 25, (int)k[1].pos.y + 25);
}
Vektor Norm(Vektor d)
{
return new Vektor(d.x / d.Betrag(), d.y / d.Betrag());
}
public boolean mouseUp(Event evt, int x, int y)
{
temp = null;
return true;
}
void saveEingaben()
{
int i = 0;
do
{
msave[i] = k[i].m;
if(vsave[i] == null)
{
vsave[i] = new Vektor(k[i].v.x, k[i].v.y);
} else
{
vsave[i].move(k[i].v.x, k[i].v.y);
}
if(possave[i] == null)
{
possave[i] = new Vektor(k[i].pos.x, k[i].pos.y);
} else
{
possave[i].move(k[i].pos.x, k[i].pos.y);
}
} while(++i < 2);
}
public synchronized void run()
{
do
{
try
{
int i = 0;
do
{
k[i].pos.move(k[i].pos.x + k[i].v.x, k[i].pos.y + k[i].v.y);
} while(++i < 2);
Vektor d = new Vektor(k[0].pos.x - k[1].pos.x, k[0].pos.y - k[1].pos.y);
if(d.Betrag() <= 50D)
{
Vektor vn[] = new Vektor[2];
Vektor vt[] = new Vektor[2];
i = 0;
do
{
P[i][1] = new Point((int)k[i].pos.x + 25, (int)k[i].pos.y + 25);
vn[i] = new Vektor(0.0D, 0.0D);
vt[i] = new Vektor(0.0D, 0.0D);
zerlege(k[i].v, d, vn[i], vt[i]);
} while(++i < 2);
Vektor vs = new Vektor(k[0].m * vn[0].x + k[1].m * vn[1].x, k[0].m * vn[0].y + k[1].m * vn[1].y);
mult(vs, 2D / (k[0].m + k[1].m));
i = 0;
do
{
subtra(vs, vn[i], vn[i]);
} while(++i < 2);
i = 0;
do
{
addieren(vn[i], vt[i], k[i].v);
} while(++i < 2);
aktualisieren();
}
repaint();
Thread.sleep(2L);
}
catch(InterruptedException e)
{
stop();
}
} while(true);
}
public boolean action(Event evt, Object was)
{
if(evt.target == Btn1)
{
if(timer == null)
{
Btn1.setLabel("Stop");
start();
} else
{
Btn1.setLabel("Start");
stop();
}
aktualisieren();
}
if(evt.target == Btn2)
{
k[0].pos.move(size().width - 50 - 50, size().height / 2 - 50);
k[1].pos.move(50D, size().height / 2 - 50);
setSpur();
repaint();
}
if(evt.target == Btn3)
{
int i = 0;
do
{
k[i].v.move(vsave[i].x, vsave[i].y);
k[i].m = msave[i];
k[i].pos.move(possave[i].x, possave[i].y);
} while(++i < 2);
setSpur();
repaint();
aktualisieren();
}
if(evt.target instanceof TextField)
{
int i = 0;
do
{
k[i].v.move(v[i].getText());
String Zahl = m[i].getText().substring(m[i].getText().indexOf('=') + 1);
k[i].m = Double.valueOf(Zahl).doubleValue();
saveEingaben();
} while(++i < 2);
setSpur();
aktualisieren();
repaint();
}
if(evt.target instanceof Checkbox)
{
repaint();
}
return true;
}
public void init()
{
resize(500, 350);
int i = 0;
do
{
k[i] = new Kugel();
v[i] = new TextField("", 15);
m[i] = new TextField("", 7);
panels[i] = new Panel();
panels[i].setForeground(i != 0 ? Color.red : Color.blue);
panels[i].add(v[i]);
panels[i].add(m[i]);
} while(++i < 2);
aktualisieren();
k[0].pos.move(size().width - 50 - 50, size().height / 2 - 50);
k[1].pos.move(50D, size().height / 2 - 50);
setSpur();
p2.setLayout(new GridLayout(2, 1));
p2.add(panels[0]);
p2.add(panels[1]);
p3.setLayout(new GridLayout(2, 1));
p1.add(p2);
p4.add(Btn1);
p4.add(Btn2);
p4.add(Btn3);
p5.add(Spur);
p5.add(Vektoren);
p3.add(p4);
p3.add(p5);
p1.add(p3);
setLayout(new BorderLayout());
add("South", p1);
saveEingaben();
Bild = createImage(size().width, size().height);
G = Bild.getGraphics();
}
public void paint(Graphics g)
{
G.setColor(Color.yellow);
G.fillRect(0, 0, size().width, size().height);
G.setColor(Color.blue);
G.fillOval((int)k[0].pos.x, (int)k[0].pos.y, 50, 50);
G.setColor(Color.red);
G.fillOval((int)k[1].pos.x, (int)k[1].pos.y, 50, 50);
G.setColor(Color.black);
G.drawString("by R. Greim", size().width - 70, size().height - p2.size().height - 15);
double Ekin = (k[0].m * k[0].v.Betrag() * k[0].v.Betrag() + k[1].m * k[1].v.Betrag() * k[1].v.Betrag()) / 2D;
String E = String.valueOf(Ekin);
E = abschneiden(E, 5);
String px = new String(String.valueOf(k[0].v.x * k[0].m + k[1].v.x * k[1].m));
String py = new String(String.valueOf(k[0].v.y * k[0].m + k[1].v.y * k[1].m));
px = abschneiden(px, 5);
py = abschneiden(py, 5);
String s = new String("E = " + E + " p = (" + px + ", " + py + ")");
G.drawString(s, 10, size().height - p2.size().height - 15);
int i = 0;
do
{
G.setColor(i != 0 ? Color.red : Color.blue);
if(Spur.getState())
{
if(P[i][1] != null)
{
G.drawLine(P[i][0].x, P[i][0].y, P[i][1].x, P[i][1].y);
G.drawLine(P[i][1].x, P[i][1].y, (int)k[i].pos.x + 25, (int)k[i].pos.y + 25);
} else
{
G.drawLine(P[i][0].x, P[i][0].y, (int)k[i].pos.x + 25, (int)k[i].pos.y + 25);
}
}
if(Vektoren.getState())
{
G.setColor(Color.black);
Vektor a = new Vektor(k[i].pos.x + 25D, k[i].pos.y + 25D);
Vektor b = new Vektor(a.x + 20D * k[i].v.x, a.y + 20D * k[i].v.y);
G.drawLine((int)a.x, (int)a.y, (int)b.x, (int)b.y);
Vektor d = new Vektor(b.x - a.x, b.y - a.y);
if(d.Betrag() != 0.0D)
{
Vektor Spitze1 = new Vektor(4D + 0.050000000000000003D * d.Betrag(), 2D + 0.025000000000000001D * d.Betrag());
Vektor Spitze2 = new Vektor(4D + 0.050000000000000003D * d.Betrag(), -(2D + 0.025000000000000001D * d.Betrag()));
Drehen(Spitze1, d);
Drehen(Spitze2, d);
G.drawLine((int)b.x, (int)b.y, (int)(b.x + Spitze1.x), (int)(b.y + Spitze1.y));
G.drawLine((int)b.x, (int)b.y, (int)(b.x + Spitze2.x), (int)(b.y + Spitze2.y));
}
}
} while(++i < 2);
g.drawImage(Bild, 0, 0, this);
}
public void update(Graphics g)
{
paint(g);
}
void mult(Vektor a, double Skal)
{
a.move(a.x * Skal, a.y * Skal);
}
}