2

RandomSquares

  1. // <applet code="RandomSquares" width="300" height="200"></applet>
  2.  
  3. import java.applet.Applet;
  4. import java.awt.Graphics;
  5.  
  6. import java.awt.Color;
  7.  
  8. public class RandomSquares extends Applet {
  9.     protected int numberOfSquares = 8;
  10.  
  11.     public void paint(Graphics g) {
  12.         int width = getSize().width, height = getSize().height;
  13.         int maxsize = Math.min(width, height);
  14.  
  15.         //  System.out.println(g.getClip());
  16.         g.setClip(null);
  17.  
  18.         g.setColor(Color.white);
  19.         g.fillRect(0, 0, width, height);
  20.  
  21.         for (int n = 0; n < numberOfSquares; n++) {
  22.             Color c = new Color(rint(256), rint(256), rint(256));
  23.  
  24.             int l = rint(maxsize / 2);
  25.             int x = rint(width - l);
  26.             int y = rint(height - l);
  27.  
  28.             g.setColor(c);
  29.             g.fillRect(x, y, l, l);
  30.         }
  31.     }
  32.  
  33.     public void update(Graphics g) {
  34.         paint(g);
  35.     }
  36.  
  37.     private int rint(int max) {
  38.         return (int) Math.floor(Math.random() * max);
  39.     }
  40. }
$ javac RandomSquares.java 
$ appletviewer RandomSquares.java

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].