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
  24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package java2d.demos.Clipping;
  33 
  34 
  35 import static java.awt.Color.BLACK;
  36 import static java.awt.Color.BLUE;
  37 import static java.awt.Color.CYAN;
  38 import static java.awt.Color.GRAY;
  39 import static java.awt.Color.RED;
  40 import static java.awt.Color.WHITE;
  41 import static java.awt.Color.YELLOW;
  42 import java.awt.BasicStroke;
  43 import java.awt.Component;
  44 import java.awt.Dimension;
  45 import java.awt.Font;
  46 import java.awt.GradientPaint;
  47 import java.awt.Graphics2D;
  48 import java.awt.Image;
  49 import java.awt.Rectangle;
  50 import java.awt.Shape;
  51 import java.awt.TexturePaint;
  52 import java.awt.event.ActionEvent;
  53 import java.awt.event.ActionListener;
  54 import java.awt.font.FontRenderContext;
  55 import java.awt.font.TextLayout;
  56 import java.awt.geom.AffineTransform;
  57 import java.awt.geom.Line2D;
  58 import java.awt.image.BufferedImage;
  59 import java2d.ControlsSurface;
  60 import java2d.CustomControls;
  61 import javax.swing.AbstractButton;
  62 import javax.swing.JToggleButton;
  63 import javax.swing.JToolBar;
  64 
  65 
  66 /**
  67  * Clipping an image, lines, text, texture and gradient with text.
  68  */
  69 @SuppressWarnings("serial")
  70 public class Text extends ControlsSurface {
  71 
  72     /**
  73      *
  74      */
  75     static Image img;
  76     static TexturePaint texturePaint;
  77 
  78     static {
  79         BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
  80         Graphics2D big = bi.createGraphics();
  81         big.setBackground(YELLOW);
  82         big.clearRect(0, 0, 5, 5);
  83         big.setColor(RED);
  84         big.fillRect(0, 0, 3, 3);
  85         texturePaint = new TexturePaint(bi, new Rectangle(0, 0, 5, 5));
  86     }
  87     private String clipType = "Lines";
  88     protected boolean doClip = true;
  89 
  90     public Text() {
  91         setBackground(WHITE);
  92         img = getImage("clouds.jpg");
  93         setControls(new Component[] { new DemoControls(this) });
  94     }
  95 
  96     @Override
  97     public void render(int w, int h, Graphics2D g2) {
  98 
  99         FontRenderContext frc = g2.getFontRenderContext();
 100         Font f = new Font(Font.SANS_SERIF, Font.BOLD, 32);
 101         String s = "JDK";
 102         TextLayout tl = new TextLayout(s, f, frc);
 103         double sw = tl.getBounds().getWidth();
 104         double sh = tl.getBounds().getHeight();
 105         double sx = (w - 40) / sw;
 106         double sy = (h - 40) / sh;
 107         AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy);
 108         Shape shape = tl.getOutline(Tx);
 109         sw = shape.getBounds().getWidth();
 110         sh = shape.getBounds().getHeight();
 111         Tx =
 112                 AffineTransform.getTranslateInstance(w / 2 - sw / 2, h / 2 + sh
 113                 / 2);
 114         shape = Tx.createTransformedShape(shape);
 115         Rectangle r = shape.getBounds();
 116 
 117         if (doClip) {
 118             g2.clip(shape);
 119         }
 120 
 121         if (clipType.equals("Lines")) {
 122             g2.setColor(BLACK);
 123             g2.fill(r);
 124             g2.setColor(YELLOW);
 125             g2.setStroke(new BasicStroke(1.5f));
 126             for (int j = r.y; j < r.y + r.height; j = j + 3) {
 127                 Line2D line = new Line2D.Float(r.x, j,
 128                         (r.x + r.width), j);
 129                 g2.draw(line);
 130             }
 131         } else if (clipType.equals("Image")) {
 132             g2.drawImage(img, r.x, r.y, r.width, r.height, null);
 133         } else if (clipType.equals("TP")) {
 134             g2.setPaint(texturePaint);
 135             g2.fill(r);
 136         } else if (clipType.equals("GP")) {
 137             g2.setPaint(new GradientPaint(0, 0, BLUE, w, h, YELLOW));
 138             g2.fill(r);
 139         } else if (clipType.equals("Text")) {
 140             g2.setColor(BLACK);
 141             g2.fill(shape.getBounds());
 142             g2.setColor(CYAN);
 143             f = new Font(Font.SERIF, Font.BOLD, 10);
 144             tl = new TextLayout("OpenJDK", f, frc);
 145             sw = tl.getBounds().getWidth();
 146 
 147             int x = r.x;
 148             int y = (int) (r.y + tl.getAscent());
 149             sh = r.y + r.height;
 150             while (y < sh) {
 151                 tl.draw(g2, x, y);
 152                 if ((x += (int) sw) > (r.x + r.width)) {
 153                     x = r.x;
 154                     y += (int) tl.getAscent();
 155                 }
 156             }
 157         }
 158         g2.setClip(new Rectangle(0, 0, w, h));
 159 
 160         g2.setColor(GRAY);
 161         g2.draw(shape);
 162     }
 163 
 164     public static void main(String[] s) {
 165         createDemoFrame(new Text());
 166     }
 167 
 168 
 169     @SuppressWarnings("serial")
 170     static final class DemoControls extends CustomControls implements
 171             ActionListener {
 172 
 173         Text demo;
 174         JToolBar toolbar;
 175 
 176         public DemoControls(Text demo) {
 177             super(demo.name);
 178             this.demo = demo;
 179             add(toolbar = new JToolBar());
 180             toolbar.setFloatable(false);
 181             addTool("Clip", true);
 182             addTool("Lines", true);
 183             addTool("Image", false);
 184             addTool("TP", false);
 185             addTool("GP", false);
 186             addTool("Text", false);
 187         }
 188 
 189         public void addTool(String str, boolean state) {
 190             JToggleButton b =
 191                     (JToggleButton) toolbar.add(new JToggleButton(str));
 192             b.setFocusPainted(false);
 193             b.setSelected(state);
 194             b.addActionListener(this);
 195             int width = b.getPreferredSize().width;
 196             Dimension prefSize = new Dimension(width, 21);
 197             b.setPreferredSize(prefSize);
 198             b.setMaximumSize(prefSize);
 199             b.setMinimumSize(prefSize);
 200         }
 201 
 202         @Override
 203         public void actionPerformed(ActionEvent e) {
 204             if (e.getSource().equals(toolbar.getComponentAtIndex(0))) {
 205                 JToggleButton b = (JToggleButton) e.getSource();
 206                 demo.doClip = b.isSelected();
 207             } else {
 208                 for (Component comp : toolbar.getComponents()) {
 209                     ((JToggleButton) comp).setSelected(false);
 210                 }
 211                 JToggleButton b = (JToggleButton) e.getSource();
 212                 b.setSelected(true);
 213                 demo.clipType = b.getText();
 214             }
 215             demo.repaint();
 216         }
 217 
 218         @Override
 219         public Dimension getPreferredSize() {
 220             return new Dimension(200, 40);
 221         }
 222 
 223         @Override
 224         @SuppressWarnings("SleepWhileHoldingLock")
 225         public void run() {
 226             try {
 227                 Thread.sleep(1111);
 228             } catch (Exception e) {
 229                 return;
 230             }
 231             Thread me = Thread.currentThread();
 232             while (thread == me) {
 233                 for (int i = 1; i < toolbar.getComponentCount() - 1; i++) {
 234                     ((AbstractButton) toolbar.getComponentAtIndex(i)).doClick();
 235                     try {
 236                         Thread.sleep(4444);
 237                     } catch (InterruptedException e) {
 238                         return;
 239                     }
 240                 }
 241             }
 242             thread = null;
 243         }
 244     } // End DemoControls
 245 } // End Text
 246