class GuiShow extends JPanel {
Timer timer = new Timer(1000, new MyTimerActionListener());
double x = 0;
double y = 0;
int xPoly[] = {100, 130, 100};
int yPoly[] = {510, 500, 490};
Polygon poly = new Polygon(xPoly, yPoly, 3);
JPanel panel = new JPanel();
BufferedImage img = null;
double R = 3.1415926 / 180.0;
double vx, vy, t = 0.0;
double dt = 0.003;
double eps = 0.01;
double c = 0.7;
double g = 9.81;
public void paintComponent(Graphics g) {
// try {
// img = ImageIO.read(new File("arrow-31.png"));
// } catch (IOException e) {
// System.out.println("Błąd odczytu pliku");
// }
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.draw(new Line2D.Double(x + 10, y + 500, x, y + 500));
// g2d.drawPolygon(poly);
// poly.translate(1,1);
timer.start();
try{
Thread.sleep(10);
}catch (InterruptedException e){
timer.stop();
}
}
double alfaVX(double v, double alfa){
double co = Math.cos(R * alfa);
vx = v * co;
return vx;
}
double alfaVY(double v, double alfa){
double si = Math.sin(R * alfa);
vy = v * si;
return vy;
}
public void simulate(double vx, double m, double vy) throws InterruptedException {
double r = c / m;
// System.out.println("czas (s) x (m) y (m) ");
int n = 0;
while (y > -eps) {
// if (!(n % 10 == 0)) {
// System.out.println(t + " " + x + " " + y);
// }
vx = vx - r * vx * dt;
vy = vy - g * dt - r * vy * dt;
x = x + vx * dt - 0.5 * r * vx * dt * dt;
y = y + vy * dt - 0.5 * g * dt * dt + 0.5 * r * vy * dt * dt;
t = t + dt;
n = n+1;
repaint();
System.out.println(vx + " " + vy);
}
}
class MyTimerActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
try {
simulate(alfaVX(30,30), 30, alfaVY(30,30));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Mam taki problem, że chciałbym rysować kreskę po każdym przejściu pętli while. Ale wyświetla się na ekranie tylko ostatni wynik jak już cała pętla się wykona. Co trzeba zmienić by to zaczęło działać jak należy?