I was trying to create a 2D Animation in Java of a moving line on Panel(A line moving from one point to another in the Panel). I hope its possible. Here's the code I used.
private void movingline(int length) throws InterruptedException {
for(int i = 0; i + length < width; i++){
for(int j = 0; j + length < height; j++){
eraseline();
drawLine(Color.cyan, i, j, i+length, j+length);
erase = true;
}
}
}
private void eraseline() {
if(erase){
fillCanvas(Color.BLUE);
}
}
On running the code, the Panel doesn't show up.
Here's the code to draw the line.
public void drawLine(Color c, int x1, int y1, int x2, int y2) {
int pix = c.getRGB();
int dx = x2 - x1;
int dy = y2 - y1;
canvas.setRGB(x1, y1, pix);
if (dx != 0) {
float m = (float) dy / (float) dx;
float b = y1 - m*x1;
dx = (x2 > x1) ? 1 : -1;
while (x1 != x2) {
x1 += dx;
y1 = Math.round(m*x1 + b);
canvas.setRGB(x1, y1, pix);
}
}
repaint();
}
On running the code the Panel doesn't show up with the moving line. Any help would be much appreciated.