Die letzten 10 Beiträge |
eulerscheZahl |
Damit wird das Pendel immer neu berechnet und gezeichnet. Sonst würde sich das Bild nicht ändern. |
Marina17 |
Hallo,
was macht bei der GUI der Timer? und wozu brauche ich ihn? |
eulerscheZahl |
x0: ich wollte das Pendel irgendwie mittig haben und nicht am Rand, dass die Hälfte (negatives x) abgeschnitten ist.
Faktor 500: dein Pendel ist sehr kurz. Das würde sich vielleicht ein Pixel hin und herbewegen, wenn ich es nicht hochskalieren würde. |
Marina17 |
Hallo;
warum muss ich wenn ich x1 haben mag x mal 500 nehmen und dann noch plus x0?
int x1 = (int) (pendel.getX() * 500 + x0); |
eulerscheZahl |
So in etwa?
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
|
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class PENDEL
{
private double theta;
private double thetaPunkt;
private double thetaPunktPunkt;
private double m;
private double l;
private double g;
private double omega;
private double I;
private double tau;
private double x;
private double y;
private int xAlt;
private int yAlt;
private GUI h;
public static void main(String[] args) {
new PENDEL();
}
public PENDEL()
{
// initialize instance variables
theta = 0.785;
thetaPunkt = 0;
thetaPunktPunkt = 0;
m = 0.05;
l = 0.10;
g = 9.81;
I = m * l * l;
omega = Math.sqrt(m * g * l / I);
tau = 0.0001;
x = Math.sin(theta) * l;
int X = (int) x;
xAlt = X;
y = Math.cos(theta) * l;
int Y = (int) y;
yAlt = Y;
h = new GUI();
final Timer t = new Timer(1, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
PENDEL.this.pendelSchwingt();
}
});
t.start();
}
public double getX() {
return x;
}
public double getY() {
return y;
}
void pendelSchwingt()
{
h.zeichnen(this);
x = (int) xAlt;
y = (int) yAlt;
theta = thetaPunkt * tau + theta;
thetaPunkt = thetaPunktPunkt * tau + thetaPunkt;
thetaPunktPunkt = omega * omega * Math.sin(theta);
x = Math.sin(theta) * l;
System.out.println("x=" + x);
y = Math.cos(theta) * l;
System.out.println("y=" + y);
}
} |
|
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
|
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
public class GUI extends JFrame
{
public GUI()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Fenster");
this.setSize(1000, 1000);
this.setLocation(20, 20);
this.setLayout(new FlowLayout());
this.setVisible(true);
}
private PENDEL pendel;
public void zeichnen(PENDEL pendel)
{
this.pendel = pendel;
Graphics g = GUI.this.getGraphics();
GUI.this.paint(g);
}
public void paint(Graphics g)
{
int x0 = 200;
int y0 = 200;
int x1 = (int) (pendel.getX() * 500 + x0);
int y1 = (int) (-pendel.getY() * 500 + y0);
g.setColor(Color.white);
g.fillRect(-1, -1, 1000, 1000);
g.setColor(Color.black);
g.drawLine(x0, y0, x1, y1);
g.fillOval(x0 - 5, y0 - 5, 10, 10);
g.setColor(Color.blue);
g.fillOval(x1 - 5, y1 - 5, 10, 10);
}
} |
|
|
Marina17 |
Ich habe ganz vergessen die GUI Klasse auch noch mal herzuzeigen.
Hier ist die GUI:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GUI extends JFrame
{
private boolean shallPaint = false;
public static void main(String[] args) {
GUI g = new GUI();
}
public GUI()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Fenster");
this.setSize(1000, 1000);
this.setLocation(20, 20);
this.setLayout(new FlowLayout());
this.setVisible(true);
}
public void Zeichnen()
{
shallPaint = true;
Graphics g = GUI.this.getGraphics();
GUI.this.paint(g);
}
public void paint(Graphics g)
{
if (shallPaint)
{
g.setColor(Color.black);
g.drawLine(500, 500, 600, 600);
g.fillOval(495, 495, 10, 10);
g.setColor(Color.blue);
g.fillOval(595, 595, 10, 10);
g.drawLine(0,0,0,0);
}
}
} |
Marina17 |
Hallo,
ich habe versucht die Methode von der anderen Klasse aufzurufen, aber immer wenn ich es ausführe haut es mir eine null pointer exception raus.
hier ist ein Auszug aus der anderen Klasse:
public class PENDEL
{
private double theta;
private double thetaPunkt;
private double thetaPunktPunkt;
private double m;
private double l;
private double g;
private double omega;
private double I;
private double tau;
private double x;
private double y;
private int X;
private int Y;
private int xAlt;
private int yAlt;
private GUI h;
public PENDEL()
{
// initialise instance variables
theta = 0.785;
thetaPunkt = 0;
thetaPunktPunkt = 0;
m = 0.05;
l = 0.10;
g = 9.81;
I = m*l*l;
omega = Math.sqrt(m*g*l/I);
tau = 0.0001;
x = Math.sin(theta)*l;
int X = (int)x;
xAlt = X;
y = Math.cos(theta)*l;
int Y = (int)y;
yAlt = Y;
GUI h = new GUI();
}
void Pendelschwingt()
{
h.Zeichnen();
while(theta != 0.00)
{
x = (int) xAlt;
y = (int) yAlt;
theta = thetaPunkt*tau+theta;
thetaPunkt = thetaPunktPunkt*tau+thetaPunkt;
thetaPunktPunkt = omega*omega*Math.sin(theta);
x = Math.sin(theta)*l;
System.out.println("x="+x);
y = Math.cos(theta)*l;
System.out.println("y="+y);
x = (int) X;
y = (int) Y;
} |
eulerscheZahl |
Ja.
Mache die Methode public und rufe sie dann von der anderen Klasse aus auf. |
Marina17 |
Hallo,
jetzt habe ich es geschafft, dass ich eine Methode Zeichnen hab, die auf Aufruf funktioniert. Wie kann ich die Methode in eine andere Klasse einbinden? |
Marina17 |
Hallo,
was soll ich dann für die Variable g einsetzen? und geht das auch ohne Button ich habe es bereits so weit, dass es mir immer die x und die y Werte jede zehntel sekunde neu berechnet. |
Es sind weitere Beiträge zu diesem Thema vorhanden. Klicken Sie hier, um sich alle Beiträge anzusehen. |
|
|