12

Gradient

  1. // <applet code="Gradient" width="200" height="160"></applet>
  2.  
  3. import java.applet.Applet;
  4. import java.awt.*;
  5. import java.awt.image.*;
  6.  
  7. public class Gradient extends Applet {
  8.     private Image image;
  9.  
  10.     public void init() {
  11.         int w = 100;
  12.         int h = 100;
  13.  
  14.         int[] pixs = new int[w * h];
  15.  
  16.         for (int y = 0; y < h; y++)
  17.             for (int x = 0; x < w; x++) {
  18.                 pixs[y * w + x] = 255 << 24     // alpha
  19.                         | ((x * 255 / w) << 16) // red
  20.                         | ((y * 255 / h) << 8)  // green
  21.                         | 0;                    // blue
  22.             }
  23.         image = createImage(new MemoryImageSource(w, h, pixs, 0, w));
  24.     }
  25.  
  26.     public void paint(Graphics g) {
  27.         Dimension size = getSize();
  28.         int w = size.width / 3, h = size.height / 3;
  29.  
  30.         g.drawImage(image, 0, 0, w, h, this);
  31.         g.drawImage(image, w, 0, w * 2, h, this);
  32.         g.drawImage(image, 0, h, w, h * 2, this);
  33.         g.drawImage(image, w, h, w * 2, h * 2, this);
  34.     }
  35. }
$ javac Gradient.java
$ appletviewer Gradient.java

Incidentally, this graphics illustrates the identity (a+b)2 = a2 + b2 + 2ab.

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].