Java graphics not showing

I am a little new to java, but I had a lot of experience in other programming languages. Right now I'm trying to create a simple scorched earth game, and I'm having problems with the appearance of the landscape. I use a 2d height field to represent the landscape. In my init method, I generate a relief using several sin functions and random numbers. I found this technique in an online tutorial.

float flat, peak;
flat = 70; peak = 50;

int rand1, rand2, rand3;
rand1 = gen.nextInt() % 4 + 1;
rand2 = gen.nextInt() % 4 + 1;
rand3 = gen.nextInt() % 4 + 1;

for (int a=0; a<750; a++) {
    double height = peak / rand1 * Math.sin((double)a / flat * rand1 + rand1);
    height += peak / rand2 * Math.sin((double)a / flat * rand2 + rand2);
    height += peak / rand3 * Math.sin((double)a / flat * rand3 + rand3);

    height += 250;
    heights[a] = (int)height;
}

Then in my method paint()I draw a landscape whose height is represented as pixels from the bottom of the screen, whose length is 750 pixels and 500.

public void paint(Graphics g)
{
    g.setColor(Color.green);

    for (int a=0; a<750; a++) {
        g.fillRect(a, 500-heights[a], 1, heights[a]);
    }
}

, . , . , my height [] , init . - , ?

+3
1

:

rand1 = 0 (or 1)
rand2 = 0 (or 1)
rand3 = 0 (or 1), 

. mod% 4 (-1 ), 1 - 1 0; , NaN , - 0.

Btw. , , , , , ,

: , my height [] , init . - , ?

, , . , . (, , (x mod 4) + 1) . rand1, rand2, rand3, . , ? Sin (..)? , , , , .

int[] heights = new int[750];
public MathTest()
{
    Random r = new Random();
    float flat;
    float peak;
    flat = 70;
    peak = 50;

    int rand1;
    int rand2;
    int rand3;

    int x = r.nextInt();
    int y = r.nextInt();
    int z = r.nextInt();

    System.out.println("x : " + x);
    System.out.println("y : " + y);
    System.out.println("z : " + z);

    System.out.println("-210263172");
    System.out.println("-724188689");
    System.out.println("-1092425465");

    System.out.println(-210263172 % 4);
    System.out.println(-724188689 % 4);
    System.out.println(-1092425465 % 4);

    rand1 = x % 4 + 1;
    rand2 = y % 4 + 1;
    rand3 = z % 4 + 1;


    for( int a = 0; a < 750; a++ )
    {
        System.out.println("rand1: " + rand1);
        System.out.println("rand2: " + rand2);
        System.out.println("rand3: " + rand3);
        double height =
            peak / rand1 * Math.sin((double) a / flat * rand1 + rand1);
        height +=
            peak / rand2 * Math.sin((double) a / flat * rand2 + rand2);
        height +=
            peak / rand3 * Math.sin((double) a / flat * rand3 + rand3);

        height += 250;
        heights[a] = (int) height;
    }
    int i;
    for(i = 0; i < 23; i++)
    {
        System.out.println("heights[" + i + "]: " + heights[i]);
    }

}
+1

All Articles