I can't find the error, say something about run()
, but looks like right.
package Animation;
import java.awt.*;
import java.awt.geom.*;
import java.util.Date;
import java.util.ArrayList;
import Point.Point;
import java.util.Collections;
import java.util.*;
import java.awt.image.BufferedImage;
public class Animation extends TimerTask{
private int radius;
private int segments;
private int repetitions;
private int x_0, y_0;
private ArrayList<Point> point_list;
//variáveis panit
//--------------------------------------------------
Point pi = new Point();
Point pf = new Point();
private double size;
private double jump_delta;
private double remainder;
private double jump_alfa;
private int jump;
//TODO: instanciar variáveis como privadas
Rectangle2D.Double quad;
Shape s;
double[] matrizInicial;
double[] matrizFinal;
AffineTransform inicialTranformation;
AffineTransform tracking ;
AffineTransform transfIntermed;
Flag turn;
Shape aux;
//The window in which everything is shown.
private BufferedImageDrawer buffid;
//The background image
private BufferedImage bg;
//calculateBpasenham
//----------------------------------------------------------
AffineTransform tr;
AffineTransform origem;
AffineTransform at;
public Animation(BufferedImageDrawer bid, BufferedImage backGround,
int height,int delay, int r, int s, int n, int x, int y ) {
this.radius = r;
this.segments = s;
this.repetitions = n;
this.x_0 = x;
this.y_0 = y;
point_list = calculateBrasenham();
//addWindowListener(new MyFinishWindow());
}
private ArrayList<Point> calculateBrasenham() {
ArrayList<Point> left_half = new ArrayList<Point>();
ArrayList<Point> rigth_half = new ArrayList<Point>();
int x = 0;
int y = radius;
int decision_var = 1 - radius;
rigth_half.add( new Point(x , y ) );
while ( y > x ) {
x++;
if (decision_var < 0) {
decision_var += 2 * x + 3;
} else {
decision_var += 2 * ( x - y ) + 5;
y--;
}
rigth_half.add( new Point(x , y ) );
}
for (Point tmp : rigth_half ) {
left_half.add( 0, new Point( tmp.get_y() , tmp.get_x() ));
}
ArrayList<Point> final_list = new ArrayList<Point>();
for (Point tmp : left_half) {
final_list.add(0, new Point( -tmp.get_x(), tmp.get_y()) );
}
int size = rigth_half.size() - 1;
for (int i = size; i > 0; i-- ) {
final_list.add( new Point( -rigth_half.get(i).get_x() , rigth_half.get(i).get_y() ) );
}
rigth_half.addAll(left_half);
final_list.addAll(rigth_half);
for (int i = 0; i < final_list.size(); i++ ) {
Point tmp = final_list.get(i);
final_list.get(i).set_x(tmp.get_x() + radius);
final_list.get(i).set_y(tmp.get_y() );
}
return final_list;
}
private void calculateTransformations(Point pf, Flag turn, AffineTransform t) {
tr = new AffineTransform();
tr.setToTranslation(pf.get_x(), pf.get_y());
if ( turn.get_value() == true ) {
origem = new AffineTransform();
origem.setToTranslation(x_0,y_0);
at = AffineTransform.getRotateInstance(-Math.PI / 4,pf.get_x() ,pf.get_y() );
t.concatenate(origem);
t.concatenate(at);
t.concatenate(tr);
t.scale(1.5,1.5);
turn.set_value(false);
} else {
origem = new AffineTransform();
origem.setToTranslation(x_0,y_0);
at = AffineTransform.getRotateInstance(0,pf.get_x() ,pf.get_y() );
t.concatenate(origem);
t.concatenate(at);
t.concatenate(tr);
turn.set_value(true);
}
}
private void calculateInterpolationPoints(Point pf, int jump, int i ,int r ) {
int size = point_list.size() - 1;
if ( (i * jump + jump) > size ) {
pf.set_x(point_list.get(size).get_x() + r);
pf.set_y(point_list.get(size).get_y());
} else {
pf.set_x(point_list.get((i * jump ) + jump ).get_x() + r);
pf.set_y(point_list.get((i * jump ) + jump ).get_y());
}
}
public void run() {
buffid.g2dbi.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
size = (double)point_list.size();
jump_delta = size / segments;
remainder = size % segments;
jump_alfa = (int)(remainder/jump_delta);
jump = (int) jump_delta;
segments = segments + (int)jump_alfa;
pi.set_x(point_list.get(0).get_x());
pi.set_y(point_list.get(0).get_y());
quad = new Rectangle2D.Double(0, 0, 10 , 10);
AffineTransform normalizer = new AffineTransform();
normalizer.setToScale(1, -1);
AffineTransform translate = new AffineTransform();
translate.setToTranslation(0, y_0 + radius + 100);
normalizer.preConcatenate(translate);
buffid.g2dbi.transform(normalizer);
matrizInicial = new double[6];
matrizFinal = new double[6];
inicialTranformation = new AffineTransform();
tracking = new AffineTransform();
inicialTranformation.setToTranslation(x_0 + pi.get_x(), y_0 + pi.get_y());
inicialTranformation.getMatrix(matrizInicial);
turn = new Flag(true);
buffid.g2dbi.setPaint(Color.white);
buffid.g2dbi.fill(new Rectangle(0,0,radius * 2 * repetitions + 100 + x_0 + radius,radius + y_0 + 100));
for ( int r = 0; r < repetitions; r++ ) {
int shift = r * radius * 2;
for (int j = 0; j < segments; j++) {
calculateInterpolationPoints(pf, jump, j, shift);
calculateTransformations( pf, turn, tracking);
tracking.getMatrix(matrizFinal);
aux = null;
for (double step = 0; step < 35; step++ ) {
transfIntermed = new AffineTransform( interpola( matrizInicial, matrizFinal, step/35) );
s = transfIntermed.createTransformedShape(quad);
// limpaJanela(buffid.g2dbi, aux);
// buffid.g2dbi.setPaint(Color.black);
// buffid.g2dbi.draw(s);
// congela(10);
aux = s;
}
tracking.setToIdentity();
for (int i = 0; i < 6; i++ ) matrizInicial[i] = matrizFinal[i];
}
}
}
public double[] interpola(double[] inicial,double[] terminal, double alfa) {
double[] intermed = new double[inicial.length];
for (int i=0; i<intermed.length; i++)
{
intermed[i] = (1-alfa)*inicial[i] + alfa*terminal[i];
}
return(intermed);
}
}
the error:
Exception in thread "Timer-0" java.lang.NullPointerException
at Animation.Animation.run(Animation.java:181)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)