Witajcie, pracuje nad animacją wykresu i natknąłem na pewien problem. Rysuje Sobie wykres, statycznie wszystko wygląda OK, do wzoru fizycznego dodaję czas dzięki któremu fale poruszają się :
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
public class DrawChart extends DrawCoordinateSystem{
private static final long serialVersionUID = 8031763602916553302L;
private double[] xPoints = new double[800];
private double[] yPoints = new double[800];
private double maxY = 0;
private double maxX = 0;
private double t = 0;
public DrawChart(double time) {
this.setT(time);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Antyaliasing(g);
assignPointsX();
assignPointsY();
FactorTimesArray();
Shape shape = createShape();
g2d.draw(shape);
//gImage.fill(shape);
//g2d.drawImage(bImage, 0,0, null);
}
protected Shape createShape() {
GeneralPath gp = new GeneralPath(GeneralPath.WIND_EVEN_ODD,xPoints.length);
gp.moveTo(xPoints[0],getHeight()/2);
for (int i = 0; i < yPoints.length; i++) {
gp.lineTo(xPoints[i],getHeight()/2 - yPoints[i]);
}
return gp;
}
protected void assignPointsX() {
for (int i = 0; i < xPoints.length; i++) {
xPoints[i] = getWidth()/22 +((i-1)* getWidth()*1/880);
if(xPoints[i] > maxX)
maxX = xPoints[i];
}
System.out.println(maxX);
}
protected void assignPointsY() {
double x = -2;
double l = 1;
for (int i = 0; i < yPoints.length; i++) {
yPoints[i] = Math.exp(-(Math.pow((x-getT()), 2)))*Math.cos((2*Math.PI*(x-getT()))/l);
x+=0.005;
if(yPoints[i] > maxY)
maxY=yPoints[i];
}
}
protected void FactorTimesArray(){
double yFactor = ((getHeight()-150)/2)/maxY;
for (int i = 0; i <yPoints.length; i++)
yPoints[i] *= yFactor;
}
public double getT() {
return t;
}
public void setT(double t) {
this.t = t;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class DrawFrame extends JFrame {
private static final long serialVersionUID = 1L;
private double timeMath = 0;
private Timer time;
public DrawFrame() {
time = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0 ; i < 1000 ; i++) {
add(new DrawChart(timeMath));
timeMath+=0.001;
}
repaint();
}
});
pack();
}
}
Za bardzo nie wiem jak rozwiązać ten problem, szukałem wielu solucji w sieci, lecz nic sensownego nie znalazłem.