home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / DEMO / APPLETS / DitherTest / DitherTest.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  8.9 KB  |  349 lines

  1. /*
  2.  * @(#)DitherTest.java    1.2 97/08/01
  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.event.*;
  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.getSize().width;
  211.     int height = canvas.getSize().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.   public String getAppletInfo() {
  256.     return "An interactive demonstration of dithering.";
  257.   }
  258.   
  259.   public String[][] getParameterInfo() {
  260.     String[][] info = {
  261.       {"xaxis", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, WHITE, YELLOW, GRAY, DARKGRAY}", "The color of the Y axis.  Default is RED."}, 
  262.       {"yaxis", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, WHITE, YELLOW, GRAY, DARKGRAY}", "The color of the X axis.  Default is BLUE."}
  263.     };
  264.     return info;
  265.   }
  266. }
  267.  
  268. class DitherCanvas extends Canvas {
  269.     Image img;
  270.     static String calcString = "Calculating...";
  271.  
  272.     public void paint(Graphics g) {
  273.     int w = getSize().width;
  274.     int h = getSize().height;
  275.     if (img == null) {
  276.         super.paint(g);
  277.         g.setColor(Color.black);
  278.         FontMetrics fm = g.getFontMetrics();
  279.         int x = (w - fm.stringWidth(calcString))/2;
  280.         int y = h/2;
  281.         g.drawString(calcString, x, y);
  282.     } else {
  283.         g.drawImage(img, 0, 0, w, h, this);
  284.     }
  285.     }
  286.  
  287.     public Dimension getMinimumSize() {
  288.     return new Dimension(20, 20);
  289.     }
  290.  
  291.     public Dimension getPreferredSize() {
  292.     return new Dimension(200, 200);
  293.     }
  294.  
  295.     public Image getImage() {
  296.     return img;
  297.     }
  298.  
  299.     public void setImage(Image img) {
  300.     this.img = img;
  301.     repaint();
  302.     }
  303. }
  304.  
  305. class DitherControls extends Panel implements ActionListener {
  306.     TextField start;
  307.     TextField end;
  308.     Button button;
  309.     Choice choice;
  310.     DitherTest applet;
  311.  
  312.     static LayoutManager dcLayout = new FlowLayout(FlowLayout.CENTER, 10, 5);
  313.  
  314.     public DitherControls(DitherTest app, int s, int e, int type,
  315.               boolean vertical) {
  316.     applet = app;
  317.     setLayout(dcLayout);
  318.     add(new Label(vertical ? "Vertical" : "Horizontal"));
  319.     add(choice = new Choice());
  320.     choice.addItem("Noop");
  321.     choice.addItem("Red");
  322.     choice.addItem("Green");
  323.     choice.addItem("Blue");
  324.     choice.addItem("Alpha");
  325.     choice.addItem("Saturation");
  326.     choice.select(type);
  327.     add(start = new TextField(Integer.toString(s), 4));
  328.     add(end = new TextField(Integer.toString(e), 4));
  329.     }
  330.  
  331.     public void addRenderButton() {
  332.     add(button = new Button("New Image"));
  333.     button.addActionListener(this);
  334.     }
  335.  
  336.     public int getParams(int vals[]) {
  337.     vals[0] = Integer.parseInt(start.getText());
  338.     vals[1] = Integer.parseInt(end.getText());
  339.     return choice.getSelectedIndex();
  340.     }
  341.  
  342.   public void actionPerformed(ActionEvent e) {
  343.     if (e.getSource() == button) {
  344.       applet.restart();
  345.     }
  346.   }
  347.  
  348. }
  349.