Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Clipping/Text.java
41175 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d.demos.Clipping;323334import static java.awt.Color.BLACK;35import static java.awt.Color.BLUE;36import static java.awt.Color.CYAN;37import static java.awt.Color.GRAY;38import static java.awt.Color.RED;39import static java.awt.Color.WHITE;40import static java.awt.Color.YELLOW;41import java.awt.BasicStroke;42import java.awt.Component;43import java.awt.Dimension;44import java.awt.Font;45import java.awt.GradientPaint;46import java.awt.Graphics2D;47import java.awt.Image;48import java.awt.Rectangle;49import java.awt.Shape;50import java.awt.TexturePaint;51import java.awt.event.ActionEvent;52import java.awt.event.ActionListener;53import java.awt.font.FontRenderContext;54import java.awt.font.TextLayout;55import java.awt.geom.AffineTransform;56import java.awt.geom.Line2D;57import java.awt.image.BufferedImage;58import java2d.ControlsSurface;59import java2d.CustomControls;60import javax.swing.AbstractButton;61import javax.swing.JToggleButton;62import javax.swing.JToolBar;636465/**66* Clipping an image, lines, text, texture and gradient with text.67*/68@SuppressWarnings("serial")69public class Text extends ControlsSurface {7071/**72*73*/74static Image img;75static TexturePaint texturePaint;7677static {78BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);79Graphics2D big = bi.createGraphics();80big.setBackground(YELLOW);81big.clearRect(0, 0, 5, 5);82big.setColor(RED);83big.fillRect(0, 0, 3, 3);84texturePaint = new TexturePaint(bi, new Rectangle(0, 0, 5, 5));85}86private String clipType = "Lines";87protected boolean doClip = true;8889public Text() {90setBackground(WHITE);91img = getImage("clouds.jpg");92setControls(new Component[] { new DemoControls(this) });93}9495@Override96public void render(int w, int h, Graphics2D g2) {9798FontRenderContext frc = g2.getFontRenderContext();99Font f = new Font(Font.SANS_SERIF, Font.BOLD, 32);100String s = "JDK";101TextLayout tl = new TextLayout(s, f, frc);102double sw = tl.getBounds().getWidth();103double sh = tl.getBounds().getHeight();104double sx = (w - 40) / sw;105double sy = (h - 40) / sh;106AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy);107Shape shape = tl.getOutline(Tx);108sw = shape.getBounds().getWidth();109sh = shape.getBounds().getHeight();110Tx =111AffineTransform.getTranslateInstance(w / 2 - sw / 2, h / 2 + sh112/ 2);113shape = Tx.createTransformedShape(shape);114Rectangle r = shape.getBounds();115116if (doClip) {117g2.clip(shape);118}119120if (clipType.equals("Lines")) {121g2.setColor(BLACK);122g2.fill(r);123g2.setColor(YELLOW);124g2.setStroke(new BasicStroke(1.5f));125for (int j = r.y; j < r.y + r.height; j = j + 3) {126Line2D line = new Line2D.Float(r.x, j,127(r.x + r.width), j);128g2.draw(line);129}130} else if (clipType.equals("Image")) {131g2.drawImage(img, r.x, r.y, r.width, r.height, null);132} else if (clipType.equals("TP")) {133g2.setPaint(texturePaint);134g2.fill(r);135} else if (clipType.equals("GP")) {136g2.setPaint(new GradientPaint(0, 0, BLUE, w, h, YELLOW));137g2.fill(r);138} else if (clipType.equals("Text")) {139g2.setColor(BLACK);140g2.fill(shape.getBounds());141g2.setColor(CYAN);142f = new Font(Font.SERIF, Font.BOLD, 10);143tl = new TextLayout("OpenJDK", f, frc);144sw = tl.getBounds().getWidth();145146int x = r.x;147int y = (int) (r.y + tl.getAscent());148sh = r.y + r.height;149while (y < sh) {150tl.draw(g2, x, y);151if ((x += (int) sw) > (r.x + r.width)) {152x = r.x;153y += (int) tl.getAscent();154}155}156}157g2.setClip(new Rectangle(0, 0, w, h));158159g2.setColor(GRAY);160g2.draw(shape);161}162163public static void main(String[] s) {164createDemoFrame(new Text());165}166167168@SuppressWarnings("serial")169static final class DemoControls extends CustomControls implements170ActionListener {171172Text demo;173JToolBar toolbar;174175public DemoControls(Text demo) {176super(demo.name);177this.demo = demo;178add(toolbar = new JToolBar());179toolbar.setFloatable(false);180addTool("Clip", true);181addTool("Lines", true);182addTool("Image", false);183addTool("TP", false);184addTool("GP", false);185addTool("Text", false);186}187188public void addTool(String str, boolean state) {189JToggleButton b =190(JToggleButton) toolbar.add(new JToggleButton(str));191b.setFocusPainted(false);192b.setSelected(state);193b.addActionListener(this);194int width = b.getPreferredSize().width;195Dimension prefSize = new Dimension(width, 21);196b.setPreferredSize(prefSize);197b.setMaximumSize(prefSize);198b.setMinimumSize(prefSize);199}200201@Override202public void actionPerformed(ActionEvent e) {203if (e.getSource().equals(toolbar.getComponentAtIndex(0))) {204JToggleButton b = (JToggleButton) e.getSource();205demo.doClip = b.isSelected();206} else {207for (Component comp : toolbar.getComponents()) {208((JToggleButton) comp).setSelected(false);209}210JToggleButton b = (JToggleButton) e.getSource();211b.setSelected(true);212demo.clipType = b.getText();213}214demo.repaint();215}216217@Override218public Dimension getPreferredSize() {219return new Dimension(200, 40);220}221222@Override223@SuppressWarnings("SleepWhileHoldingLock")224public void run() {225try {226Thread.sleep(1111);227} catch (Exception e) {228return;229}230Thread me = Thread.currentThread();231while (thread == me) {232for (int i = 1; i < toolbar.getComponentCount() - 1; i++) {233((AbstractButton) toolbar.getComponentAtIndex(i)).doClick();234try {235Thread.sleep(4444);236} catch (InterruptedException e) {237return;238}239}240}241thread = null;242}243} // End DemoControls244} // End Text245246247248