class R5_1 { public void paint( Graphics g ) { Random random = new Random(); double area = 0.0;
while( area < 250000.0 ) { int red = random.nextInt( 256 ); int green = random.nextInt( 256 ); int blue = random.nextInt( 256 ); Color c = new Color( red, green, blue );
int px = random.nextInt( 200 ); int py = random.nextInt( 300 ); g.setColor( c ); area += px * py; } }
public static void main( String [] args ) { R5_1 rcf = new R5_1();
private static final long serialVersionUID = -4569467927269047759L;
public R5_1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void update(Graphics g) {
paint(g);
}
@Override
public void paint(Graphics g) {
/* remove
Random random = new Random();
double area = 0.0;
while( area < 250000.0 )
{
int red = random.nextInt( 256 );
int green = random.nextInt( 256 );
int blue = random.nextInt( 256 );
Color c = new Color( red, green, blue );
int px = random.nextInt( 200 );
int py = random.nextInt( 300 );
g.setColor( c );
area += px * py;
}
*/
super.paint(g);
int frameArea = getWidth() * getHeight();
int totalRectArea = 0;
Random r = new Random();
while (totalRectArea < frameArea) {
Rectangle rect = new Rectangle();
rect.setLocation(r.nextInt(getWidth()), r.nextInt(getHeight()));
rect.setSize(r.nextInt(getWidth() - rect.x), r.nextInt(getHeight()
- rect.y));
g.setColor(randomColor(r));
g.drawRect(rect.x, rect.y, rect.width, rect.height);
System.out.println(getWidth() + ", " + getHeight());
System.out.println(rect);
totalRectArea += rect.width * rect.height;
}
}
private Color randomColor(Random r) {
return new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
R5_1 rcf = new R5_1();
class RandomCircleFrame
{
public void paint( Graphics g )
{
Random random = new Random();
double area = 0.0;
while( area < 1000000.0 )
{
int red = random.nextInt( 256 );
int green = random.nextInt( 256 );
int blue = random.nextInt( 256 );
Color c = new Color( red, green, blue );
int px = random.nextInt( 500 );
int py = random.nextInt( 500 );
int r = random.nextInt( 20 )+1;
g.setColor( c );
g.fillOval( px, py, 2 * r, 2 * r);
area += Math.PI * r * r;
}
}
public static void main( String [] args )
{
RandomCircleFrame rcf = new RandomCircleFrame();
class RandomCircleFrame extends Frame {
public void paint(Graphics g) {
Random random = new Random();
int area = 0;
int frameArea = getWidth() * getHeight();
while (area < frameArea) {
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
Color c = new Color(red, green, blue);
int px = random.nextInt(500);
int py = random.nextInt(500);
int width = random.nextInt(20) + 1;
int height = random.nextInt(20) + 1;
g.setColor(c);
g.fillRect(px, py, width, height);
area = width * height;
}
}
public static void main(String[] args) {
final RandomCircleFrame rcf = new RandomCircleFrame();
rcf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
rcf.setSize(500, 500);
rcf.setTitle("ランダムな四角");
rcf.setVisible(true);
}
}
class R5_2 extends Frame {
public void paint(Graphics g) {
Random random = new Random();
int area = 0;
int frameArea = getWidth() * getHeight();
while (area < frameArea) {
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
Color c = new Color(red, green, blue);
int px = random.nextInt(500);
int py = random.nextInt(500);
int width = random.nextInt(20) + 1;
int height = random.nextInt(25) + 1;
g.setColor(c);
g.fillRect(px, py, width, height);
area += width * height;
}
}
public static void main(String[] args) {
R5_2 rcf = new R5_2();
class R5_2 extends Frame {
public void paint(Graphics g) {
List<Rectangle> rects = new LinkedList<Rectangle>();
Random random = new Random();
while (true) {
if (rects.size() >= 100) {
Rectangle eraseRect = rects.remove(0);
g.setColor(this.getBackground());
g.fillRect(eraseRect.x, eraseRect.y, eraseRect.width, eraseRect.height);
}
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
Color c = new Color(red, green, blue);