I'm a coding-beginner and have got a problem with my code. I'm working at an algorithm to create a kind of chessboard. But my array always gets overritten. I've read a lot of articels, but nothing has helped me.
My idea:
int pointsY [y] <--- y equals the row
int pointsX [x] <--- x equals the column
After initializing the array, the content of should change to: current value + a random Integer.
public class draw extends JLabel {
private static final long serialVersionUID = 1L;
static int x;
static int y;
static int columns = 10;
static int rows = 10;
static int offset = 50;
static int size;
static int squareSizX;
static int squareSizY;
static int [] pointsX = new int[columns + 1];
static int [] pointsY = new int[rows + 1];
static Random r = new Random();
public static void draw() {
Random r = new Random();
squareSizX = cmain.frame.getWidth() / columns;
squareSizY = cmain.frame.getHeight() / rows;
for (int iy = 0; iy < pointsY.length; iy++) {
pointsY[iy] = iy * squareSizY;
}
for (int ix = 0; ix < pointsX.length; ix++) {
pointsX[ix] = ix * squareSizX;
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (int iy = 0; iy < pointsY.length; iy++) {
for (int ix = 0; ix < pointsX.length; ix++) {
g.drawString("°", pointsX[ix], pointsY[iy]);
if (iy < pointsY.length - 1) {
g.drawLine(pointsX[ix], pointsY[iy], pointsX[ix], pointsY[iy + 1]);
}
if (ix < pointsX.length - 1) {
g.drawLine(pointsX[ix], pointsY[iy], pointsX[ix + 1], pointsY[iy]);
}
}
}
repaint();
}
public static void randomize() {
Random r = new Random();
for (int ix = 0; ix < pointsX.length; ix++) {
pointsX[ix] = pointsX[ix] + r.nextInt(offset);
}
for (int iy = 0; iy < pointsY.length; iy++) {
pointsY[iy] = pointsY[iy] + r.nextInt(offset);
}
}
}
When I run this code, they're all in one line, but I want that they are randomly distributed. I'm looking forward to your reply.
Aucun commentaire:
Enregistrer un commentaire