home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / Atom.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  2.2 KB  |  85 lines

  1. /*
  2.  * A set of classes to parse, represent and display Chemical compounds in
  3.  * .xyz format (see http://chem.leeds.ac.uk/Project/MIME.html)
  4.  */
  5.  
  6. package sunw.demo.molecule;
  7.  
  8. import java.awt.*;
  9. import java.awt.image.*;
  10.  
  11. class Atom {
  12.     private static java.awt.Panel panel;
  13.     private static byte[] data;
  14.     private final static int R = 40;
  15.     private final static int hx = 15;
  16.     private final static int hy = 15;
  17.     private final static int bgGrey = 192;
  18.     private final static int nBalls = 16;
  19.     private static int maxr;
  20.  
  21.     private int Rl;
  22.     private int Gl;
  23.     private int Bl;
  24.     private Image balls[];
  25.  
  26.     static {
  27.     data = new byte[R * 2 * R * 2];
  28.     int mr = 0;
  29.     for (int Y = 2 * R; --Y >= 0;) {
  30.         int x0 = (int) (Math.sqrt(R * R - (Y - R) * (Y - R)) + 0.5);
  31.         int p = Y * (R * 2) + R - x0;
  32.         for (int X = -x0; X < x0; X++) {
  33.         int x = X + hx;
  34.         int y = Y - R + hy;
  35.         int r = (int) (Math.sqrt(x * x + y * y) + 0.5);
  36.         if (r > mr)
  37.             mr = r;
  38.         data[p++] = r <= 0 ? 1 : (byte) r;
  39.         }
  40.     }
  41.     maxr = mr;
  42.     }
  43.     static void setPanel(java.awt.Panel app) {
  44.     panel = app;
  45.     }
  46.     Atom(int Rl, int Gl, int Bl) {
  47.     this.Rl = Rl;
  48.     this.Gl = Gl;
  49.     this.Bl = Bl;
  50.     }
  51.     private final int blend(int fg, int bg, float fgfactor) {
  52.     return (int) (bg + (fg - bg) * fgfactor);
  53.     }
  54.     private synchronized void Setup() {
  55.     balls = new Image[nBalls];
  56.     byte red[] = new byte[256];
  57.     red[0] = (byte) bgGrey;
  58.     byte green[] = new byte[256];
  59.     green[0] = (byte) bgGrey;
  60.     byte blue[] = new byte[256];
  61.     blue[0] = (byte) bgGrey;
  62.     for (int r = 0; r < nBalls; r++) {
  63.         float b = (float) (r+1) / nBalls;
  64.         for (int i = maxr; i >= 1; --i) {
  65.         float d = (float) i / maxr;
  66.         red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
  67.         green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
  68.         blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
  69.         }
  70.         IndexColorModel model = new IndexColorModel(8, maxr + 1,
  71.                             red, green, blue, 0);
  72.         balls[r] = panel.createImage(
  73.         new MemoryImageSource(R*2, R*2, model, data, 0, R*2));
  74.     }
  75.     }
  76.     synchronized void paint(Graphics gc, int x, int y, int radius) {
  77.     if (balls == null) {
  78.         Setup();
  79.     }
  80.     Image i = balls[radius];
  81.     int size = 10 + radius;
  82.     gc.drawImage(i, x - (size/2), y - (size/2), size, size, panel);
  83.     }
  84. }
  85.