Grundlagen der Programmierung 2 SS 2005 - Datei RandomFiguresFrame.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class RandomFiguresFrame extends JFrame {
int count;
int random(int lb, int ub) {
return (int) (Math.random() * (ub - lb + 1) + lb);
}
RandomFiguresFrame(int n) {
super("Zeichnen von " + n + " Klecksen");
count = n;
setSize(550, 550);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
for (int i = 0; i < count; i++) {
int x = random(0, 400);
int y = random(0, 400);
int width = random(50, 150);
int height = random(50, 150);
g.setColor(new Color(random(0, 255), random(0, 255),
random(0, 255)));
if (random(0, 1) == 0) // Entscheidung für Rechteck
g.fillRect(x, y, width, height);
else
g.fillOval(x, y, width, height);
}
}
public static void main(String[] args) {
RandomFiguresFrame b = new RandomFiguresFrame(new Integer(args[0])
.intValue());
}
}
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 22.04.2005


