codice:
package funzioneseno;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static int FRAME_X =400;
public static int FRAME_Y =400;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Funzione seno");
frame.setSize(FRAME_X, FRAME_Y);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
SenoComponent seno = new SenoComponent();
seno.setPreferredSize(new Dimension(400,400));
panel.add(seno);
frame.add(panel);
frame.setVisible(true);
class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent event){
SenoComponent seno = new SenoComponent();
seno.aumentaFi();
}
}
ActionListener listener = new TimerListener();
final int DELAY =100;// millisecondi fra ogni evento
Timer t = new Timer(DELAY, listener);//ERRORE: costruttore sconosciuto
t.start();// ovviamente altro errore: metodo start sconosciuto
}
}
package funzioneseno;
import java.awt.Color;
import javax.swing.JComponent;
import java.awt.Graphics;
public class SenoComponent extends JComponent{
public double fi=0;
@Override
public void paintComponent(Graphics g){
double s=0, ampiezza=1, passo=0.001, omega=1;
int x1=100, y1=200, x2, y2, maxX=400, maxY=400, scala=2;
g.drawLine(0, 200, 400, 200);//ascisse
g.drawLine(100, 0, 100, 400);//ordinate
g.drawString("0", 90, 220);
g.drawLine(400, 200, 385, 195);// freccia delle
g.drawLine(400, 200, 385, 205);// ascisse
g.drawString("t", 380, 220);
g.drawLine(100, 0, 95, 15); // freccia delle
g.drawLine(100, 0, 105, 15); // ordinate
g.drawString("sin", 75, 25);
g.drawString("frequenza: "+omega, 5, 320);
g.drawString("ampiezza: "+ampiezza, 5, 340);
g.drawString("fi: "+fi, 5, 360);
for (double t = passo; t < 2*Math.PI; t=t+passo) {
s=ampiezza*Math.sin(t*omega+fi);
x2=(int) (maxX/4+t*maxX/(6*scala));
y2=(int) (maxY/2-s*maxY/(2*scala));
g.setColor(Color.blue);
g.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
}
}
public void aumentaFi(){
fi+=0.1;
repaint();
}
}