Z-buffer algorithm implementation problem

Hi guys, now I’m making a 3d game engine in Java from scratch.

Now I’m working at the Z-buffer because I didn’t like the effect of the Painter’s Algorithm(Z-sorting).

My question is: what is the easiest way to determine the Z value of each pixel? Please include a source code if you can :wink:

P.S.: The renderer contains just triangles, something like that:

class Vector3d {
double x,y,z;
class Triangle {
Vector3d v1,v2,v3;
Color color;
class Renderer {
public Triangle[] triangles;
[ . . . ]