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

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)SelectTx.java 1.22 99/04/23
  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. package demos.Transforms;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.awt.font.TextLayout;
  34. import java.awt.font.FontRenderContext;
  35. import java.awt.geom.Rectangle2D;
  36. import java.awt.image.*;
  37. import javax.swing.*;
  38. import AnimatingContext;
  39. import DemoSurface;
  40. import DemoPanel;
  41. import CustomControls;
  42. /**
  43.  * Scaling or Shearing or Rotating an image & rectangle.
  44.  */
  45. public class SelectTx extends DemoSurface implements AnimatingContext, CustomControls {
  46.     protected static final int RIGHT = 0;
  47.     private static final int LEFT = 1;
  48.     private static final int XMIDDLE = 2;
  49.     private static final int DOWN = 3;
  50.     private static final int UP = 4;
  51.     private static final int YMIDDLE = 5;
  52.     private static final int XupYup = 6;
  53.     private static final int XdownYdown = 7;
  54.     private static final String[] title = { "Scale" , "Shear", "Rotate" };
  55.     private Image img, original;
  56.     private DemoControls controls;
  57.     private int iw, ih;
  58.     protected static final int SCALE = 0;
  59.     protected static final int SHEAR = 1;
  60.     protected static final int ROTATE = 2;
  61.     protected int transformType = SHEAR;
  62.     protected double sx, sy;
  63.     protected double angdeg;
  64.     protected int direction = RIGHT;
  65.     protected int transformToggle;
  66.     public SelectTx() {
  67.         setBackground(Color.white);
  68.         original = getImage("painting.gif");
  69.         iw = original.getWidth(this);
  70.         ih = original.getHeight(this);
  71.         controls = new DemoControls(this);
  72.     }
  73.     public String[] getCustomControlsConstraints() {
  74.         return new String[] { BorderLayout.NORTH };
  75.     }
  76.     public Component[] getCustomControls() {
  77.         return new Component[] { (Component) controls };
  78.     }
  79.     public void customControlsThread(int state) {
  80.         if (state == CustomControls.START) {
  81.             controls.start();
  82.         } else if (state == CustomControls.STOP) {
  83.             controls.stop();
  84.         }
  85.     }
  86.     public void reset(int w, int h) {
  87.         iw = w/3;
  88.         ih = h/3;
  89.         img = createImage(iw, ih);
  90.         Graphics big = img.getGraphics();
  91.         big.drawImage(original, 0, 0, iw, ih, Color.orange, null);
  92.         if (transformType == SCALE) {
  93.             direction = RIGHT;
  94.             sx = sy = 1.0;
  95.         } else if (transformType == SHEAR) {
  96.             direction = RIGHT;
  97.             sx = sy = 0;
  98.         } else {
  99.             angdeg = 0;
  100.         }
  101.     }
  102.     public void step(int w, int h) {
  103.         int rw = iw + 10;
  104.         int rh = ih + 10;
  105.         if (transformType == SCALE && direction == RIGHT) {
  106.             sx += .05;
  107.             if (w * .5 - iw * .5 + rw * sx + 10 > w) {
  108.                 direction = DOWN;
  109.             }
  110.         } else if (transformType == SCALE && direction == DOWN) {
  111.            sy += .05;
  112.            if (h * .5 - ih * .5 + rh * sy + 20 > h) {
  113.                direction = LEFT;
  114.             }
  115.         } else if (transformType == SCALE && direction == LEFT) {
  116.             sx -= .05;
  117.             if (rw * sx - 10 <= -(w * .5 - iw * .5)) {
  118.                 direction = UP;
  119.             }
  120.         } else if (transformType == SCALE && direction == UP) {
  121.             sy -= .05;
  122.             if (rh * sy - 20 <= -(h * .5 - ih * .5)) {
  123.                 direction = RIGHT;
  124.                 transformToggle = SHEAR;
  125.             }
  126.         }
  127.         if (transformType == SHEAR && direction == RIGHT) {
  128.             sx += .05;
  129.             if (rw + 2 * rh * sx + 20 > w) {
  130.                 direction = LEFT;
  131.                 sx -= .1;
  132.             }
  133.         } else if (transformType == SHEAR && direction == LEFT) {
  134.             sx -= .05;
  135.             if (rw - 2 * rh * sx + 20 > w) {
  136.                 direction = XMIDDLE;
  137.             }
  138.         } else if (transformType == SHEAR && direction == XMIDDLE) {
  139.             sx += .05;
  140.             if (sx > 0) {
  141.                 direction = DOWN;
  142.                 sx = 0;
  143.             }
  144.         } else if (transformType == SHEAR && direction == DOWN) {
  145.             sy -= .05;
  146.             if (rh - 2 * rw * sy + 20 > h) {
  147.                 direction = UP;
  148.                 sy += .1;
  149.             }
  150.         } else if (transformType == SHEAR && direction == UP) {
  151.             sy += .05;
  152.             if (rh + 2 * rw * sy + 20 > h) {
  153.                 direction = YMIDDLE;
  154.             }
  155.         } else if (transformType == SHEAR && direction == YMIDDLE) {
  156.             sy -= .05;
  157.             if (sy < 0) {
  158.                 direction = XupYup;
  159.                 sy = 0;
  160.             }
  161.         } else if (transformType == SHEAR && direction == XupYup) {
  162.             sx += .05; sy += .05;
  163.             if (rw + 2 * rh * sx + 30 > w || rh + 2 * rw * sy + 30 > h) {
  164.                 direction = XdownYdown;
  165.             }
  166.         } else if (transformType == SHEAR && direction == XdownYdown) {
  167.             sy -= .05; sx -= .05;
  168.             if (sy < 0) {
  169.                 direction = RIGHT;
  170.                 sx = sy = 0.0;
  171.                 transformToggle = ROTATE;
  172.             }
  173.         }
  174.         if (transformType == ROTATE) {
  175.             angdeg += 5;
  176.             if (angdeg == 360) { 
  177.                 angdeg = 0;
  178.                 transformToggle = SCALE;
  179.             }
  180.         }
  181.     }
  182.     public void drawDemo(int w, int h, Graphics2D g2) {
  183.         Font font = g2.getFont();
  184.         FontRenderContext frc = g2.getFontRenderContext();
  185.         TextLayout tl = new TextLayout(title[transformType], font, frc);
  186.         g2.setColor(Color.black);
  187.         tl.draw(g2, (float) (w/2-tl.getBounds().getWidth()/2), 
  188.             (float) (tl.getAscent()+tl.getDescent()));
  189.         if (transformType == ROTATE) {
  190.             String s = Double.toString(angdeg);
  191.             g2.drawString("angdeg=" + s, 2, h-4);
  192.         } else {
  193.             String s = Double.toString(sx);
  194.             s = (s.length() < 5) ? s : s.substring(0,5);
  195.             TextLayout tlsx = new TextLayout("sx=" + s, font, frc);
  196.             tlsx.draw(g2, 2, h-4);
  197.             s = Double.toString(sy);
  198.             s = (s.length() < 5) ? s : s.substring(0,5);
  199.             g2.drawString("sy=" + s,(int)(tlsx.getBounds().getWidth()+4), h-4);
  200.         }
  201.         if (transformType == SCALE) {
  202.             g2.translate(w/2-iw/2, h/2-ih/2);
  203.             g2.scale(sx, sy);
  204.         } else if (transformType == SHEAR) {
  205.             g2.translate(w/2-iw/2,h/2-ih/2);
  206.             g2.shear(sx, sy);
  207.         } else {
  208.             g2.rotate(Math.toRadians(angdeg),w/2,h/2);
  209.             g2.translate(w/2-iw/2,h/2-ih/2);
  210.         }
  211.         
  212.         g2.setColor(Color.orange);
  213.         g2.fillRect(0, 0, iw+10, ih+10);
  214.         g2.drawImage(img, 5, 5, this);
  215.     }
  216.     public static void main(String argv[]) {
  217.         final DemoPanel dp = new DemoPanel(new SelectTx());
  218.         Frame f = new Frame("Java2D Demo - SelectTx");
  219.         f.addWindowListener(new WindowAdapter() {
  220.             public void windowClosing(WindowEvent e) {System.exit(0);}
  221.             public void windowDeiconified(WindowEvent e) { 
  222.                 dp.surface.start(); 
  223.             }
  224.             public void windowIconified(WindowEvent e) { 
  225.                 dp.surface.stop(); 
  226.             }
  227.         });
  228.         f.add("Center", dp);
  229.         f.pack();
  230.         f.setSize(new Dimension(400,300));
  231.         f.show();
  232.         dp.surface.start();
  233.     }
  234.     static class DemoControls extends JPanel implements ActionListener, Runnable {
  235.         SelectTx demo;
  236.         JToolBar toolbar;
  237.         Thread thread;
  238.         public DemoControls(SelectTx demo) {
  239.             this.demo = demo;
  240.             setBackground(Color.gray);
  241.             add(toolbar = new JToolBar());
  242.             toolbar.setFloatable(false);
  243.             addTool("Scale", false);
  244.             addTool("Shear", true);
  245.             addTool("Rotate", false);
  246.             addMouseListener(new MouseAdapter() {
  247.                 public void mouseClicked(MouseEvent e) {
  248.                     if (thread == null) start(); else stop();
  249.                 }
  250.             });
  251.         }
  252.         public void addTool(String str, boolean state) {
  253.             JButton b = (JButton) toolbar.add(new JButton(str));
  254.             b.setBackground(state ? Color.green : Color.lightGray);
  255.             b.addActionListener(this);
  256.         }
  257.         public void actionPerformed(ActionEvent e) {
  258.             for (int i = 0; i < toolbar.getComponentCount(); i++) {
  259.                 JButton b = (JButton) toolbar.getComponentAtIndex(i);
  260.                 b.setBackground(Color.lightGray);
  261.             }
  262.             JButton b = (JButton) e.getSource();
  263.             b.setBackground(Color.green);
  264.             if (b.getText().equals("Scale")) {
  265.                 demo.transformType = demo.SCALE;
  266.                 demo.direction = demo.RIGHT;
  267.                 demo.sx = demo.sy = 1;
  268.             } else if (b.getText().equals("Shear")) {
  269.                 demo.transformType = demo.SHEAR;
  270.                 demo.direction = demo.RIGHT;
  271.                 demo.sx = demo.sy = 0;
  272.             } else if (b.getText().equals("Rotate")) {
  273.                 demo.transformType = demo.ROTATE;
  274.                 demo.angdeg = 0;
  275.             }
  276.         }
  277.         public Dimension getPreferredSize() {
  278.             return new Dimension(200,36);
  279.         }
  280.         public void start() {
  281.             if (thread != null) {
  282.                 return;
  283.             }
  284.             thread = new Thread(this);
  285.             thread.setPriority(Thread.MIN_PRIORITY);
  286.             thread.setName("Transforms.SelectTx DemoControls Thread");
  287.             thread.start();
  288.         }
  289.         public synchronized void stop() {
  290.             if (thread != null) {
  291.                 thread.interrupt();
  292.             }
  293.             thread = null;
  294.             notifyAll();
  295.         }
  296.         public void run() {
  297.             Thread me = Thread.currentThread();
  298.             demo.transformToggle = demo.transformType;
  299.             while (thread == me) {
  300.                 try {
  301.                     thread.sleep(222);
  302.                 } catch (InterruptedException e) { return; }
  303.                 if (demo.transformToggle != demo.transformType) {
  304.                     ((JButton) toolbar.getComponentAtIndex(demo.transformToggle)).doClick();
  305.                 }
  306.             }
  307.             thread = null;
  308.         }
  309.     } // End DemoControls class
  310. } // End SelectTx class