home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Paint / TextureAnim.java < prev   
Encoding:
Java Source  |  2002-09-06  |  13.9 KB  |  423 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.  * @(#)TextureAnim.java    1.14 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Paint;
  41.     
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import java.awt.image.BufferedImage;
  45. import java.awt.geom.AffineTransform;
  46. import java.awt.font.TextLayout;
  47. import java.awt.font.FontRenderContext;
  48. import javax.swing.*;
  49. import java2d.AnimatingControlsSurface;
  50. import java2d.CustomControls;
  51.  
  52.  
  53.  
  54. /**
  55.  * TexturePaint animation with controls for transformations.
  56.  */
  57. public class TextureAnim extends AnimatingControlsSurface {
  58.  
  59.     public static final Color colorblend = new Color(0f, 0f, 1f, .5f);
  60.     protected static BufferedImage textureImg;
  61.     protected int bNum;
  62.     protected int tilesize;
  63.     private boolean newtexture;
  64.     private TexturePaint texture;
  65.     private Rectangle tilerect;
  66.     private boolean bouncesize = false;
  67.     private boolean bouncerect = true;
  68.     private boolean rotate = false;
  69.     private boolean shearx = false;
  70.     private boolean sheary = false;
  71.     private boolean showanchor = true;
  72.     private boolean quality = false;
  73.     private AnimVal w, h, x, y, rot, shx, shy;
  74.     private static Image img[] = new Image[2];
  75.     
  76.  
  77.     public TextureAnim() {
  78.         img[0] = getImage("duke.gif");   // 8 bit gif
  79.         img[1] = getImage("duke.png");   // 24 bit png
  80.  
  81.         textureImg = makeImage(32, 0);
  82.         tilesize = textureImg.getWidth();
  83.         w = new AnimVal(0, 200, 3, 10, tilesize);
  84.         h = new AnimVal(0, 200, 3, 10, tilesize);
  85.         x = new AnimVal(0, 200, 3, 10, 0);
  86.         y = new AnimVal(0, 200, 3, 10, 0);
  87.         rot = new AnimVal(-360, 360, 5, 15, 0);
  88.         shx = new AnimVal(-50, 50, 3, 10, 0);
  89.         shy = new AnimVal(-50, 50, 3, 10, 0);
  90.         tilerect = new Rectangle(x.getInt(), y.getInt(),
  91.                                  w.getInt(), h.getInt());
  92.         texture = new TexturePaint(textureImg, tilerect);
  93.         setControls(new Component[] { new DemoControls(this) });
  94.     }
  95.  
  96.  
  97.     protected BufferedImage makeImage(int size, int num) {
  98.         newtexture = true;
  99.         switch (bNum = num) {
  100.             case 0 : return makeRGBImage(size);
  101.             case 1 : return makeGIFImage(size);
  102.             case 2 : return makePNGImage(size);
  103.         }
  104.         return null;
  105.     }
  106.  
  107.  
  108.     private BufferedImage makeRGBImage(int size) {
  109.         BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
  110.         Graphics2D big = bi.createGraphics();
  111.         big.setColor(Color.white);
  112.         big.fillRect(0, 0, size, size);
  113.         for (int j = 0; j < size; j++) {
  114.             float red = j / (float) size;
  115.             for (int i = 0; i < size; i++) {
  116.                 float green = i / (float) size;
  117.                 big.setColor(new Color(1.0f - red, 1.0f - green, 0.0f, 1.0f));
  118.                 big.drawLine(i, j, i, j);
  119.             }
  120.         }
  121.         return bi;
  122.     }
  123.  
  124.  
  125.     private BufferedImage makeGIFImage(int d) {
  126.         BufferedImage bi = new BufferedImage(d, d, BufferedImage.TYPE_INT_RGB);
  127.         Graphics2D big = bi.createGraphics();
  128.         big.drawImage(img[0], 0, 0, d, d, new Color(204, 204, 255), null);
  129.         return bi;
  130.     }
  131.  
  132.  
  133.     private BufferedImage makePNGImage(int d) {
  134.         BufferedImage bi = new BufferedImage(d, d, BufferedImage.TYPE_INT_RGB);
  135.         Graphics2D big = bi.createGraphics();
  136.         big.drawImage(img[1], 0, 0, d, d, Color.lightGray, null);
  137.         return bi;
  138.     }
  139.  
  140.  
  141.     public void reset(int width, int height) {
  142.         x.newlimits(-width/4, width/4 - w.getInt());
  143.         y.newlimits(-height/4, height/4 - h.getInt());
  144.     }
  145.  
  146.  
  147.     public void step(int width, int height) {
  148.         if (tilesize != textureImg.getWidth()) {
  149.             tilesize = textureImg.getWidth();
  150.         }
  151.         if (bouncesize) {
  152.             w.anim();
  153.             h.anim();
  154.             x.newlimits(-width/4, width/4 - w.getInt());
  155.             y.newlimits(-height/4, height/4 - h.getInt());
  156.         } else {
  157.             if (w.getInt() != tilesize) {
  158.                 w.set(tilesize);
  159.                 x.newlimits(-width/4, width/4 - w.getInt());
  160.             }
  161.             if (h.getInt() != tilesize) {
  162.                 h.set(tilesize);
  163.                 y.newlimits(-height/4, height/4 - h.getInt());
  164.             }
  165.         }
  166.         if (bouncerect) {
  167.             x.anim();
  168.             y.anim();
  169.         }
  170.         if (newtexture ||
  171.             x.getInt() != tilerect.x || y.getInt() != tilerect.y ||
  172.             w.getInt() != tilerect.width || h.getInt() != tilerect.height)
  173.         {
  174.             newtexture = false;
  175.             int X = x.getInt();
  176.             int Y = y.getInt();
  177.             int W = w.getInt();
  178.             int H = h.getInt();
  179.             tilerect = new Rectangle(X, Y, W, H);
  180.             texture = new TexturePaint(textureImg, tilerect);
  181.         }
  182.     }
  183.  
  184.  
  185.     public void render(int width, int height, Graphics2D g2) {
  186.  
  187.         g2.translate(width/2, height/2);
  188.         if (rotate) {
  189.             rot.anim();
  190.             g2.rotate(Math.toRadians(rot.getFlt()));
  191.         } else {
  192.             rot.set(0);
  193.         }
  194.         if (shearx) {
  195.             shx.anim();
  196.             g2.shear(shx.getFlt()/100, 0.0f);
  197.         } else {
  198.             shx.set(0);
  199.         }
  200.         if (sheary) {
  201.             shy.anim();
  202.             g2.shear(0.0f, shy.getFlt()/100);
  203.         } else {
  204.             shy.set(0);
  205.         }
  206.         g2.setPaint(texture);
  207.         g2.fillRect(-1000, -1000, 2000, 2000);
  208.         if (showanchor) {
  209.             g2.setColor(Color.black);
  210.             g2.setColor(colorblend);
  211.             g2.fill(tilerect);
  212.         }
  213.     }
  214.  
  215.  
  216.     public static void main(String argv[]) {
  217.         createDemoFrame(new TextureAnim());
  218.     }
  219.  
  220.  
  221.     static class AnimVal {
  222.         float curval;
  223.         float lowval;
  224.         float highval;
  225.         float currate;
  226.         float lowrate;
  227.         float highrate;
  228.  
  229.         public AnimVal(int lowval, int highval,
  230.                        int lowrate, int highrate) {
  231.             this.lowval = lowval;
  232.             this.highval = highval;
  233.             this.lowrate = lowrate;
  234.             this.highrate = highrate;
  235.             this.curval = randval(lowval, highval);
  236.             this.currate = randval(lowrate, highrate);
  237.         }
  238.  
  239.         public AnimVal(int lowval, int highval,
  240.                        int lowrate, int highrate,
  241.                        int pos) {
  242.             this(lowval, highval, lowrate, highrate);
  243.             set(pos);
  244.         }
  245.  
  246.         public float randval(float low, float high) {
  247.             return (float) (low + Math.random() * (high - low));
  248.         }
  249.  
  250.         public float getFlt() {
  251.             return curval;
  252.         }
  253.  
  254.         public int getInt() {
  255.             return (int) curval;
  256.         }
  257.  
  258.         public void anim() {
  259.             curval += currate;
  260.             clip();
  261.         }
  262.  
  263.         public void set(float val) {
  264.             curval = val;
  265.             clip();
  266.         }
  267.  
  268.         public void clip() {
  269.             if (curval > highval) {
  270.                 curval = highval - (curval - highval);
  271.                 if (curval < lowval) {
  272.                     curval = highval;
  273.                 }
  274.                 currate = - randval(lowrate, highrate);
  275.             } else if (curval < lowval) {
  276.                 curval = lowval + (lowval - curval);
  277.                 if (curval > highval) {
  278.                     curval = lowval;
  279.                 }
  280.                 currate = randval(lowrate, highrate);
  281.             }
  282.         }
  283.  
  284.         public void newlimits(int lowval, int highval) {
  285.             this.lowval = lowval;
  286.             this.highval = highval;
  287.             clip();
  288.         }
  289.     }  // End AnimVal class
  290.  
  291.  
  292.     class DemoControls extends CustomControls implements ActionListener {
  293.  
  294.         TextureAnim demo;
  295.         JToolBar toolbar;
  296.         JComboBox combo;
  297.         JMenu menu;
  298.         JMenuItem menuitems[];
  299.         int iconSize = 20;
  300.         
  301.  
  302.         public DemoControls(TextureAnim demo) {
  303.             super(demo.name);
  304.             this.demo = demo;
  305.             menuitems = new JMenuItem[3];
  306.             setBackground(Color.gray);
  307.             add(toolbar = new JToolBar());
  308.             toolbar.setFloatable(false);
  309.             addTool("BO", "bounce", true);
  310.             addTool("SA", "show anchor", true);
  311.             addTool("RS", "resize", false);
  312.             addTool("RO", "rotate", false);
  313.             addTool("SX", "shear x", false);
  314.             addTool("SY", "shear y", false);
  315.             add(combo = new JComboBox());
  316.             combo.addActionListener(this);
  317.             combo.addItem("8");
  318.             combo.addItem("16");
  319.             combo.addItem("32");
  320.             combo.addItem("64");
  321.             combo.addItem("80");
  322.             combo.setSelectedIndex(2);
  323.  
  324.             JMenuBar menuBar = new JMenuBar();
  325.             menu = (JMenu) menuBar.add(new JMenu());
  326.             for (int i = 0; i < 3; i++) {
  327.                 BufferedImage bimg = demo.makeImage(iconSize, i);
  328.                 TexturedIcon icon = new TexturedIcon(bimg);
  329.                 menuitems[i] = menu.add(new JMenuItem(icon));
  330.                 menuitems[i].addActionListener(this);
  331.             } 
  332.             menu.setIcon(menuitems[0].getIcon());
  333.             add(menuBar);
  334.            demo.bNum = 0;
  335.         }
  336.  
  337.  
  338.         public void addTool(String str, String toolTip, boolean state) {
  339.             JButton b = (JButton) toolbar.add(new JButton(str));
  340.             b.setBackground(state ? Color.green : Color.lightGray);
  341.             b.setSelected(state);
  342.             b.setToolTipText(toolTip);
  343.             b.addActionListener(this);
  344.         }
  345.  
  346.  
  347.         public void actionPerformed(ActionEvent e) {
  348.             Object obj = e.getSource();
  349.             if (obj instanceof JComboBox) {
  350.         String selItem = (String) combo.getSelectedItem();
  351.         if (selItem != null) {
  352.             int size = Integer.parseInt(selItem);
  353.             demo.textureImg = demo.makeImage(size, demo.bNum);
  354.         }
  355.             } else if (obj instanceof JMenuItem) {
  356.                 for (int i = 0; i < menuitems.length; i++) {
  357.                     if (obj.equals(menuitems[i])) {
  358.                         demo.textureImg = demo.makeImage(demo.tilesize, i);
  359.                         menu.setIcon(menuitems[i].getIcon());
  360.                         break;
  361.                     } 
  362.                 }
  363.             } else {
  364.                 JButton b = (JButton) obj;
  365.                 b.setSelected(!b.isSelected());
  366.                 b.setBackground(b.isSelected() ? Color.green : Color.lightGray);
  367.                 if (b.getText().equals("BO")) {
  368.                     demo.bouncerect = b.isSelected();
  369.                 } else if (b.getText().equals("SA")) {
  370.                     demo.showanchor = b.isSelected();
  371.                 } else if (b.getText().equals("RS")) {
  372.                     demo.bouncesize = b.isSelected();
  373.                 } else if (b.getText().equals("RO")) {
  374.                     demo.rotate = b.isSelected();
  375.                 } else if (b.getText().equals("SX")) {
  376.                     demo.shearx = b.isSelected();
  377.                 } else if (b.getText().equals("SY")) {
  378.                     demo.sheary = b.isSelected();
  379.                 }
  380.             }
  381.             if (demo.animating.thread == null) {
  382.                 demo.repaint();
  383.             }
  384.         }
  385.  
  386.         public Dimension getPreferredSize() {
  387.             return new Dimension(200,37);
  388.         }
  389.  
  390.  
  391.         public void run() {
  392.             Thread me = Thread.currentThread();
  393.             while (thread == me) {
  394.                 for (int i = 2; i < toolbar.getComponentCount(); i++) {
  395.                     try {
  396.                         thread.sleep(4444);
  397.                     } catch (InterruptedException e) { return; }
  398.                     ((JButton) toolbar.getComponentAtIndex(i)).doClick();
  399.                 }
  400.             }
  401.             thread = null;
  402.         }
  403.  
  404.  
  405.         class TexturedIcon implements Icon {
  406.             BufferedImage bi;
  407.             public TexturedIcon(BufferedImage bi) {
  408.                 this.bi = bi;
  409.             }
  410.             public void paintIcon(Component c, Graphics g, int x, int y) {
  411.                 Graphics2D g2 = (Graphics2D) g;
  412.                 Rectangle r = new Rectangle(x, y, iconSize, iconSize);
  413.                 g2.setPaint(new TexturePaint(bi, r));
  414.             g2.fillRect(x, y, iconSize, iconSize);
  415.             g2.setColor(Color.gray);
  416.             g2.draw3DRect(x, y, iconSize-1, iconSize-1, true);
  417.             }
  418.             public int getIconWidth() { return iconSize; }
  419.             public int getIconHeight() { return iconSize; }
  420.         } // End TexturedIcon class
  421.     } // End DemoControls class
  422. } // End TextureAnim class
  423.