I use a power feature to make progressively more stars towards the central horizontal line in the image.
Look at this:
for (int i=0; i<50000; i++) //Generates lower half of dim stars in galactic plane.
{
g.fillRect((int)(Math.random()*1250),(int)(374+Math.pow(375,(Math.random()))),1,1);
}
A for loop which runs 50,000 times, because i is set to 0, then the loop is defined to run while it is below 50,000, and at last "i" is told to increment every time it runs through the loop.
Inside the loop, the { and } tell the program that that is what it has to do inside the loop. It ignores what is after until the loop is broken.
The a method tells it to feel a rectangle. The parameters are inside (), and they are:
(x position, y position, width, height)
(0,0) in Java is the top left corner, not bottom left like on normal coordinate systems. So x's are "inverted".
So by setting the last two numbers to 1, the stars become 1 pixel.
The first parameter (x position) is
(int)(Math.random()*1250)
(int) tells it to convert the number to an integer, because pixel positions must be integers.
This generates the number: (Math.random()*1250)
Math.random() gives a random number from 0 to 1, with a lot of decimals. It is multiplied to 1250, so it becomes a number between 0 and 1250.
This is the y-position parameter:
(int)(374+Math.pow(375,(Math.random())))
Math.pow(a,b) calculates what c would be here: c = ab
So by setting a = 375 and uplifting it in a random number between 0 and 1, I get a number between 0 and 375, but where progressively more numbers will be closer to 0. By adding 374 to that, I get numbers between 0+374 and 375+374.
The result is scattered, dark grey pixels on the lower half of the window, and where they will get increasingly dense towards the center of the screen.
I used 4 colors, starting with dark grey, then going to white at last, and each time I only generate the lower or upper half of the window (in most cases).