12
Gradient
- // <applet code="Gradient" width="200" height="160"></applet>
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.image.*;
- public class Gradient extends Applet {
- private Image image;
- public void init() {
- int w = 100;
- int h = 100;
- int[] pixs = new int[w * h];
- for (int y = 0; y < h; y++)
- for (int x = 0; x < w; x++) {
- pixs[y * w + x] = 255 << 24 // alpha
- | ((x * 255 / w) << 16) // red
- | ((y * 255 / h) << 8) // green
- | 0; // blue
- }
- image = createImage(new MemoryImageSource(w, h, pixs, 0, w));
- }
- public void paint(Graphics g) {
- Dimension size = getSize();
- int w = size.width / 3, h = size.height / 3;
- g.drawImage(image, 0, 0, w, h, this);
- g.drawImage(image, w, 0, w * 2, h, this);
- g.drawImage(image, 0, h, w, h * 2, this);
- g.drawImage(image, w, h, w * 2, h * 2, this);
- }
- }
$ javac Gradient.java
$ appletviewer Gradient.java
Incidentally, this graphics illustrates the identity (a+b)2 = a2 + b2 + 2ab.
Comments