TextureChooser.java
Upload User: cdlibang
Upload Date: 2016-07-17
Package Size: 774k
Code Size: 7k
Category:

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)TextureChooser.java 1.21 99/06/22
  3.  *
  4.  * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  * 
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  * 
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30. import java.awt.*;
  31. import java.awt.geom.Ellipse2D;
  32. import java.awt.image.BufferedImage;
  33. import java.awt.event.MouseListener;
  34. import java.awt.event.MouseEvent;
  35. import java.awt.font.TextLayout;
  36. import java.awt.font.FontRenderContext;
  37. import javax.swing.JPanel;
  38. import javax.swing.border.TitledBorder;
  39. import javax.swing.border.EtchedBorder;
  40. import java.awt.event.WindowEvent;
  41. import java.awt.event.WindowAdapter;
  42. /**
  43.  * Four types of Paint displayed: Geometry, Text & Image Textures and
  44.  * a Gradient Paint.  Paints can be selected with the Mouse.
  45.  */
  46. public class TextureChooser extends JPanel {
  47.     static public Object texture = getGeomTexture();
  48.     public int num;
  49.     public TextureChooser(int num) {
  50.         this.num = num;
  51.         setLayout(new GridLayout(0,2,5,5));
  52.         setBorder(new TitledBorder(new EtchedBorder(), "Texture Chooser"));
  53.         add(new Surface(getGeomTexture(), this, 0));
  54.         add(new Surface(getImageTexture(), this, 1));
  55.         add(new Surface(getTextTexture(), this, 2));
  56.         add(new Surface(getGradientPaint(), this, 3));
  57.     }
  58.     static public TexturePaint getGeomTexture() {
  59.         BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
  60.         Graphics2D tG2 = bi.createGraphics();
  61.         tG2.setBackground(Color.white);
  62.         tG2.clearRect(0,0,5,5);
  63.         tG2.setColor(new Color(211,211,211,200));
  64.         tG2.fill(new Ellipse2D.Float(0,0,5,5));
  65.         Rectangle r = new Rectangle(0,0,5,5);
  66.         return new TexturePaint(bi,r);
  67.     }
  68.     public TexturePaint getImageTexture() {
  69.         Image img = DemoImages.getImage("HotJava-16.gif", this);
  70.         int iw = img.getWidth(this);
  71.         int ih = img.getHeight(this);
  72.         BufferedImage bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
  73.         Graphics2D tG2 = bi.createGraphics();
  74.         tG2.drawImage(img, 0, 0, this);
  75.         Rectangle r = new Rectangle(0,0,iw,ih);
  76.         return new TexturePaint(bi,r);
  77.     }
  78.     public TexturePaint getTextTexture() {
  79.         Font f = new Font("Times New Roman", Font.BOLD, 10);
  80.         TextLayout tl = new TextLayout("Java2D", f, new FontRenderContext(null, false, false));
  81.         int sw = (int) tl.getBounds().getWidth();
  82.         int sh = (int) (tl.getAscent()+tl.getDescent());
  83.         BufferedImage bi = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB);
  84.         Graphics2D tG2 = bi.createGraphics();
  85.         tG2.setBackground(Color.white);
  86.         tG2.clearRect(0,0,sw,sh);
  87.         tG2.setColor(Color.lightGray);
  88.         tl.draw(tG2, 0, (float) tl.getAscent());
  89.         Rectangle r = new Rectangle(0,0,sw,sh);
  90.         return new TexturePaint(bi,r);
  91.     }
  92.     public GradientPaint getGradientPaint() {
  93.         return new GradientPaint(0,0,Color.white,80,0,Color.green);
  94.     }
  95.     public class Surface extends JPanel implements MouseListener {
  96.         public boolean clickedFrame;
  97.         private int num;
  98.         private TextureChooser tc;
  99.         private boolean enterExitFrame = false;
  100.         private Object t;
  101.         public Surface(Object t, TextureChooser tc, int num) {
  102.             setBackground(Color.white);
  103.             this.t = t;
  104.             this.tc = tc;
  105.             this.clickedFrame = (num == tc.num);
  106.             this.num = num;
  107.             if (num == tc.num)
  108.                 tc.texture = t;
  109.             addMouseListener(this);
  110.         }
  111.         public void paintComponent(Graphics g) {
  112.             super.paintComponent(g);
  113.             Graphics2D g2 = (Graphics2D) g;
  114.             int w = getSize().width;
  115.             int h = getSize().height;
  116.             if (t instanceof TexturePaint)
  117.                 g2.setPaint((TexturePaint) t);
  118.             else {
  119.                 g2.setPaint((GradientPaint) t);
  120.             }
  121.             g2.fill(new Rectangle(0,0,w,h));
  122.             if (clickedFrame || enterExitFrame) {
  123.                 g2.setColor(Color.gray);
  124.                 BasicStroke bs = new BasicStroke(3, BasicStroke.CAP_BUTT,
  125.                                 BasicStroke.JOIN_MITER);
  126.                 g2.setStroke(bs);
  127.                 g2.drawRect(0,0,w-1,h-1);
  128.                 tc.num = num;
  129.             }
  130.         }
  131.         public void mouseClicked(MouseEvent e) {
  132.             tc.texture = t;
  133.             clickedFrame = true;
  134.             Component cmps[] = tc.getComponents();
  135.             for (int i = 0; i < cmps.length; i++) {
  136.                 if (cmps[i] instanceof Surface) {
  137.                     Surface surf = (Surface) cmps[i];
  138.                     if (!surf.equals(this) && surf.clickedFrame) {
  139.                         surf.clickedFrame = false;
  140.                         surf.repaint();
  141.                     }
  142.                 }
  143.             }
  144.         }
  145.         public void mousePressed(MouseEvent e) {
  146.         }
  147.         public void mouseReleased(MouseEvent e) {
  148.         }
  149.         public void mouseEntered(MouseEvent e) {
  150.             enterExitFrame = true;
  151.             repaint();
  152.         }
  153.         public void mouseExited(MouseEvent e) {
  154.             enterExitFrame = false;
  155.             repaint();
  156.         }
  157.         public Dimension getMinimumSize() {
  158.             return getPreferredSize();
  159.         }
  160.         public Dimension getMaximumSize() {
  161.             return getPreferredSize();
  162.         }
  163.         public Dimension getPreferredSize() {
  164.             return new Dimension(30,30);
  165.         }
  166.     }
  167.     public static void main(String s[]) {
  168.         Frame f = new Frame("Java2D Demo - TextureChooser");
  169.         f.addWindowListener(new WindowAdapter() {
  170.             public void windowClosing(WindowEvent e) {System.exit(0);}
  171.         });
  172.         f.add("Center", new TextureChooser(0));
  173.         f.pack();
  174.         f.setSize(new Dimension(400,400));
  175.         f.show();
  176.     }
  177. }