home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / vjplusb / msdev / samples / sun / imagetest / imagetest.java < prev    next >
Encoding:
Java Source  |  1996-07-10  |  8.7 KB  |  334 lines

  1. /*
  2.  * @(#)ImageTest.java    1.1 95/09/08
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31. import java.awt.*;
  32. import java.awt.image.*;
  33. //import sun.awt.image.URLImageSource;
  34. import java.applet.Applet;
  35.  
  36. public class imagetest extends Applet {
  37.     public void init() {
  38.     setLayout(new BorderLayout());
  39.     add("Center", new ImagePanel(this));
  40.     add("North", new ImageHelp());
  41.     reshape(0, 0, 800, 600);
  42.     show();
  43.     }
  44. }
  45.  
  46. class ImageHelp extends Panel {
  47.     public ImageHelp() {
  48.     setLayout(new GridLayout(0, 2));
  49.     add(new Label("Move the images using the arrow keys",
  50.               Label.CENTER));
  51.     add(new Label("Resize the images using the PgUp/PgDn keys",
  52.               Label.CENTER));
  53.     add(new Label("Toggle a red/blue color filter using the Home key",
  54.               Label.CENTER));
  55.     add(new Label("Change the alpha using the shifted PgUp/PgDn keys",
  56.               Label.CENTER));
  57.     }
  58. }
  59.  
  60. class ImagePanel extends Panel {
  61.     Applet applet;
  62.  
  63.     public ImagePanel(Applet app) {
  64.     applet = app;
  65.     setLayout(new BorderLayout());
  66.     Panel grid = new Panel();
  67.     grid.setLayout(new GridLayout(0, 2));
  68.     add("Center", grid);
  69.     grid.add(new ImageCanvas(applet, makeDitherImage(), 0.5));
  70.     Image joe = applet.getImage(applet.getDocumentBase(),
  71.                     "graphics/joe.surf.yellow.small.gif");
  72.     grid.add(new ImageCanvas(applet, joe, 1.0));
  73.     reshape(0, 0, 20, 20);
  74.     }
  75.  
  76.     Image makeDitherImage() {
  77.     int w = 100;
  78.     int h = 100;
  79.     int pix[] = new int[w * h];
  80.     int index = 0;
  81.     for (int y = 0; y < h; y++) {
  82.         int red = (y * 255) / (h - 1);
  83.         for (int x = 0; x < w; x++) {
  84.         int blue = (x * 255) / (w - 1);
  85.         pix[index++] = (255 << 24) | (red << 16) | blue;
  86.         }
  87.     }
  88.     return applet.createImage(new MemoryImageSource(w, h, pix, 0, w));
  89.     }
  90. }
  91.  
  92. class ImageCanvas extends Canvas implements ImageObserver {
  93.     double     hmult = 0;
  94.     int        xadd = 0;
  95.     int        yadd = 0;
  96.     int        imgw = -1;
  97.     int        imgh = -1;
  98.     int        scalew = -1;
  99.     int        scaleh = -1;
  100.     boolean    focus = false;
  101.     boolean    usefilter = false;
  102.     static final int numalphas = 8;
  103.     int        alpha = numalphas - 1;
  104.     Image    imagevariants[] = new Image[numalphas * 2];
  105.     ImageFilter colorfilter;
  106.     Image    origimage;
  107.     Image    curimage;
  108.     Applet    applet;
  109.  
  110.     public ImageCanvas(Applet app, Image img, double mult) {
  111.     applet = app;
  112.     origimage = img;
  113.     imagevariants[numalphas - 1] = origimage;
  114.     hmult = mult;
  115.     pickImage();
  116.     reshape(0, 0, 100, 100);
  117.     }
  118.  
  119.     public void gotFocus() {
  120.     focus = true;
  121.     repaint();
  122.     }
  123.  
  124.     public void lostFocus() {
  125.     focus = false;
  126.     repaint();
  127.     }
  128.  
  129.     public void paint(Graphics g) {
  130.     Rectangle r = bounds();
  131.     int hlines = r.height / 10;
  132.     int vlines = r.width / 10;
  133.  
  134.     if (focus) {
  135.         g.setColor(Color.red);
  136.     } else {
  137.         g.setColor(Color.darkGray);
  138.     }
  139.     g.drawRect(0, 0, r.width-1, r.height-1);
  140.     g.drawLine(0, 0, r.width, r.height);
  141.     g.drawLine(r.width, 0, 0, r.height);
  142.     g.drawLine(0, r.height / 2, r.width, r.height / 2);
  143.     g.drawLine(r.width / 2, 0, r.width / 2, r.height);
  144.     if (imgw < 0) {
  145.         imgw = curimage.getWidth(this);
  146.         imgh = curimage.getHeight(this);
  147.         if (imgw < 0 || imgh < 0) {
  148.         return;
  149.         }
  150.     }
  151.     if (scalew < 0) {
  152.         scalew = (int) (imgw * hmult);
  153.         scaleh = (int) (imgh * hmult);
  154.     }
  155.     if (imgw != scalew || imgh != scaleh) {
  156.         g.drawImage(curimage, xadd, yadd, scalew, scaleh, this);
  157.     } else {
  158.         g.drawImage(curimage, xadd, yadd, this);
  159.     }
  160.  
  161.     }
  162.  
  163.     static final long updateRate = 100;
  164.  
  165.     public synchronized boolean imageUpdate(Image img, int infoflags,
  166.                         int x, int y, int w, int h) {
  167.     if (img != curimage) {
  168.         return false;
  169.     }
  170.     boolean ret = true;
  171.     boolean dopaint = false;
  172.     long updatetime = 0;
  173.     if ((infoflags & WIDTH) != 0) {
  174.         imgw = w;
  175.         dopaint = true;
  176.     }
  177.     if ((infoflags & HEIGHT) != 0) {
  178.         imgh = h;
  179.         dopaint = true;
  180.     }
  181.     if ((infoflags & (FRAMEBITS | ALLBITS)) != 0) {
  182.         dopaint = true;
  183.         ret = false;
  184.     } else if ((infoflags & SOMEBITS) != 0) {
  185.         dopaint = true;
  186.         updatetime = updateRate;
  187.     }
  188.     if ((infoflags & ERROR) != 0) {
  189.         ret = false;
  190.     }
  191.     if (dopaint) {
  192.         repaint(updatetime);
  193.     }
  194.     return ret;
  195.     }
  196.  
  197.     public synchronized Image pickImage() {
  198.     int index = alpha;
  199.     if (usefilter) {
  200.         index += numalphas;
  201.     }
  202.     Image choice = imagevariants[index];
  203.     if (choice == null) {
  204.         choice = imagevariants[alpha];
  205.         if (choice == null) {
  206.         int alphaval = (alpha * 255) / (numalphas - 1);
  207.         ImageFilter imgf = new AlphaFilter(alphaval);
  208.         ImageProducer src = origimage.getSource();
  209.         src = new FilteredImageSource(src, imgf);
  210.         choice = applet.createImage(src);
  211.         imagevariants[alpha] = choice;
  212.         }
  213.         if (usefilter) {
  214.         if (colorfilter == null) {
  215.             colorfilter = new RedBlueSwapFilter();
  216.         }
  217.         ImageProducer src = choice.getSource();
  218.         src = new FilteredImageSource(src, colorfilter);
  219.         choice = applet.createImage(src);
  220.         }
  221.         imagevariants[index] = choice;
  222.     }
  223.     curimage = choice;
  224.     return choice;
  225.     }
  226.  
  227.     public synchronized boolean handleEvent(Event e) {
  228.     switch (e.id) {
  229.       case Event.KEY_ACTION:
  230.       case Event.KEY_PRESS:
  231.         switch (e.key) {
  232.           case Event.HOME:
  233.         usefilter = !usefilter;
  234.         pickImage();
  235.         repaint();
  236.         return true;
  237.           case Event.UP:
  238.         yadd -= 5;
  239.         repaint();
  240.         return true;
  241.           case Event.DOWN:
  242.         yadd += 5;
  243.         repaint();
  244.         return true;
  245.           case Event.RIGHT:
  246.           case 'r':
  247.         xadd += 5;
  248.         repaint();
  249.         return true;
  250.           case Event.LEFT:
  251.         xadd -= 5;
  252.         repaint();
  253.         return true;
  254.           case Event.PGUP:
  255.         if ((e.modifiers & Event.SHIFT_MASK) != 0) {
  256.             if (++alpha > numalphas - 1) {
  257.             alpha = numalphas - 1;
  258.             }
  259.             pickImage();
  260.         } else {
  261.             hmult *= 1.2;
  262.         }
  263.         scalew = scaleh = -1;
  264.         repaint();
  265.         return true;
  266.           case Event.PGDN:
  267.         if ((e.modifiers & Event.SHIFT_MASK) != 0) {
  268.             if (--alpha < 0) {
  269.             alpha = 0;
  270.             }
  271.             pickImage();
  272.         } else {
  273.             hmult /= 1.2;
  274.         }
  275.         scalew = scaleh = -1;
  276.         repaint();
  277.         return true;
  278.           default:
  279.         return false;
  280.         }
  281.  
  282.       default:
  283.         return false;
  284.     }
  285.     }
  286.  
  287. }
  288.  
  289. class RedBlueSwapFilter extends RGBImageFilter {
  290.     public RedBlueSwapFilter() {
  291.     canFilterIndexColorModel = true;
  292.     }
  293.  
  294.     public void setColorModel(ColorModel model) {
  295.     if (model instanceof DirectColorModel) {
  296.         DirectColorModel dcm = (DirectColorModel) model;
  297.         int rm = dcm.getRedMask();
  298.         int gm = dcm.getGreenMask();
  299.         int bm = dcm.getBlueMask();
  300.         int am = dcm.getAlphaMask();
  301.         int bits = dcm.getPixelSize();
  302.         dcm = new DirectColorModel(bits, bm, gm, rm, am);
  303.         substituteColorModel(model, dcm);
  304.         consumer.setColorModel(dcm);
  305.     } else {
  306.         super.setColorModel(model);
  307.     }
  308.     }
  309.  
  310.     public int filterRGB(int x, int y, int rgb) {
  311.     return ((rgb & 0xff00ff00)
  312.         | ((rgb & 0xff0000) >> 16)
  313.         | ((rgb & 0xff) << 16));
  314.     }
  315. }
  316.  
  317. class AlphaFilter extends RGBImageFilter {
  318.     ColorModel origmodel;
  319.     ColorModel newmodel;
  320.  
  321.     int alphaval;
  322.  
  323.     public AlphaFilter(int alpha) {
  324.     alphaval = alpha;
  325.     canFilterIndexColorModel = true;
  326.     }
  327.  
  328.     public int filterRGB(int x, int y, int rgb) {
  329.     int alpha = (rgb >> 24) & 0xff;
  330.     alpha = alpha * alphaval / 255;
  331.     return ((rgb & 0x00ffffff) | (alpha << 24));
  332.     }
  333. }
  334.