home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / DitherTest.java < prev    next >
Text File  |  1997-07-30  |  9KB  |  339 lines

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