package cap17.uomocan;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class UomoCannone extends JApplet
{ private static int DELAY = 50;
  private JLabel angLab =
    new JLabel("Angolo (gradi)", JLabel.TRAILING);
  private JTextField angolo = new JTextField("45");
  private JLabel velLab =
    new JLabel("Velocita (m/s)", JLabel.TRAILING);
  private JTextField velocita = new JTextField("50");
  private JButton spara = new JButton("Spara");
  private JPanel barra = new JPanel();
  private Pannello p = new Pannello();
  private Ascoltatore asc = new Ascoltatore();
  private Timer timer;
  public void init()
  { barra.setLayout(new GridLayout(1, 5));
    barra.add(angLab);
    barra.add(angolo);
    barra.add(velLab);
    barra.add(velocita);
    barra.add(spara);
    spara.addActionListener(asc);
    getContentPane().add(BorderLayout.PAGE_END, barra);
    getContentPane().add(p);
  }
  public void stop()
  { if (timer != null)
      timer.stop();
  }
  class Pannello extends JPanel
  { private int x;
    private int y;
    private String messaggio;
    private boolean primo = true;
    Pannello()
    { setBackground(Color.yellow);
      messaggio = "";
    }
    void setMessaggio(String m)
    { messaggio = m;
    }
    protected void paintComponent(Graphics g)
    { super.paintComponent(g);
      if (primo)
      { y = getHeight();
        primo = false;
      }
      g.drawString(messaggio, 20, 20);
      g.fillOval(x - 4, y - 4, 8, 8);
      g.drawString("Alfredo", x + 10, y - 10);
    }
    void aggiorna(double x, double y)
    { this.x = (int) x;
      this.y = getHeight() - (int) y;
    }
  }
  class Aggiornatore implements ActionListener
  { private final static double G = 9.8;
    private double vx0;
    private double vy0;
    private long time = System.currentTimeMillis();
    Aggiornatore(double a, double v)
    { vx0 = v * Math.cos(a);
      vy0 = v * Math.sin(a);
      p.setMessaggio(
        "Impatto a circa " + (int) ((2 * vx0 * vy0) / G) +
        " metri");
    }
    double getX()
    { long t = System.currentTimeMillis() - time;
      return (vx0 * t) / 1000.0;
    }
    double getY()
    { double x = getX();
      double y =
        ((vy0 * x) / vx0) -
        (0.5 * G * Math.pow(x / vx0, 2));
      return y;
    }
    public void actionPerformed(ActionEvent e)
    { double x = getX();
      double y = getY();
      p.aggiorna(x, y);
      p.repaint();
      if (y < 0)
      { timer.stop();
        spara.setEnabled(true);
      }
    }
  }
  class Ascoltatore implements ActionListener
  { public void actionPerformed(ActionEvent e)
    { spara.setEnabled(false);
      double angrad =
        Math.toRadians(
          Double.parseDouble(angolo.getText()));
      double vel = Double.parseDouble(velocita.getText());
      timer = new Timer(
          DELAY, new Aggiornatore(angrad, vel));
      timer.start();
    }
  }
}
