home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / AFC / FxTextures / Src / TexturePnl.java < prev   
Encoding:
Java Source  |  1998-03-05  |  4.3 KB  |  175 lines

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4. import java.awt.Insets;
  5. import java.awt.Event;
  6. import java.awt.Image;
  7. import java.awt.Dimension;
  8. import com.ms.ui.*;
  9. import com.ms.fx.*;
  10.  
  11. public class TexturePnl extends UIPanel implements SDKConsts
  12. {
  13.     private FxGraphics dragGraf;
  14.     private int mode;
  15.     private FxTexture cross;
  16.     private FxRubberPen rubber;
  17.     private int xprev, yprev, xcur, ycur;
  18.     private boolean snapto;
  19.  
  20.     public TexturePnl()
  21.     {
  22.         setLayout(new UIBorderLayout());
  23.         setBackground(new FxColor(180,135,148));
  24.         xprev = 20; yprev = 20; xcur = 180; ycur = 220;
  25.         snapto = false;
  26.  
  27.         //
  28.         // Create FxRubberPen
  29.         //
  30.         rubber = new FxRubberPen(new FxColor(180,135,148));
  31.  
  32.         //
  33.         // Create FxTexture for drawing
  34.         //
  35.         // L_LEFT, L_TOP, L_RIGHT, L_BOTTOM define partitioning of texture
  36.         //
  37.         cross = new FxTexture(SDKImages.get(CROSS), FxTexture.STRETCH_NONE, 
  38.                               L_LEFT, L_TOP, L_RIGHT, L_BOTTOM, false);
  39.  
  40.         // set partial texture mode
  41.         cross.setSnapDraw(snapto);
  42.     }
  43.  
  44.     /*************************************************************************
  45.     
  46.     setMode() - method for setting texture stretch mode
  47.  
  48.     *************************************************************************/
  49.     public void setMode(int mode)
  50.     {
  51.         cross.setStretch(mode);
  52.         cross.setSnapDraw(snapto);
  53.         repaint();
  54.     }
  55.  
  56.     /*************************************************************************
  57.  
  58.     setSnapTo() - method for enabling/disabling drawing of partial textures
  59.  
  60.     *************************************************************************/
  61.     public void setSnapTo(boolean set)
  62.     {
  63.         snapto = set;
  64.         cross.setSnapDraw(set);
  65.         repaint();
  66.     }
  67.  
  68.     /*************************************************************************
  69.  
  70.     setTile() - method for enabling/disabling tiling
  71.  
  72.     *************************************************************************/
  73.     public void setTile(boolean set)
  74.     {
  75.         cross.setPinOrigin(set);
  76.         // don't need to repaint if Tiling is being deselected
  77.         if ( set ) {
  78.             cross.setSnapDraw(false);
  79.             repaint();
  80.         }
  81.         else
  82.             cross.setSnapDraw(snapto);
  83.     }
  84.  
  85.     /*************************************************************************
  86.  
  87.     mouseDown(), mouseDrag() - uses FxRubberPen to draw outline of where
  88.         FxTexture will be drawn, draws FxTexture on mouseUp()
  89.  
  90.     *************************************************************************/
  91.     public boolean mouseDown(Event evt, int x, int y)
  92.     {
  93.         if ( (xprev != x) || (yprev != y) ) {
  94.             xprev = x;
  95.             yprev = y;
  96.         }
  97.         dragGraf = getGraphics();
  98.         dragGraf.setColor(rubber);
  99.         return true;
  100.     }
  101.  
  102.     public boolean mouseDrag(Event evt, int x, int y)
  103.     {
  104.         SortPoints pt = new SortPoints(xprev, yprev, x, y);
  105.         dragGraf.drawRect(pt.xStart, pt.yStart, pt.width, pt.height);
  106.         return true;
  107.     }
  108.     
  109.     public boolean mouseUp(Event evt, int x, int y)
  110.     {
  111.         if ( x < 0 ) x = 0;    if ( y < 0 ) y = 0;
  112.         Dimension pnlsize = getSize();
  113.         if ( x > pnlsize.width ) x = pnlsize.width;
  114.         if ( y > pnlsize.height ) y = pnlsize.height;
  115.  
  116.         rubber.drawLast(dragGraf);
  117.         dragGraf = null;
  118.         
  119.         if ( (xprev != x) || (yprev != y) ) {
  120.             xcur = x;
  121.             ycur = y;
  122.         }
  123.         repaint();
  124.         return true;
  125.     }
  126.  
  127.     /*************************************************************************
  128.  
  129.     paint() - draw FxTexture
  130.  
  131.     *************************************************************************/
  132.     public void paint(FxGraphics fxg)
  133.     {
  134.         Dimension pnlsize = getSize();
  135.         SortPoints pt;
  136.         pt = new SortPoints(xprev, yprev, xcur, ycur);
  137.  
  138.         // Only draw minimum size
  139.         if ( pt.width < 45 )
  140.             pt.width = 45;
  141.         if ( pt.height < 65 )
  142.             pt.height = 65;
  143.  
  144.         // Don't draw passed panel boundary
  145.         if ( (pt.xStart + pt.width) > pnlsize.width )
  146.             pt.xStart = pnlsize.width - pt.width;
  147.         if ( (pt.yStart + pt.height) > pnlsize.height )
  148.             pt.yStart = pnlsize.height - pt.height;
  149.  
  150.         //
  151.         // Draw FxTexture
  152.         //
  153.         fxg.setColor(cross);
  154.         fxg.fillRect(pt.xStart, pt.yStart, pt.width, pt.height);
  155.     }
  156. }
  157.  
  158. class SortPoints
  159. {
  160.     public int xStart, yStart, width, height;
  161.  
  162.     public SortPoints(int xStart, int yStart, int xEnd, int yEnd)
  163.     {
  164.         width = Math.abs(xEnd - xStart);
  165.         height = Math.abs(yEnd - yStart);
  166.  
  167.         // Assume Start < End
  168.         this.xStart = xStart;
  169.         this.yStart = yStart;
  170.         if ( xStart > xEnd ) this.xStart = xEnd;
  171.         if ( yStart > yEnd ) this.yStart = yEnd;
  172.     }
  173. }
  174.  
  175.