Hallo
Ich habe ein Programm programmiert, welches Kreise an random Positionen spawnen lässt. Nun habe ich das Problem, dass das Programm einfach nicht aufhört. Ich hoffe, ihr könnt mir helfen und den Fehler finden. Bin noch Anfänger, bitte nicht zu hart meckern.
Gruß Drasco
Ich habe ein Programm programmiert, welches Kreise an random Positionen spawnen lässt. Nun habe ich das Problem, dass das Programm einfach nicht aufhört. Ich hoffe, ihr könnt mir helfen und den Fehler finden. Bin noch Anfänger, bitte nicht zu hart meckern.
Gruß Drasco
Java:
import javax.swing.*;
import java.awt.*;
public class BauplanZufallBall extends JFrame {
VorlageBall ball = new VorlageBall();
public BauplanZufallBall() {
this.setTitle("Random Kreise");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.add(ball);
this.setVisible(true);
}
public class VorlageBall extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.orange);
for(int i = 0; i < 10; i++) {
int x = (int) (Math.random() * 800);
int y = (int) (Math.random() * 600);
int r = (int) (Math.random() * 10);
int z = (int) (Math.random() * 30);
int s = z;
if (r > 5) {
g2.fillOval(x, y, z, s);
}
else {
g2.drawOval(x, y, z, s);
g2.setColor(Color.green);
}
ball.repaint();
try{
Thread.sleep(10);
}catch(Exception e){}
}
}
}
}