home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / AppB / DitherTest / DitherTest.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-07-26  |  5.2 KB  |  239 lines

  1. import java.applet.Applet;
  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Frame;
  6. import java.awt.Image;
  7. import java.awt.Window;
  8. import java.awt.image.ColorModel;
  9. import java.awt.image.MemoryImageSource;
  10.  
  11. public class DitherTest extends Applet implements Runnable {
  12.    static final int NOOP = 0;
  13.    static final int RED = 1;
  14.    static final int GREEN = 2;
  15.    static final int BLUE = 3;
  16.    static final int ALPHA = 4;
  17.    static final int SATURATION = 5;
  18.    Thread kicker;
  19.    DitherControls XControls;
  20.    DitherControls YControls;
  21.    DitherCanvas canvas;
  22.  
  23.    public synchronized void start() {
  24.       if (this.canvas.getImage() == null) {
  25.          this.kicker = new Thread(this);
  26.          this.kicker.start();
  27.       }
  28.  
  29.    }
  30.  
  31.    public void restart() {
  32.       this.stop();
  33.       this.canvas.setImage((Image)null);
  34.       this.start();
  35.    }
  36.  
  37.    public synchronized void stop() {
  38.       try {
  39.          if (this.kicker != null) {
  40.             this.kicker.stop();
  41.          }
  42.       } catch (Exception var3) {
  43.       }
  44.  
  45.       this.kicker = null;
  46.    }
  47.  
  48.    void applymethod(int[] c, int method, int step, int total, int[] vals) {
  49.       if (method != 0) {
  50.          int val = total < 2 ? vals[0] : vals[0] + (vals[1] - vals[0]) * step / (total - 1);
  51.          switch (method) {
  52.             case 1:
  53.                c[0] = val;
  54.                break;
  55.             case 2:
  56.                c[1] = val;
  57.                break;
  58.             case 3:
  59.                c[2] = val;
  60.                break;
  61.             case 4:
  62.                c[3] = val;
  63.                break;
  64.             case 5:
  65.                int max = Math.max(Math.max(c[0], c[1]), c[2]);
  66.                int min = max * (255 - val) / 255;
  67.                if (c[0] == 0) {
  68.                   c[0] = min;
  69.                }
  70.  
  71.                if (c[1] == 0) {
  72.                   c[1] = min;
  73.                }
  74.  
  75.                if (c[2] == 0) {
  76.                   c[2] = min;
  77.                }
  78.          }
  79.  
  80.       }
  81.    }
  82.  
  83.    int colormethod(String s, int[] vals) {
  84.       int method = 0;
  85.       if (s == null) {
  86.          s = "";
  87.       }
  88.  
  89.       String lower = s.toLowerCase();
  90.       int len = 0;
  91.       if (lower.startsWith("red")) {
  92.          method = 1;
  93.          lower = lower.substring(3);
  94.       } else if (lower.startsWith("green")) {
  95.          method = 2;
  96.          lower = lower.substring(5);
  97.       } else if (lower.startsWith("blue")) {
  98.          method = 3;
  99.          lower = lower.substring(4);
  100.       } else if (lower.startsWith("alpha")) {
  101.          method = 4;
  102.          lower = lower.substring(4);
  103.       } else if (lower.startsWith("saturation")) {
  104.          method = 5;
  105.          lower = lower.substring(10);
  106.       }
  107.  
  108.       if (method == 0) {
  109.          vals[0] = 0;
  110.          vals[1] = 0;
  111.          return method;
  112.       } else {
  113.          int begval = 0;
  114.          int endval = 255;
  115.  
  116.          try {
  117.             int dash = lower.indexOf(45);
  118.             if (dash < 0) {
  119.                begval = endval = Integer.parseInt(lower);
  120.             } else {
  121.                begval = Integer.parseInt(lower.substring(0, dash));
  122.                endval = Integer.parseInt(lower.substring(dash + 1));
  123.             }
  124.          } catch (Exception var10) {
  125.          }
  126.  
  127.          if (begval < 0) {
  128.             begval = 0;
  129.          }
  130.  
  131.          if (endval < 0) {
  132.             endval = 0;
  133.          }
  134.  
  135.          if (begval > 255) {
  136.             begval = 255;
  137.          }
  138.  
  139.          if (endval > 255) {
  140.             endval = 255;
  141.          }
  142.  
  143.          vals[0] = begval;
  144.          vals[1] = endval;
  145.          return method;
  146.       }
  147.    }
  148.  
  149.    synchronized void newImage(Thread me, int width, int height, int[] pixels) {
  150.       if (this.kicker == me) {
  151.          Image img = ((Component)this).createImage(new MemoryImageSource(width, height, ColorModel.getRGBdefault(), pixels, 0, width));
  152.          this.canvas.setImage(img);
  153.          this.kicker = null;
  154.       }
  155.    }
  156.  
  157.    public void run() {
  158.       Thread me = Thread.currentThread();
  159.       me.setPriority(4);
  160.       int width = this.canvas.size().width;
  161.       int height = this.canvas.size().height;
  162.       int[] xvals = new int[2];
  163.       int[] yvals = new int[2];
  164.       int xmethod = this.XControls.getParams(xvals);
  165.       int ymethod = this.YControls.getParams(yvals);
  166.       int[] pixels = new int[width * height];
  167.       int[] c = new int[4];
  168.       int index = 0;
  169.  
  170.       for(int j = 0; j < height; ++j) {
  171.          for(int i = 0; i < width; ++i) {
  172.             c[0] = c[1] = c[2] = 0;
  173.             c[3] = 255;
  174.             if (xmethod < ymethod) {
  175.                this.applymethod(c, xmethod, i, width, xvals);
  176.                this.applymethod(c, ymethod, j, height, yvals);
  177.             } else {
  178.                this.applymethod(c, ymethod, j, height, yvals);
  179.                this.applymethod(c, xmethod, i, width, xvals);
  180.             }
  181.  
  182.             pixels[index++] = c[3] << 24 | c[0] << 16 | c[1] << 8 | c[2];
  183.             if (this.kicker != me) {
  184.                return;
  185.             }
  186.          }
  187.       }
  188.  
  189.       this.newImage(me, width, height, pixels);
  190.    }
  191.  
  192.    public void init() {
  193.       int[] xvals = new int[2];
  194.       int[] yvals = new int[2];
  195.  
  196.       String xspec;
  197.       try {
  198.          xspec = ((Applet)this).getParameter("xaxis");
  199.       } catch (Exception var9) {
  200.          xspec = null;
  201.       }
  202.  
  203.       String yspec;
  204.       try {
  205.          yspec = ((Applet)this).getParameter("yaxis");
  206.       } catch (Exception var8) {
  207.          yspec = null;
  208.       }
  209.  
  210.       if (xspec == null) {
  211.          xspec = "red";
  212.       }
  213.  
  214.       if (yspec == null) {
  215.          yspec = "blue";
  216.       }
  217.  
  218.       int xmethod = this.colormethod(xspec, xvals);
  219.       int ymethod = this.colormethod(yspec, yvals);
  220.       ((Container)this).setLayout(new BorderLayout());
  221.       this.XControls = new DitherControls(this, xvals[0], xvals[1], xmethod, false);
  222.       this.YControls = new DitherControls(this, yvals[0], yvals[1], ymethod, true);
  223.       this.YControls.addRenderButton();
  224.       ((Container)this).add("North", this.XControls);
  225.       ((Container)this).add("South", this.YControls);
  226.       ((Container)this).add("Center", this.canvas = new DitherCanvas());
  227.    }
  228.  
  229.    public static void main(String[] args) {
  230.       Frame f = new Frame("ArcTest");
  231.       DitherTest ditherTest = new DitherTest();
  232.       ditherTest.init();
  233.       ((Container)f).add("Center", ditherTest);
  234.       ((Window)f).pack();
  235.       ((Window)f).show();
  236.       ditherTest.start();
  237.    }
  238. }
  239.