我的目标是创建一个随机生成的点的无限循环。 结果如下:
但由于某种原因,几分钟后,点停止生成,它只是变成黑屏。如果有人有更好的方法,或者可以帮助我,那将是伟大的。
package game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.color.ColorSpace;
import java.util.Random;
import main.Commons;
public class Background implements Commons {
private int dX = -4;
private int subX = 800;
private int subX2 = 1600;
private Random r = new Random();
private Dot dot;
private Dot sDot;
private final int ARRAY_LENGTH = 100;
private int[] xPos = new int[ARRAY_LENGTH];
private int[] yPos = new int[ARRAY_LENGTH];
private int[] size = new int[ARRAY_LENGTH];
private Color[] cArray = new Color[ARRAY_LENGTH];
private int[] xPos2 = new int[ARRAY_LENGTH];
private int[] yPos2 = new int[ARRAY_LENGTH];
private int[] size2 = new int[ARRAY_LENGTH];
private Color[] cArray2 = new Color[ARRAY_LENGTH];
private boolean init1 = true;
private boolean init2 = true;
public Background() {
subX = 0;
subX2 = WIDTH;
}
public void update() {
subX += dX;
subX2 += dX;
}
public void paint(Graphics g) {
// background
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
// dots
if (init1 || subX < -WIDTH) {
for (int i = 0; i < ARRAY_LENGTH; i++) {
xPos[i] = r.nextInt(WIDTH);
yPos[i] = r.nextInt(HEIGHT);
size[i] = r.nextInt(4);
cArray[i] = new Color(r.nextInt(256), r.nextInt(256),
r.nextInt(256));
}
init1 = false;
}
if (init2 || subX2 < -WIDTH) {
for (int i = 0; i < ARRAY_LENGTH; i++) {
xPos2[i] = r.nextInt(WIDTH);
yPos2[i] = r.nextInt(HEIGHT);
size2[i] = r.nextInt(4);
cArray2[i] = new Color(r.nextInt(256), r.nextInt(256),
r.nextInt(256));
}
init2 = false;
}
if (!init1) {
for (int i = 0; i < ARRAY_LENGTH; i++) {
dot = new Dot(xPos[i] + subX, yPos[i], size[i], cArray[i]);
dot.paint(g);
}
if (subX == -WIDTH) {
init1 = true;
subX = WIDTH;
}
}
if (!init2) {
for (int i = 0; i < ARRAY_LENGTH; i++) {
sDot = new Dot(xPos2[i] + subX2, yPos2[i], size2[i], cArray2[i]);
sDot.paint(g);
}
if (subX == 0) {
init2 = true;
subX2 = WIDTH;
}
}
}
}
package game;
import java.awt.Color;
import java.awt.Graphics;
import main.Commons;
public class Dot implements Commons {
int x;
int y;
int size;
int loopTimes = 0;
Color c;
public Dot(int x, int y, int size, Color c) {
this.x = x;
this.y = y;
this.size = size;
this.c = c;
}
public void paint(Graphics g) {
g.setColor(c);
g.fillRect(x + (WIDTH * loopTimes), y, size, size);
}
}
感谢所有帮助,
-Steve