< prev index next >

src/demo/share/jfc/J2Ddemo/java2d/demos/Colors/ColorConvert.java

Print this page


   1 /*
   2  *
   3  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


  48 import java.awt.Image;
  49 import java.awt.RenderingHints;
  50 import java.awt.Shape;
  51 import java.awt.color.ColorSpace;
  52 import java.awt.font.FontRenderContext;
  53 import java.awt.font.TextLayout;
  54 import java.awt.geom.Rectangle2D;
  55 import java.awt.image.BufferedImage;
  56 import java.awt.image.ColorConvertOp;
  57 import java2d.Surface;
  58 
  59 
  60 /**
  61  * ColorConvertOp a ColorSpace.TYPE_RGB BufferedImage to a ColorSpace.CS_GRAY
  62  * BufferedImage.
  63  */
  64 @SuppressWarnings("serial")
  65 public class ColorConvert extends Surface {
  66 
  67     private static Image img;
  68     private static Color colors[] = { red, pink, orange,
  69         yellow, green, magenta, cyan, blue };
  70 
  71     public ColorConvert() {
  72         setBackground(white);
  73         img = getImage("clouds.jpg");
  74     }
  75 
  76     @Override
  77     public void render(int w, int h, Graphics2D g2) {
  78 
  79         int iw = img.getWidth(this);
  80         int ih = img.getHeight(this);
  81         FontRenderContext frc = g2.getFontRenderContext();
  82         Font font = g2.getFont();
  83         g2.setColor(black);
  84         TextLayout tl = new TextLayout("ColorConvertOp RGB->GRAY", font, frc);
  85         tl.draw(g2, (float) (w / 2 - tl.getBounds().getWidth() / 2),
  86                 tl.getAscent() + tl.getLeading());
  87 
  88         BufferedImage srcImg =


 106             srcG.setColor(colors[i % colors.length]);
 107             tl.draw(srcG, (float) (iw / 2 - tlb.getWidth() / 2 + charWidth),
 108                     (float) (ih / 2 + tlb.getHeight() / 2));
 109             charWidth += (float) shape.getBounds().getWidth();
 110             srcG.fillRect(i * rw, ih - rh, rw, rh);
 111             srcG.setColor(colors[colors.length - 1 - i % colors.length]);
 112             srcG.fillRect(i * rw, 0, rw, rh);
 113         }
 114 
 115         ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
 116         ColorConvertOp theOp = new ColorConvertOp(cs, rhs);
 117 
 118         BufferedImage dstImg =
 119                 new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
 120         theOp.filter(srcImg, dstImg);
 121 
 122         g2.drawImage(srcImg, 10, 20, w / 2 - 20, h - 30, null);
 123         g2.drawImage(dstImg, w / 2 + 10, 20, w / 2 - 20, h - 30, null);
 124     }
 125 
 126     public static void main(String s[]) {
 127         createDemoFrame(new ColorConvert());
 128     }
 129 }
   1 /*
   2  *
   3  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


  48 import java.awt.Image;
  49 import java.awt.RenderingHints;
  50 import java.awt.Shape;
  51 import java.awt.color.ColorSpace;
  52 import java.awt.font.FontRenderContext;
  53 import java.awt.font.TextLayout;
  54 import java.awt.geom.Rectangle2D;
  55 import java.awt.image.BufferedImage;
  56 import java.awt.image.ColorConvertOp;
  57 import java2d.Surface;
  58 
  59 
  60 /**
  61  * ColorConvertOp a ColorSpace.TYPE_RGB BufferedImage to a ColorSpace.CS_GRAY
  62  * BufferedImage.
  63  */
  64 @SuppressWarnings("serial")
  65 public class ColorConvert extends Surface {
  66 
  67     private static Image img;
  68     private static Color[] colors = { red, pink, orange,
  69         yellow, green, magenta, cyan, blue };
  70 
  71     public ColorConvert() {
  72         setBackground(white);
  73         img = getImage("clouds.jpg");
  74     }
  75 
  76     @Override
  77     public void render(int w, int h, Graphics2D g2) {
  78 
  79         int iw = img.getWidth(this);
  80         int ih = img.getHeight(this);
  81         FontRenderContext frc = g2.getFontRenderContext();
  82         Font font = g2.getFont();
  83         g2.setColor(black);
  84         TextLayout tl = new TextLayout("ColorConvertOp RGB->GRAY", font, frc);
  85         tl.draw(g2, (float) (w / 2 - tl.getBounds().getWidth() / 2),
  86                 tl.getAscent() + tl.getLeading());
  87 
  88         BufferedImage srcImg =


 106             srcG.setColor(colors[i % colors.length]);
 107             tl.draw(srcG, (float) (iw / 2 - tlb.getWidth() / 2 + charWidth),
 108                     (float) (ih / 2 + tlb.getHeight() / 2));
 109             charWidth += (float) shape.getBounds().getWidth();
 110             srcG.fillRect(i * rw, ih - rh, rw, rh);
 111             srcG.setColor(colors[colors.length - 1 - i % colors.length]);
 112             srcG.fillRect(i * rw, 0, rw, rh);
 113         }
 114 
 115         ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
 116         ColorConvertOp theOp = new ColorConvertOp(cs, rhs);
 117 
 118         BufferedImage dstImg =
 119                 new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
 120         theOp.filter(srcImg, dstImg);
 121 
 122         g2.drawImage(srcImg, 10, 20, w / 2 - 20, h - 30, null);
 123         g2.drawImage(dstImg, w / 2 + 10, 20, w / 2 - 20, h - 30, null);
 124     }
 125 
 126     public static void main(String[] s) {
 127         createDemoFrame(new ColorConvert());
 128     }
 129 }
< prev index next >