home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Fonts / AttributedStr.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  6.8 KB  |  177 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)AttributedStr.java    1.9 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Fonts;
  41.  
  42. import java.awt.*;
  43. import java.awt.font.*;
  44. import java.awt.geom.*;
  45. import java.net.URL;
  46. import java.io.File;
  47. import java.io.FileInputStream;
  48. import java.io.InputStream;
  49. import java.awt.image.BufferedImage;
  50. import java.text.AttributedString;
  51. import java.text.AttributedCharacterIterator;
  52. import java2d.Surface;
  53.  
  54.  
  55. /**
  56.  * Demonstrates how to build an AttributedString and then render the
  57.  * string broken over lines. 
  58.  */
  59. public class AttributedStr extends Surface {
  60.  
  61.     static Color black = new Color(20, 20, 20); 
  62.     static Color blue = new Color(94, 105, 176); 
  63.     static Color yellow = new Color(255, 255, 140);
  64.     static Color red = new Color(149, 43, 42);
  65.     static Color white = new Color(240, 240, 255); 
  66.     static String text = "  A quick brown  fox  jumped  over the lazy duke  ";
  67.     static AttributedString as = new AttributedString(text);
  68.     static AttributedCharacterIterator aci; 
  69.     static {
  70.         Shape shape = new Ellipse2D.Double(0,25,12,12);
  71.     ShapeGraphicAttribute sga = new ShapeGraphicAttribute(shape, GraphicAttribute.TOP_ALIGNMENT, false);
  72.     as.addAttribute(TextAttribute.CHAR_REPLACEMENT, sga, 0, 1);
  73.  
  74.  
  75.         Font font = new Font("sanserif", Font.BOLD | Font.ITALIC, 20);
  76.         int index = text.indexOf("quick");
  77.         as.addAttribute(TextAttribute.FONT, font, index, index+5);
  78.  
  79.         index = text.indexOf("brown");
  80.         font = new Font("serif", Font.BOLD, 20);
  81.         as.addAttribute(TextAttribute.FONT, font, index, index+5);
  82.         as.addAttribute(TextAttribute.FOREGROUND, red, index, index+5);
  83.  
  84.         index = text.indexOf("fox");
  85.         AffineTransform fontAT = new AffineTransform();
  86.         fontAT.rotate(Math.toRadians(10));
  87.         Font fx = new Font("serif", Font.BOLD, 30).deriveFont(fontAT);
  88.         as.addAttribute(TextAttribute.FONT, fx, index, index+1);
  89.         as.addAttribute(TextAttribute.FONT, fx, index+1, index+2);
  90.         as.addAttribute(TextAttribute.FONT, fx, index+2, index+3);
  91.  
  92.         fontAT.setToRotation(Math.toRadians(-4));
  93.         fx = font.deriveFont(fontAT);
  94.         index = text.indexOf("jumped");
  95.         as.addAttribute(TextAttribute.FONT, fx, index, index+6);
  96.  
  97.         font = new Font("serif", Font.BOLD | Font.ITALIC, 30);
  98.         index = text.indexOf("over");
  99.         as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, index, index+4);
  100.         as.addAttribute(TextAttribute.FOREGROUND, white, index, index+4);
  101.         as.addAttribute(TextAttribute.FONT, font, index, text.length());
  102.  
  103.         font = new Font("dialog", Font.PLAIN, 20);
  104.         int i = text.indexOf("duke");
  105.         as.addAttribute(TextAttribute.FONT, font, index, i-1);
  106.  
  107.         BufferedImage bi = new BufferedImage(4,4,BufferedImage.TYPE_INT_ARGB);
  108.         bi.setRGB(0, 0, 0xffffffff); 
  109.         TexturePaint tp = new TexturePaint(bi,new Rectangle(0,0,4,4));
  110.         as.addAttribute(TextAttribute.BACKGROUND, tp, i, i+4);
  111.         font = new Font("serif", Font.BOLD, 40);
  112.         as.addAttribute(TextAttribute.FONT, font, i, i+4);
  113.     }
  114.  
  115.  
  116.     public AttributedStr() {
  117.         setBackground(Color.white);
  118.  
  119.         Font font = getFont("A.ttf");
  120.         if (font != null) {
  121.             font = font.deriveFont(Font.PLAIN, 70);
  122.         } else {
  123.             font = new Font("serif", Font.PLAIN, 50);
  124.         }
  125.         int index = text.indexOf("A")+1;
  126.         as.addAttribute(TextAttribute.FONT, font, 0, index);
  127.         as.addAttribute(TextAttribute.FOREGROUND, white, 0, index);
  128.  
  129.         font = new Font("dialog", Font.PLAIN, 40);
  130.         int size = getFontMetrics(font).getHeight();
  131.         BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
  132.         Graphics2D big = bi.createGraphics();
  133.         big.drawImage(getImage("snooze.gif"), 0, 0, size, size, null);
  134.     ImageGraphicAttribute iga = new ImageGraphicAttribute(bi, GraphicAttribute.TOP_ALIGNMENT);
  135.     as.addAttribute(TextAttribute.CHAR_REPLACEMENT, iga, text.length()-1, text.length());
  136.  
  137.         aci = as.getIterator();
  138.     }
  139.  
  140.  
  141.     public void render(int w, int h, Graphics2D g2) {
  142.  
  143.         float x = 5, y = 0;
  144.         FontRenderContext frc = g2.getFontRenderContext();
  145.         LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
  146.  
  147.         g2.setPaint(new GradientPaint(0,h,blue,w,0,black));
  148.         g2.fillRect(0, 0, w, h);
  149.  
  150.         g2.setColor(white);
  151.         String s = "AttributedString LineBreakMeasurer";
  152.         Font font = new Font("serif", Font.PLAIN, 12);
  153.         TextLayout tl = new TextLayout(s, font, frc);
  154.         
  155.         tl.draw(g2, 5, y += (float) tl.getBounds().getHeight());
  156.  
  157.         g2.setColor(yellow);
  158.  
  159.         while (y < h-tl.getAscent()) {
  160.             lbm.setPosition(0);
  161.             while (lbm.getPosition() < text.length()) {
  162.                 tl = lbm.nextLayout(w-x);
  163.                 if (!tl.isLeftToRight()) {
  164.                     x = w - tl.getAdvance();
  165.                 }
  166.                 tl.draw(g2, x, y += tl.getAscent());
  167.                 y += tl.getDescent() + tl.getLeading();
  168.             }
  169.         }
  170.     }
  171.  
  172.  
  173.     public static void main(String s[]) {
  174.         createDemoFrame(new AttributedStr());
  175.     }
  176. }
  177.