home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Clipping / Text.java < prev   
Encoding:
Java Source  |  2002-09-06  |  7.6 KB  |  217 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.  * @(#)Text.java    1.27 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Clipping;
  41.  
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import java.awt.image.BufferedImage;
  45. import java.awt.geom.Line2D;
  46. import java.awt.geom.AffineTransform;
  47. import java.awt.image.BufferedImage;
  48. import java.awt.font.TextLayout;
  49. import java.awt.font.FontRenderContext;
  50. import javax.swing.*;
  51. import java2d.ControlsSurface;
  52. import java2d.CustomControls;
  53.  
  54.  
  55. /**
  56.  * Clipping an image, lines, text, texture and gradient with text.
  57.  */
  58. public class Text extends ControlsSurface {
  59.  
  60.     static Image img;
  61.     static TexturePaint texture;
  62.     static {
  63.         BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
  64.         Graphics2D big = bi.createGraphics();
  65.         big.setBackground(Color.yellow);
  66.         big.clearRect(0,0,5,5);
  67.         big.setColor(Color.red);
  68.         big.fillRect(0,0,3,3);
  69.         texture = new TexturePaint(bi,new Rectangle(0,0,5,5));
  70.     }
  71.     private String clipType = "Lines";
  72.     private DemoControls controls;
  73.     protected boolean doClip = true;
  74.  
  75.  
  76.     public Text() {
  77.         setBackground(Color.white);
  78.         img = getImage("clouds.jpg");
  79.         setControls(new Component[] { new DemoControls(this) });
  80.     }
  81.  
  82.  
  83.     public void render(int w, int h, Graphics2D g2) {
  84.  
  85.         FontRenderContext frc = g2.getFontRenderContext();
  86.         Font f = new Font("sansserif",Font.BOLD,32);
  87.         String s = new String("JAVA");
  88.         TextLayout tl = new TextLayout(s, f, frc);
  89.         double sw = tl.getBounds().getWidth();
  90.         double sh = tl.getBounds().getHeight();
  91.         double sx = (w-40)/sw;
  92.         double sy = (h-40)/sh;
  93.         AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy);
  94.         Shape shape = tl.getOutline(Tx);
  95.         sw = shape.getBounds().getWidth();
  96.         sh = shape.getBounds().getHeight();
  97.         Tx = AffineTransform.getTranslateInstance(w/2-sw/2, h/2+sh/2);
  98.         shape = Tx.createTransformedShape(shape);
  99.         Rectangle r = shape.getBounds();
  100.  
  101.         if (doClip) {
  102.             g2.clip(shape);
  103.         }
  104.  
  105.         if (clipType.equals("Lines")) {
  106.             g2.setColor(Color.black);
  107.             g2.fill(r);
  108.             g2.setColor(Color.yellow);
  109.             g2.setStroke(new BasicStroke(1.5f));
  110.             for (int j = r.y; j < r.y + r.height; j=j+3) {
  111.                 Line2D line = new Line2D.Float( (float) r.x, (float) j,
  112.                                             (float) (r.x+r.width), (float) j);
  113.                 g2.draw(line);
  114.             }
  115.         } else if (clipType.equals("Image")) {
  116.             g2.drawImage(img, r.x, r.y, r.width, r.height, null);
  117.         } else if (clipType.equals("TP")) {
  118.             g2.setPaint(texture);
  119.             g2.fill(r);
  120.         } else if (clipType.equals("GP")) {
  121.             g2.setPaint(new GradientPaint(0,0,Color.blue,w,h,Color.yellow));
  122.             g2.fill(r);
  123.         } else if (clipType.equals("Text")) {
  124.             g2.setColor(Color.black);
  125.             g2.fill(shape.getBounds());
  126.             g2.setColor(Color.cyan);
  127.             f = new Font("serif",Font.BOLD,10);
  128.             tl = new TextLayout("java", f, frc);
  129.             sw = tl.getBounds().getWidth();
  130.     
  131.             int x = r.x;
  132.             int y = (int) (r.y + tl.getAscent());
  133.             sh = r.y + r.height;
  134.             while ( y < sh ) {
  135.                 tl.draw(g2, x, y);
  136.                 if ((x += (int) sw) > (r.x+r.width)) {
  137.                     x = r.x;
  138.                     y += (int) tl.getAscent();
  139.                 }
  140.             }
  141.         }
  142.         g2.setClip(new Rectangle(0, 0, w, h));
  143.  
  144.         g2.setColor(Color.gray);
  145.         g2.draw(shape);
  146.     }
  147.  
  148.  
  149.     public static void main(String s[]) {
  150.         createDemoFrame(new Text());
  151.     }
  152.  
  153.  
  154.     static class DemoControls extends CustomControls implements ActionListener {
  155.  
  156.         Text demo;
  157.         JToolBar toolbar;
  158.  
  159.         public DemoControls(Text demo) {
  160.             super(demo.name);
  161.             this.demo = demo;
  162.             setBackground(Color.gray);
  163.             add(toolbar = new JToolBar());
  164.             toolbar.setFloatable(false);
  165.             addTool("Clip", true);
  166.             addTool("Lines", true);
  167.             addTool("Image", false);
  168.             addTool("TP", false);
  169.             addTool("GP", false);
  170.             addTool("Text", false);
  171.         }
  172.  
  173.         public void addTool(String str, boolean state) {
  174.             JButton b = (JButton) toolbar.add(new JButton(str));
  175.             b.setSelected(state);
  176.             b.setBackground(state ? Color.green : Color.lightGray);
  177.             b.addActionListener(this);
  178.         }
  179.  
  180.         public void actionPerformed(ActionEvent e) {
  181.             if (e.getSource().equals(toolbar.getComponentAtIndex(0))) {
  182.                 JButton b = (JButton) e.getSource();
  183.                 b.setSelected(demo.doClip = !demo.doClip);
  184.                 b.setBackground(b.isSelected() ? Color.green : Color.lightGray);
  185.             } else {
  186.                 for (int i = 1; i < toolbar.getComponentCount(); i++) {
  187.                     JButton b = (JButton) toolbar.getComponentAtIndex(i);
  188.                     b.setBackground(Color.lightGray);
  189.                 }
  190.                 JButton b = (JButton) e.getSource();
  191.                 b.setBackground(Color.green);
  192.                 demo.clipType = b.getText();
  193.             }
  194.             demo.repaint();
  195.         }
  196.  
  197.         public Dimension getPreferredSize() {
  198.             return new Dimension(200,36);
  199.         }
  200.  
  201.  
  202.         public void run() {
  203.             try { thread.sleep(1111); } catch (Exception e) { return; }
  204.             Thread me = Thread.currentThread();
  205.             while (thread == me) {
  206.                 for (int i = 1; i < toolbar.getComponentCount()-1; i++) {
  207.                     ((JButton) toolbar.getComponentAtIndex(i)).doClick();
  208.                     try {
  209.                         thread.sleep(4444);
  210.                     } catch (InterruptedException e) { return; }
  211.                 }
  212.             }
  213.             thread = null;
  214.         }
  215.     } // End DemoControls
  216. } // End Text
  217.