Hallo,
Ich habe hier ein Programm dass die Bahn (inkl. Wechselwirkungen) von 2 Planeten um die Sonne anzeigt. Um die Arbeit zu vereinfachen habe ich die Variabeln zuerst als Konstanten eingefügt (siehe im Code zBsp. m1 = 6000). Per Textfeld soll aber der Wert der ins Textfeld eingegeben wird als m1 gesetzt werden (oder per Button eben der Wert der auf dem Button steht...).
Wie verknüpfe ich den Button/Textfeld mit meinen Variabeln (also zBsp. m1) ?
Ich habe hier ein Programm dass die Bahn (inkl. Wechselwirkungen) von 2 Planeten um die Sonne anzeigt. Um die Arbeit zu vereinfachen habe ich die Variabeln zuerst als Konstanten eingefügt (siehe im Code zBsp. m1 = 6000). Per Textfeld soll aber der Wert der ins Textfeld eingegeben wird als m1 gesetzt werden (oder per Button eben der Wert der auf dem Button steht...).
Wie verknüpfe ich den Button/Textfeld mit meinen Variabeln (also zBsp. m1) ?
Java:
package Test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Success extends JPanel {
// the contents of the window
JButton eatMeButton, drinkmeButton;
Circle circ;
JTextField m1, m2, r1, r2, typeMeField, textfield1;
JLabel m1l, m2l, r1l, r2l, label1;
ActionListener al = new myButtonListener();
// ActionListener clear = new myButtonListener2();
final static Dimension BUTTON_SIZE = new Dimension(125,75);
final static Dimension WINDOW_SIZE = new Dimension(1024,768);
final static Dimension CIRC_SIZE = new Dimension(900,900);
final static Dimension FIELD_SIZE = new Dimension(150,95);
public Success() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS/2));
// sandwich the buttonBox between two layers of stretchy glue
Box buttonBox = Box.createHorizontalBox();
add(Box.createVerticalGlue());
add(buttonBox);
add(Box.createVerticalGlue());
eatMeButton = new JButton("exit me");
eatMeButton.addActionListener(al);
eatMeButton.setPreferredSize(BUTTON_SIZE);
drinkmeButton = new JButton("clear me");
//drinkmeButton.addActionListener(clear);
drinkmeButton.setPreferredSize(BUTTON_SIZE);
circ = new Circle();
circ.setPreferredSize(new Dimension(CIRC_SIZE.height, CIRC_SIZE.width));
label1 = new JLabel("typeme");
typeMeField = new JTextField(15);
typeMeField.setPreferredSize(FIELD_SIZE);
buttonBox.add(Box.createHorizontalGlue());
add(circ);
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(eatMeButton);
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(drinkmeButton);
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(label1);
buttonBox.add(typeMeField);
label1.setLabelFor(typeMeField);
}
// the painting
static class Circle extends JPanel {
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.red);
Graphics2D gg = (Graphics2D)g;
int m = 200000;
int m1 = 6000;
int m2 = 7500;
int b = 900;
int h = 900;
double gamma = 6.67;
double r = 15;
double r2 = 20;
double vx = 0;
double vx2 = 0;
double alpha = 0;
double alpha2 = 0;
double alpha3 = 0;
double alpha4 = 0;
double x = r;
double x2 = r2;
double y = 0;
double y2 = 0;
double mittelpunktx = b/2;
double mittelpunkty = h/2;
double vy = 0.0001 * Math.sqrt(1000 * gamma * m / r);
double vy2 = 0.0001 * Math.sqrt(1000* gamma * m / r2);
double tend = 500;
double t = 0;
double pi = 4*Math.atan(1);
double deltat = 0.01;
double result = 0;
while (t <= tend) {
t = t + deltat;
r = Math.sqrt(x * x + y * y);
r2 = Math.sqrt(x2 * x2 + y2 * y2);
double posx = mittelpunktx + x;
double posy = mittelpunkty + y;
double posx2 = mittelpunktx + x2;
double posy2 = mittelpunkty + y2;
double poswechselx = posx2 - posx;
double poswechsely = posy2 - posy;
double rwechselq = poswechselx * poswechselx + poswechsely * poswechsely;
double a = 0.00001 * gamma * ((m / r) / r);
double a2 = 0.00001 * gamma * ((m / r2) / r2);
double avon1auf2 = 0.00001 * gamma * (m2 / rwechselq);
double avon2auf1 = 0.00001 * gamma * (m1 / rwechselq);
alpha = winkel(x,y,alpha);
alpha2 = winkel(x2, y2, alpha2);
alpha3 = winkel(poswechselx, poswechsely, alpha3);
alpha4 = alpha3 + pi;
double ax = a * Math.cos(alpha);
double ay = a * Math.sin(alpha);
double ax2 = a2 * Math.cos(alpha2);
double ay2 = a2 * Math.sin(alpha2);
double ax3 = avon1auf2 * Math.cos(alpha3);
double ay3 = avon1auf2 * Math.sin(alpha3);
double ax4 = avon2auf1 * Math.cos(alpha4);
double ay4 = avon2auf1 * Math.sin(alpha4);
ax = ax + ax3;
ay = ay + ay3;
ax2 = ax2 + ax4;
ay2 = ay2 + ay4;
vx = vx - ax * deltat;
vy = vy - ay * deltat;
x = x + vx * deltat;
y = y + vy * deltat;
vx2 = vx2 - ax2 * deltat;
vy2 = vy2 - ay2 * deltat;
x2 = x2 + vx2 * deltat;
y2 = y2 + vy2 * deltat;
double finalx = skalierenx(posx);
double finaly = skaliereny(posy);
double finalx2 = skalierenx(posx2);
double finaly2 = skaliereny(posy2);
double finalmittelpunktx = skalierenx(mittelpunktx);
double finalmittelpunkty = skaliereny(mittelpunkty);
g.setColor(Color.blue);
g.drawLine((int)(finalx),(int)(finaly),(int)(finalx),(int)(finaly));
g.setColor(Color.red);
g.drawLine((int)(finalx2),(int)(finaly2),(int)(finalx2),(int)(finaly2));
g.drawLine((int)(finalmittelpunktx),(int)(finalmittelpunkty),(int)(finalmittelpunktx),
(int)(finalmittelpunkty));
// g.drawOval((int)(finalx),(int)(finaly),);
}
}
/*public void clearing(Graphics g) {
g.fillRect(0,0, (int)(b),(int)(h));
}*/
public double winkel(double xposition, double yposition, double result){
result = Math.atan(yposition / xposition);
double pi = 4*Math.atan(1);
if ((xposition < 0) && (yposition < 0)) {
result = result + pi;
}
if ((xposition < 0 ) && (yposition > 0)){
result = result + pi;
}
if ((xposition > 0) && (yposition <0)) {
result = result + 2*pi;
}
return result;
}
public double skalierenx(double posx){
double minPointX = 508.0;
double maxPointX = 366.594;
double screenX = (posx - minPointX) * getWidth() / (maxPointX - minPointX);
return screenX;
}
public double skaliereny(double posy){
double minPointY = 497.66;
double maxPointY = 337.468;
double screenY = (posy - minPointY) * getWidth() / (maxPointY - minPointY);
return screenY;
}
/*public double skalierenx2(double posx2){
double minPointX2 = 508.0;
double maxPointX2 = 366.594;
double screenX2 = (posx2 - minPointX2) * getWidth() / (maxPointX2 - minPointX2);
return screenX2;
}
public double skaliereny2(double posy2){
double minPointY2 = 497.66;
double maxPointY2 = 337.468;
double screenY2 = (posy2 - minPointY2) * getWidth() / (maxPointY2 - minPointY2);
return screenY2;
} */
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Success");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new Success();
frame.setContentPane(newContentPane);
frame.setPreferredSize(WINDOW_SIZE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
class myButtonListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
//int m1 = 1500;
System.exit(0);
}
}
/*class myButtonListener2 implements ActionListener{
public void actionPerformed(ActionEvent ae){
Circle(clearing);*/
//}
// }
}
Zuletzt bearbeitet von einem Moderator: