Hallo zusammen,
Ich stehe vor folgendem Problem.
Wenn ich einen Zufallszahl zwischen 20 und 580 generieren lassen will, wird trotzdem einen Zahl über 580 generiert.
Hier ist die Klasse
Hier sind einige Ausgaben:
Und das ist die Main-Methode:
Ich stehe vor folgendem Problem.
Wenn ich einen Zufallszahl zwischen 20 und 580 generieren lassen will, wird trotzdem einen Zahl über 580 generiert.
Hier ist die Klasse
Java:
import processing.core.PGraphics;
import java.util.Random;
class Ball {
int x, y;
int fillColor;
float xSpeed = 5;
float ySpeed = 5;
int widthBound, heightBound;
Random random = new Random();
Ball(int fillColor, int widthBound, int heightBound) {
this.fillColor = fillColor;
this.widthBound = widthBound;
this.heightBound = heightBound;
this.randomPos();
}
void randomPos() {
this.x = (int) (Math.random()*580+20);
this.y = (int) (Math.random()*580+20);
System.out.println("X: "+this.x);
System.out.println("Y: "+this.y);
}
void draw(PGraphics g) {
g.fill(this.fillColor);
g.ellipse(this.x, this.y, 40, 40);
x += xSpeed;
y += ySpeed;
if (x < 20 || x > widthBound-20) {
xSpeed *= -1;
}
if (y < 20 || y > heightBound-20) {
ySpeed *= -1;
}
}
}
Hier sind einige Ausgaben:
Code:
X: 208
Y: 442
X: 426
Y: 535
X: 421
Y: 593 // das ist die Zahl, die über 580 ist
Und das ist die Main-Methode:
Code:
import processing.core.PApplet;
public class Billiardtisch extends PApplet {
public static void main(String[] args) {
PApplet.runSketch(new String[]{""}, new Billiardtisch());
}
Ball[] balls = new Ball[100];
public void settings() {
super.size(600, 600);
}
public void setup() {
frameRate(20);
super.background(0);
for(int i = 0; i < balls.length; i++) {
balls[i] = new Ball((color((int) (Math.random()*(255)),(int) (Math.random()*(255)),(int) (Math.random()*(255)))),super.width,super.height);
}
}
public void draw() {
super.background(0,102,51);
for (Ball elem : balls) elem.draw(super.g);
}
}