I'm working with applets in school ( in a JAVA class) , and the program asks for us to draw a little box in the middle of the screen. Using certain keys (Q, E, A, etc.) the box will move around leaving a trail. When the box has moved 250 times, the trail that it has left should start fading from black to white (the background color). It should start fading from where the box started (not all at once).

I have the basic trail set up, and once the box moves 250 times, the entire trail turns gray. I don't know how to make it fade properly- any help? :)

Here's my code so far:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Trail extends JApplet {

private Path canvas;

public void init ( ) {
canvas = new Path();
setContentPane(canvas);
}
}

class Path extends JPanel implements KeyListener{

private int x, y;
private int [][] grid;
private int count;

public Path ( ) {
x = y = 25;
count = 0;
grid = new int [50][50];
for (int i = 0; i <grid.length; i++) {
for (int j = 0; j <grid.length; j++) {
grid [i][j]= 0;
}
}

setBackground(Color.white);
addKeyListener (this);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i <grid.length; i++) {
for (int j = 0; j <grid.length; j++) {
g.setColor (Color.white);
if (x == i && y == j) {
g.setColor (Color.black);
}

if (grid [i][j] == 1) {
g.setColor (Color.black);
if (count > 250) {
g.setColor (Color.gray);
}

}
g.fillRect (j*8, i*8, 8, 8);

}
}

requestFocus ();
}

public void keyTyped ( KeyEvent e ) {
char ch = e.getKeyChar();
}

public void keyPressed ( KeyEvent e ) {
int value = e.getKeyCode();
if (value == KeyEvent.VK_Q && y < 500) {
x--;
count ++;
y--;
System.out.print (count + " ");
grid[x][y] = 1;

}
if (value == KeyEvent.VK_W && y < 500) {
x--;
count ++;
System.out.print (count + " ");
grid[x][y] = 1;

}
if (value == KeyEvent.VK_E && x < 500) {
x--;
count ++;
y++;
System.out.print (count + " ");
grid[x][y] = 1;

}
if (value == KeyEvent.VK_A && x < 500) {
y--;
count ++;
System.out.print (count + " ");
grid[x][y] = 1;
}

if (value == KeyEvent.VK_D && x < 500) {
y++;
count ++;
System.out.print (count + " ");
grid[x][y] = 1;
}

if (value == KeyEvent.VK_Z && x < 500) {
x++;
count ++;
y--;
System.out.print (count + " ");
grid[x][y] = 1;
}

if (value == KeyEvent.VK_X && x < 500) {
x++;
count ++;
System.out.print (count + " ");
grid[x][y] = 1;
}

if (value == KeyEvent.VK_C && x < 500) {
y++;
count ++;
x++;
System.out.print (count + " ");
grid[x][y] = 1;
}

repaint ( );
}
public void keyReleased ( KeyEvent e ) {}
}

__________________________________________

Thanks in advance. :)