home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / jfc / Java2D / src / java2d / TextureChooser.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  7.6 KB  |  223 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.  * @(#)TextureChooser.java    1.27 02/06/13
  38.  */
  39.  
  40. package java2d;
  41.  
  42. import java.awt.*;
  43. import java.awt.geom.Ellipse2D;
  44. import java.awt.image.BufferedImage;
  45. import java.awt.event.MouseListener;
  46. import java.awt.event.MouseEvent;
  47. import java.awt.font.TextLayout;
  48. import java.awt.font.FontRenderContext;
  49. import javax.swing.JPanel;
  50. import javax.swing.border.TitledBorder;
  51. import javax.swing.border.EtchedBorder;
  52. import java.awt.event.WindowEvent;
  53. import java.awt.event.WindowAdapter;
  54.  
  55.  
  56. /**
  57.  * Four types of Paint displayed: Geometry, Text & Image Textures and
  58.  * a Gradient Paint.  Paints can be selected with the Mouse.
  59.  */
  60. public class TextureChooser extends JPanel {
  61.  
  62.     static public Object texture = getGeomTexture();
  63.     public int num;
  64.  
  65.     public TextureChooser(int num) {
  66.         this.num = num;
  67.         setLayout(new GridLayout(0,2,5,5));
  68.         setBorder(new TitledBorder(new EtchedBorder(), "Texture Chooser"));
  69.  
  70.         add(new Surface(getGeomTexture(), this, 0));
  71.         add(new Surface(getImageTexture(), this, 1));
  72.         add(new Surface(getTextTexture(), this, 2));
  73.         add(new Surface(getGradientPaint(), this, 3));
  74.     }
  75.  
  76.  
  77.     static public TexturePaint getGeomTexture() {
  78.         BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
  79.         Graphics2D tG2 = bi.createGraphics();
  80.         tG2.setBackground(Color.white);
  81.         tG2.clearRect(0,0,5,5);
  82.         tG2.setColor(new Color(211,211,211,200));
  83.         tG2.fill(new Ellipse2D.Float(0,0,5,5));
  84.         Rectangle r = new Rectangle(0,0,5,5);
  85.         return new TexturePaint(bi,r);
  86.     }
  87.  
  88.     public TexturePaint getImageTexture() {
  89.         Image img = DemoImages.getImage("HotJava-16.gif", this);
  90.         int iw = img.getWidth(this);
  91.         int ih = img.getHeight(this);
  92.         BufferedImage bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
  93.         Graphics2D tG2 = bi.createGraphics();
  94.         tG2.drawImage(img, 0, 0, this);
  95.         Rectangle r = new Rectangle(0,0,iw,ih);
  96.         return new TexturePaint(bi,r);
  97.     }
  98.  
  99.  
  100.     public TexturePaint getTextTexture() {
  101.         Font f = new Font("Times New Roman", Font.BOLD, 10);
  102.         TextLayout tl = new TextLayout("Java2D", f, new FontRenderContext(null, false, false));
  103.         int sw = (int) tl.getBounds().getWidth();
  104.         int sh = (int) (tl.getAscent()+tl.getDescent());
  105.         BufferedImage bi = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB);
  106.         Graphics2D tG2 = bi.createGraphics();
  107.         tG2.setBackground(Color.white);
  108.         tG2.clearRect(0,0,sw,sh);
  109.         tG2.setColor(Color.lightGray);
  110.         tl.draw(tG2, 0, (float) tl.getAscent());
  111.         Rectangle r = new Rectangle(0,0,sw,sh);
  112.         return new TexturePaint(bi,r);
  113.     }
  114.  
  115.  
  116.     public GradientPaint getGradientPaint() {
  117.         return new GradientPaint(0,0,Color.white,80,0,Color.green);
  118.     }
  119.  
  120.     public class Surface extends JPanel implements MouseListener {
  121.  
  122.         public boolean clickedFrame;
  123.         private int num;
  124.         private TextureChooser tc;
  125.         private boolean enterExitFrame = false;
  126.         private Object t;
  127.  
  128.         public Surface(Object t, TextureChooser tc, int num) {
  129.             setBackground(Color.white);
  130.             this.t = t;
  131.             this.tc = tc;
  132.             this.clickedFrame = (num == tc.num);
  133.             this.num = num;
  134.             if (num == tc.num)
  135.                 tc.texture = t;
  136.             addMouseListener(this);
  137.         }
  138.  
  139.         public void paintComponent(Graphics g) {
  140.             super.paintComponent(g);
  141.             Graphics2D g2 = (Graphics2D) g;
  142.             int w = getSize().width;
  143.             int h = getSize().height;
  144.             if (t instanceof TexturePaint)
  145.                 g2.setPaint((TexturePaint) t);
  146.             else {
  147.                 g2.setPaint((GradientPaint) t);
  148.             }
  149.             g2.fill(new Rectangle(0,0,w,h));
  150.             if (clickedFrame || enterExitFrame) {
  151.                 g2.setColor(Color.gray);
  152.                 BasicStroke bs = new BasicStroke(3, BasicStroke.CAP_BUTT,
  153.                                 BasicStroke.JOIN_MITER);
  154.                 g2.setStroke(bs);
  155.                 g2.drawRect(0,0,w-1,h-1);
  156.                 tc.num = num;
  157.             }
  158.         }
  159.  
  160.         public void mouseClicked(MouseEvent e) {
  161.             tc.texture = t;
  162.             clickedFrame = true;
  163.  
  164.             Component cmps[] = tc.getComponents();
  165.             for (int i = 0; i < cmps.length; i++) {
  166.                 if (cmps[i] instanceof Surface) {
  167.                     Surface surf = (Surface) cmps[i];
  168.                     if (!surf.equals(this) && surf.clickedFrame) {
  169.                         surf.clickedFrame = false;
  170.                         surf.repaint();
  171.                     }
  172.                 }
  173.             }
  174.             
  175.             // ABP
  176.             if (Java2Demo.controls.textureCB.isSelected()) {
  177.                 Java2Demo.controls.textureCB.doClick();
  178.                 Java2Demo.controls.textureCB.doClick();
  179.         }            
  180.         }
  181.  
  182.         public void mousePressed(MouseEvent e) {
  183.         }
  184.  
  185.         public void mouseReleased(MouseEvent e) {
  186.         }
  187.  
  188.         public void mouseEntered(MouseEvent e) {
  189.             enterExitFrame = true;
  190.             repaint();
  191.         }
  192.  
  193.         public void mouseExited(MouseEvent e) {
  194.             enterExitFrame = false;
  195.             repaint();
  196.         }
  197.  
  198.         public Dimension getMinimumSize() {
  199.             return getPreferredSize();
  200.         }
  201.  
  202.         public Dimension getMaximumSize() {
  203.             return getPreferredSize();
  204.         }
  205.  
  206.         public Dimension getPreferredSize() {
  207.             return new Dimension(30,30);
  208.         }
  209.  
  210.     }
  211.  
  212.     public static void main(String s[]) {
  213.         Frame f = new Frame("Java2D Demo - TextureChooser");
  214.         f.addWindowListener(new WindowAdapter() {
  215.             public void windowClosing(WindowEvent e) {System.exit(0);}
  216.         });
  217.         f.add("Center", new TextureChooser(0));
  218.         f.pack();
  219.         f.setSize(new Dimension(400,400));
  220.         f.setVisible(true);
  221.     }
  222. }
  223.