ColorConvert.java
Upload User: cdlibang
Upload Date: 2016-07-17
Package Size: 774k
Code Size: 5k
Category:

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)ColorConvert.java 1.28 99/04/23
  3.  *
  4.  * Copyright (c) 1998, 1999 by 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. package demos.Colors;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.awt.geom.Rectangle2D;
  34. import java.awt.color.ColorSpace;
  35. import java.awt.image.BufferedImage;
  36. import java.awt.image.ColorConvertOp;
  37. import java.awt.font.TextLayout;
  38. import java.awt.font.FontRenderContext;
  39. import DemoSurface;
  40. import DemoPanel;
  41. /**
  42.  * ColorConvertOp a ColorSpace.TYPE_RGB BufferedImage to a ColorSpace.CS_GRAY 
  43.  * BufferedImage.
  44.  */
  45. public class ColorConvert extends DemoSurface {
  46.     private static Image img;
  47.     private static Color colors[] = { Color.red, Color.pink, Color.orange, 
  48.             Color.yellow, Color.green, Color.magenta, Color.cyan, Color.blue};
  49.     public ColorConvert() {
  50.         setBackground(Color.white);
  51.         img = getImage("clouds.jpg");
  52.     }
  53.     public void drawDemo(int w, int h, Graphics2D g2) {
  54.         int iw = img.getWidth(this);
  55.         int ih = img.getHeight(this);
  56.         FontRenderContext frc = g2.getFontRenderContext();
  57.         Font font = g2.getFont();
  58.         g2.setColor(Color.black);
  59.         TextLayout tl = new TextLayout("ColorConvertOp RGB->GRAY", font, frc);
  60.         tl.draw(g2, (float) (w/2-tl.getBounds().getWidth()/2), 
  61.                             tl.getAscent()+tl.getLeading());
  62.         BufferedImage srcImg = 
  63.             new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
  64.         Graphics2D srcG = srcImg.createGraphics();
  65.         RenderingHints rhs = g2.getRenderingHints();
  66.         srcG.setRenderingHints(rhs);
  67.         srcG.drawImage(img, 0, 0, null);
  68.         String s = "JavaColor";
  69.         Font f = new Font("serif", Font.BOLD, iw/6);
  70.         tl = new TextLayout(s, f, frc);
  71.         Rectangle2D tlb = tl.getBounds();
  72.         char[] chars = s.toCharArray();
  73.         float charWidth = 0.0f;
  74.         int rw = iw/chars.length;
  75.         int rh = ih/chars.length;
  76.         for (int i = 0; i < chars.length; i++) {
  77.             tl = new TextLayout(String.valueOf(chars[i]), f, frc);
  78.             Shape shape = tl.getOutline(null);
  79.             srcG.setColor(colors[i%colors.length]);
  80.             tl.draw(srcG, (float) (iw/2-tlb.getWidth()/2+charWidth), 
  81.                         (float) (ih/2+tlb.getHeight()/2));
  82.             charWidth += (float) shape.getBounds().getWidth();
  83.             srcG.fillRect(i*rw, ih-rh, rw, rh);
  84.             srcG.setColor(colors[colors.length-1-i%colors.length]);
  85.             srcG.fillRect(i*rw, 0, rw, rh);
  86.         }
  87.         ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
  88.         ColorConvertOp theOp = new ColorConvertOp(cs, rhs);
  89.         BufferedImage dstImg = 
  90.                 new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
  91.         theOp.filter(srcImg, dstImg);
  92.         g2.drawImage(srcImg, 10, 20, w/2-20, h-30, null);
  93.         g2.drawImage(dstImg, w/2+10, 20, w/2-20, h-30, null);
  94.     }
  95.     public static void main(String s[]) {
  96.         Frame f = new Frame("Java2D Demo - ColorConvert");
  97.         f.addWindowListener(new WindowAdapter() {
  98.             public void windowClosing(WindowEvent e) {System.exit(0);}
  99.         });
  100.         f.add("Center", new DemoPanel(new ColorConvert()));
  101.         f.pack();
  102.         f.setSize(new Dimension(400,300));
  103.         f.show();
  104.     }
  105. }