home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / SAMPLES.BIN / DitherTest.java < prev    next >
Text File  |  1997-10-27  |  9KB  |  338 lines

  1. /*
  2.  * @(#)DitherTest.java    1.1 97/03/20
  3.  *
  4.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.applet.Applet;
  32. import java.awt.*;
  33. import java.awt.image.ColorModel;
  34. import java.awt.image.MemoryImageSource;
  35. import java.lang.InterruptedException;
  36.  
  37. public class DitherTest extends Applet implements Runnable {
  38.     final static int NOOP = 0;
  39.     final static int RED = 1;
  40.     final static int GREEN = 2;
  41.     final static int BLUE = 3;
  42.     final static int ALPHA = 4;
  43.     final static int SATURATION = 5;
  44.  
  45.     ThreadGroup appletThreadGroup;
  46.     Thread kicker;
  47.  
  48.     DitherControls XControls;
  49.     DitherControls YControls;
  50.     DitherCanvas canvas;
  51.  
  52.     public void init() {
  53.     String xspec, yspec;
  54.     int xvals[] = new int[2];
  55.     int yvals[] = new int[2];
  56.  
  57.     try {
  58.         xspec = getParameter("xaxis");
  59.     } catch (Exception e) {
  60.         xspec = null;
  61.     }
  62.     try {
  63.         yspec = getParameter("yaxis");
  64.     } catch (Exception e) {
  65.         yspec = null;
  66.     }
  67.     if (xspec == null) xspec = "red";
  68.     if (yspec == null) yspec = "blue";
  69.     int xmethod = colormethod(xspec, xvals);
  70.     int ymethod = colormethod(yspec, yvals);
  71.  
  72.     setLayout(new BorderLayout());
  73.     XControls = new DitherControls(this, xvals[0], xvals[1],
  74.                        xmethod, false);
  75.     YControls = new DitherControls(this, yvals[0], yvals[1],
  76.                        ymethod, true);
  77.     YControls.addRenderButton();
  78.     add("North", XControls);
  79.     add("South", YControls);
  80.     add("Center", canvas = new DitherCanvas());
  81.  
  82.     appletThreadGroup = Thread.currentThread().getThreadGroup();
  83.     }
  84.  
  85.     public synchronized void start() {
  86.     if (canvas.getImage() == null) {
  87.         kicker = new Thread(appletThreadGroup, this);
  88.         kicker.start();
  89.     }
  90.     }
  91.  
  92.     public synchronized void stop() {
  93.     try {
  94.         if (kicker != null) {
  95.         kicker.stop();
  96.         }
  97.     } catch (Exception e) {
  98.     }
  99.     kicker = null;
  100.     }
  101.  
  102.     public void restart() {
  103.     stop();
  104.     canvas.setImage(null);
  105.     start();
  106.     }
  107.  
  108.     public static void main(String args[]) {
  109.     Frame f = new Frame("DitherTest");
  110.     DitherTest    ditherTest = new DitherTest();
  111.  
  112.     ditherTest.init();
  113.  
  114.     f.add("Center", ditherTest);
  115.     f.pack();
  116.     f.show();
  117.  
  118.     ditherTest.start();
  119.     }
  120.  
  121.     int colormethod(String s, int vals[]) {
  122.     int method = NOOP;
  123.  
  124.     if (s == null)
  125.         s = "";
  126.  
  127.     String lower = s.toLowerCase();
  128.     int len = 0;
  129.     if (lower.startsWith("red")) {
  130.         method = RED;
  131.         lower = lower.substring(3);
  132.     } else if (lower.startsWith("green")) {
  133.         method = GREEN;
  134.         lower = lower.substring(5);
  135.     } else if (lower.startsWith("blue")) {
  136.         method = BLUE;
  137.         lower = lower.substring(4);
  138.     } else if (lower.startsWith("alpha")) {
  139.         method = ALPHA;
  140.         lower = lower.substring(4);
  141.     } else if (lower.startsWith("saturation")) {
  142.         method = SATURATION;
  143.         lower = lower.substring(10);
  144.     }
  145.  
  146.     if (method == NOOP) {
  147.         vals[0] = 0;
  148.         vals[1] = 0;
  149.         return method;
  150.     }
  151.  
  152.     int begval = 0;
  153.     int endval = 255;
  154.  
  155.     try {
  156.         int dash = lower.indexOf('-');
  157.         if (dash < 0) {
  158.         begval = endval = Integer.parseInt(lower);
  159.         } else {
  160.         begval = Integer.parseInt(lower.substring(0, dash));
  161.         endval = Integer.parseInt(lower.substring(dash+1));
  162.         }
  163.     } catch (Exception e) {
  164.     }
  165.  
  166.     if (begval < 0) begval = 0;
  167.     if (endval < 0) endval = 0;
  168.     if (begval > 255) begval = 255;
  169.     if (endval > 255) endval = 255;
  170.  
  171.     vals[0] = begval;
  172.     vals[1] = endval;
  173.  
  174.     return method;
  175.     }
  176.  
  177.     void applymethod(int c[], int method, int step, int total, int vals[]) {
  178.     if (method == NOOP)
  179.         return;
  180.     int val = ((total < 2)
  181.            ? vals[0]
  182.            : vals[0] + ((vals[1] - vals[0]) * step / (total - 1)));
  183.     switch (method) {
  184.     case RED:
  185.         c[0] = val;
  186.         break;
  187.     case GREEN:
  188.         c[1] = val;
  189.         break;
  190.     case BLUE:
  191.         c[2] = val;
  192.         break;
  193.     case ALPHA:
  194.         c[3] = val;
  195.         break;
  196.     case SATURATION:
  197.         int max = Math.max(Math.max(c[0], c[1]), c[2]);
  198.         int min = max * (255 - val) / 255;
  199.         if (c[0] == 0) c[0] = min;
  200.         if (c[1] == 0) c[1] = min;
  201.         if (c[2] == 0) c[2] = min;
  202.         break;
  203.     }
  204.     }
  205.  
  206.     public void run() {
  207.     Thread me = Thread.currentThread();
  208.     me.setPriority(3);
  209.     int width = canvas.size().width;
  210.     int height = canvas.size().height;
  211.     int xvals[] = new int[2];
  212.     int yvals[] = new int[2];
  213.     int xmethod = XControls.getParams(xvals);
  214.     int ymethod = YControls.getParams(yvals);
  215.     int pixels[] = new int[width * height];
  216.     int c[] = new int[4];
  217.     int index = 0;
  218.     for (int j = 0; j < height; j++) {
  219.         for (int i = 0; i < width; i++) {
  220.         c[0] = c[1] = c[2] = 0;
  221.         c[3] = 255;
  222.         if (xmethod < ymethod) {
  223.             applymethod(c, xmethod, i, width, xvals);
  224.             applymethod(c, ymethod, j, height, yvals);
  225.         } else {
  226.             applymethod(c, ymethod, j, height, yvals);
  227.             applymethod(c, xmethod, i, width, xvals);
  228.         }
  229.         pixels[index++] = ((c[3] << 24) |
  230.                    (c[0] << 16) |
  231.                    (c[1] << 8) |
  232.                    (c[2] << 0));
  233.         if (kicker != me) {
  234.             return;
  235.         }
  236.         }
  237.     }
  238.     newImage(me, width, height, pixels);
  239.     }
  240.  
  241.     synchronized void newImage(Thread me, int width, int height,
  242.                    int pixels[]) {
  243.     if (kicker != me) {
  244.         return;
  245.     }
  246.     Image img;
  247.     img = createImage(new MemoryImageSource(width, height,
  248.                         ColorModel.getRGBdefault(),
  249.                         pixels, 0, width));
  250.     canvas.setImage(img);
  251.     kicker = null;
  252.     }
  253. }
  254.  
  255. class DitherCanvas extends Canvas {
  256.     Image img;
  257.     static String calcString = "Calculating...";
  258.  
  259.     public void paint(Graphics g) {
  260.     int w = size().width;
  261.     int h = size().height;
  262.     if (img == null) {
  263.         super.paint(g);
  264.         g.setColor(Color.black);
  265.         FontMetrics fm = g.getFontMetrics();
  266.         int x = (w - fm.stringWidth(calcString))/2;
  267.         int y = h/2;
  268.         g.drawString(calcString, x, y);
  269.     } else {
  270.         g.drawImage(img, 0, 0, w, h, this);
  271.     }
  272.     }
  273.  
  274.     public Dimension minimumSize() {
  275.     return new Dimension(20, 20);
  276.     }
  277.  
  278.     public Dimension preferredSize() {
  279.     return new Dimension(200, 200);
  280.     }
  281.  
  282.     public Image getImage() {
  283.     return img;
  284.     }
  285.  
  286.     public void setImage(Image img) {
  287.     this.img = img;
  288.     repaint();
  289.     }
  290. }
  291.  
  292. class DitherControls extends Panel {
  293.     TextField start;
  294.     TextField end;
  295.     Button button;
  296.     Choice choice;
  297.     DitherTest applet;
  298.  
  299.     static LayoutManager dcLayout = new FlowLayout(FlowLayout.CENTER, 10, 5);
  300.  
  301.     public DitherControls(DitherTest app, int s, int e, int type,
  302.               boolean vertical) {
  303.     applet = app;
  304.     setLayout(dcLayout);
  305.     add(new Label(vertical ? "Vertical" : "Horizontal"));
  306.     add(choice = new Choice());
  307.     choice.addItem("Noop");
  308.     choice.addItem("Red");
  309.     choice.addItem("Green");
  310.     choice.addItem("Blue");
  311.     choice.addItem("Alpha");
  312.     choice.addItem("Saturation");
  313.     choice.select(type);
  314.     add(start = new TextField(Integer.toString(s), 4));
  315.     add(end = new TextField(Integer.toString(e), 4));
  316.     }
  317.  
  318.     public void addRenderButton() {
  319.     add(button = new Button("New Image"));
  320.     }
  321.  
  322.     public int getParams(int vals[]) {
  323.     vals[0] = Integer.parseInt(start.getText());
  324.     vals[1] = Integer.parseInt(end.getText());
  325.     return choice.getSelectedIndex();
  326.     }
  327.  
  328.     public boolean action(Event ev, Object arg) {
  329.     if (ev.target instanceof Button) {
  330.         applet.restart();
  331.  
  332.         return true;
  333.     }
  334.  
  335.     return false;
  336.     }
  337. }
  338.