Blending Red and Green

A purple box is a pretty boring image, and they're several easier and more efficient ways to draw it. However some images are much easier to draw with an algorithmic approach. The following applet creates a 256 by 256 pixel image that blends from pure black to bright red on the lower left hand corner, bright green on the upper right hand corner, and various mixtures in between with yellow appearing in the bottom right corner.

(How good this applet looks depends heavily on the number of colors your monitor can display without dithering.)


import java.applet.*;
import java.awt.*;
import java.awt.image.*; // Don't forget this!


public class RGBlend extends Applet {

  private Image picture;

  public void init() {

    int opaque = 255 << 24;
    int[] pixels = new int[256*256];
    for (int i=0; i < 256; i++) {
      int red = i << 16;
      for (int j = 0; j < 256; j++) {
        int green = j << 8;
        pixels[i*256 + j] = opaque | red | green;
      }
    }
    MemoryImageSource RGBlendMIS = 
     new MemoryImageSource(256, 256, pixels, 0, 256);
    this.picture = this.createImage(RGBlendMIS);

  }
  
  public void paint(Graphics g) {
  
    // picture is created internally so we know it won't be null
    g.drawImage(this.picture, 0, 0, this);
    
  }

}

Previous | Next | Top
Last Modified April 10, 2000
Copyright 1997-2000 Elliotte Rusty Harold
elharo@metalab.unc.edu