home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / AFC / FxShapes / Src / PaintPnl.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  8.7 KB  |  335 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.Point;
  8. import java.awt.Dimension;
  9. import java.awt.Rectangle;
  10. import com.ms.ui.*;
  11. import com.ms.fx.*;
  12.  
  13. public class PaintPnl extends UIPanel implements SDKConsts, IFxTextConstants
  14. {
  15.     private FxGraphics dragGraf;
  16.     private int drawmode;
  17.     private FxRubberPen rubber;
  18.     private int xprev, yprev, xcur, ycur, penwidth, penstyle;
  19.     private int previdx, curidx, numpoints;
  20.     private Point points[] = new Point[MAX_POINTS];
  21.     private FxStyledPen dash, dot;
  22.     private FxPen bpen, tpen;
  23.     private FxTexture texture, bg;
  24.     private FxFont efont, rfont;
  25.     private FxFormattedText ft_right;
  26.  
  27.     public PaintPnl()
  28.     {
  29.         setLayout(new UIBorderLayout());
  30.  
  31.         xprev = 40; yprev = 40; xcur = 180; ycur = 220;
  32.         penwidth = INIT_WIDTH; numpoints = MAX_POINTS;
  33.         drawmode = ID_LINE; penstyle = ID_SOLID;
  34.         previdx = 0; curidx = 1;
  35.  
  36.         efont = new FxFont("Times New Roman", 0, 28);
  37.         rfont = new FxFont("Times New Roman", 0, 20);
  38.  
  39.         points[0] = new Point(175,190);    points[1] = new Point(175,50);
  40.         points[2] = new Point(225,50);    points[3] = new Point(225,215);
  41.         points[4] = new Point(175,265);    points[5] = new Point(100,265);
  42.         points[6] = new Point(50,215);    points[7] = new Point(100,190);
  43.         points[8] = new Point(125,215);    points[9] = new Point(150,215);
  44.  
  45.         // Create background texture
  46.         bg = new FxTexture(SDKImages.get(BACKGROUND), FxTexture.STRETCH_NONE,
  47.                             0, 0, -1, -1, false, 207,178,148);
  48.         setBackground(bg);
  49.  
  50.         // Create rubber pen
  51.         rubber = new FxRubberPen(bg);
  52.  
  53.         // Create texture for textured pen
  54.         texture = new FxTexture(SDKImages.get(TEXTURE), FxTexture.STRETCH_NONE,
  55.                                     0, 0, -1, -1, false, 237,207,39);
  56.  
  57.         // Create all the Pens
  58.         FxColor blue = new FxColor(0,0,255);
  59.         bpen = new FxPen(INIT_WIDTH, blue);
  60.         dash = new FxStyledPen(FxStyledPen.DASH, 1, blue);
  61.         dot = new FxStyledPen(FxStyledPen.DOT, 1, blue);
  62.         tpen = new FxPen(INIT_WIDTH, texture);
  63.  
  64.         // Create Formatted Text
  65.         ft_right = new FxFormattedText(FT_HELLO);
  66.         ft_right.setTextDirection(tdTB_RL);
  67.         ft_right.setHorizAlign(htaCenter);
  68.         ft_right.setVertAlign(vtaCenter);
  69.         ft_right.setFont(rfont);
  70.     }
  71.  
  72.     public void setPenStyle(int style, boolean repaint)
  73.     {
  74.         penstyle = style;
  75.         if ( repaint )
  76.             repaint();
  77.     }
  78.  
  79.     public void setDrawMode(int mode) { drawmode = mode; repaint(); }
  80.  
  81.     public void setPenWidth(int width) { penwidth = width; repaint(); }
  82.  
  83.     public void setNumPoints(int num) { numpoints = num; repaint(); }
  84.  
  85.     public boolean mouseDown(Event evt, int x, int y)
  86.     {
  87.         // Make sure we are within bounds of panel
  88.         Dimension pnlsize = getSize();
  89.         x = bindToPanel(x, pnlsize.width); y = bindToPanel(y, pnlsize.height);
  90.  
  91.         if ( drawmode != ID_POLY ) {
  92.             if ( (xprev != x) || (yprev != y) ) {
  93.                 xprev = x;
  94.                 yprev = y;
  95.             }
  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;
  105.         int xStart, yStart;
  106.  
  107.         if ( drawmode != ID_POLY ) {
  108.             xStart = xprev; yStart = yprev;
  109.         }
  110.         else {
  111.             xStart = points[previdx].x; yStart = points[previdx].y;
  112.         }
  113.  
  114.         // Make sure we are within bounds of panel
  115.         Dimension pnlsize = getSize();
  116.         x = bindToPanel(x, pnlsize.width); y = bindToPanel(y, pnlsize.height);
  117.  
  118.         switch ( drawmode ) {
  119.         case ID_LINE:    
  120.             dragGraf.drawLine(xStart, yStart, x, y);
  121.             break;
  122.  
  123.         case ID_POLY:
  124.             points[curidx] = new Point(x,y);
  125.             dragGraf.drawPolygon(MakeArray.x(points), MakeArray.y(points), numpoints);
  126.             break;
  127.  
  128.         case ID_FORMAT_TEXT:
  129.         case ID_ELLIPSE:
  130.         case ID_LINE_ELLIPSE:
  131.             pt = new SortPoints(xStart, yStart, x, y);
  132.             dragGraf.drawOval(pt.xStart, pt.yStart, pt.width, pt.height);
  133.             break;
  134.  
  135.         case ID_RECT:
  136.             pt = new SortPoints(xStart, yStart, x, y);
  137.             dragGraf.drawRect(pt.xStart, pt.yStart, pt.width, pt.height);
  138.             break;
  139.  
  140.         case ID_ROUNDRECT:    
  141.             pt = new SortPoints(xStart, yStart, x, y);
  142.             dragGraf.drawRoundRect(pt.xStart, pt.yStart, 
  143.                                    pt.width,  pt.height, 
  144.                                    pt.width/6,pt.height/6);
  145.             break;
  146.         }
  147.         return true;
  148.     }
  149.  
  150.     public boolean mouseUp(Event evt, int x, int y)
  151.     {
  152.         // Erase last thing drawn by rubber pen
  153.         rubber.drawLast(dragGraf); dragGraf = null;
  154.         
  155.         // Make sure we are within bounds of panel
  156.         Dimension pnlsize = getSize();
  157.         x = bindToPanel(x, pnlsize.width); y = bindToPanel(y, pnlsize.height);
  158.  
  159.         if ( drawmode != ID_POLY ) {
  160.             if ( (xprev != x) || (yprev != y) ) {
  161.                 xcur = x;
  162.                 ycur = y;
  163.             }
  164.         }
  165.         else {
  166.             if ( (points[previdx].x != x) || (points[previdx].y != y) ) {
  167.                 points[curidx].x = x;
  168.                 points[curidx].y = y;
  169.             }
  170.             previdx = curidx;
  171.             curidx = ((curidx+1) % numpoints);
  172.         }
  173.         repaint();
  174.         return true;
  175.     }
  176.  
  177.     private int bindToPanel(int coord, int size)
  178.     {
  179.         int inset;
  180.  
  181.         if ( drawmode == ID_FORMAT_TEXT )
  182.             inset = FT_INSET;
  183.         else
  184.             inset = penwidth;
  185.  
  186.         if ( coord < inset )
  187.             return inset;
  188.         else if ( coord > (size - inset) )
  189.             return size - inset;
  190.         else 
  191.             return coord;
  192.     }
  193.  
  194.     public void paint(FxGraphics fxg)
  195.     {
  196.         SortPoints pt;
  197.         FxEllipse oval;
  198.  
  199.         switch ( penstyle ) {
  200.         case ID_SOLID: bpen.setWidth(penwidth); fxg.setColor(bpen); break;
  201.         case ID_DASH: fxg.setColor(dash); break;
  202.         case ID_DOT: fxg.setColor(dot); break;
  203.         case ID_TEXTURE:
  204.             if ( penwidth == 2 )
  205.                 tpen.setWidth(3);
  206.             else
  207.                 tpen.setWidth(penwidth);
  208.             fxg.setColor(tpen); break;
  209.         }
  210.  
  211.         switch ( drawmode ) {
  212.         case ID_LINE:    
  213.             fxg.drawLine(xprev, yprev, xcur, ycur);
  214.             break;
  215.  
  216.         case ID_POLY:
  217.             fxg.drawPolygon(MakeArray.x(points), MakeArray.y(points), numpoints);
  218.             break;
  219.  
  220.         case ID_ELLIPSE:
  221.             pt = new SortPoints(xprev, yprev, xcur, ycur);
  222.             fxg.drawOval(pt.xStart, pt.yStart, pt.width, pt.height);
  223.             break;
  224.  
  225.         case ID_RECT:
  226.             pt = new SortPoints(xprev, yprev, xcur, ycur);
  227.             fxg.drawRect(pt.xStart, pt.yStart, pt.width, pt.height);
  228.             break;
  229.  
  230.         case ID_ROUNDRECT:
  231.             pt = new SortPoints(xprev, yprev, xcur, ycur);
  232.             fxg.drawRoundRect(pt.xStart, pt.yStart, pt.width, pt.height, 
  233.                               pt.width/6, pt.height/6);
  234.             break;
  235.  
  236.         case ID_FORMAT_TEXT:
  237.             Rectangle rect;
  238.             Dimension size = getSize();
  239.  
  240.             pt = new SortPoints(xprev, yprev, xcur, ycur);
  241.  
  242.             // Re-originate ellipse
  243.             pt.xStart -= 10; pt.yStart += 10;
  244.  
  245.  
  246.             if ( pt.width < FT_MIN_WIDTH ) {
  247.                 pt.width = FT_MIN_WIDTH;
  248.                 if ( (pt.xStart + FT_MIN_WIDTH + FT_INSET + 20) > size.width )
  249.                     pt.xStart = size.width - FT_MIN_WIDTH - FT_INSET - 20;
  250.             }
  251.  
  252.             if ( pt.height < FT_MIN_HEIGHT ) {
  253.                 pt.height = FT_MIN_HEIGHT;
  254.                 if ( (pt.yStart + FT_MIN_HEIGHT + FT_INSET ) > (size.height + 10) )
  255.                     pt.yStart = size.height - FT_MIN_HEIGHT - FT_INSET + 10;
  256.             }
  257.  
  258.             fxg.setColor(FxColor.red);
  259.  
  260.             // Center vertical text in ellipse
  261.             int xc = 2*(pt.xStart + pt.width/2);
  262.             int yc = 2*(pt.yStart + pt.height/2 - 10);
  263.             rect = new Rectangle(xc, yc);
  264.             ft_right.setBounding(rect, true);
  265.             ft_right.paint(fxg, 0, ft_right.getLength(), 0);
  266.  
  267.             // Draw circular Formatted Text
  268.             fxg.setColor(FxColor.blue);
  269.             oval = new FxEllipse(pt.xStart, pt.yStart, pt.width, pt.height);
  270.             fxg.setFont(efont);
  271.             rect = new Rectangle(0, 0, size.width, size.height);
  272.             fxg.drawStringFormatted("Hello, World.", rect, htaStretch, 
  273.                                     vtaCenter, wwNone, true, 0, null, oval, false);
  274.             break;
  275.  
  276.         case ID_LINE_ELLIPSE:
  277.             pt = new SortPoints(xprev, yprev, xcur, ycur);
  278.  
  279.             oval = new FxEllipse(pt.xStart, pt.yStart, pt.width, pt.height);
  280.  
  281.             int nPoints = oval.getBorderLength();
  282.             FxColor cols[] = new FxColor[4];
  283.             cols[0] = new FxColor(255,255,0);    // yellow
  284.             cols[1] = new FxColor(0,0,0);        // black
  285.             cols[2] = new FxColor(255,255,255);    // white
  286.             cols[3] = new FxColor(0,255,255);    // cyan
  287.             for ( int j = 0; j < nPoints; j++ ) {
  288.                 fxg.setColor(cols[j%4]);
  289.                 Point p = oval.getPoint(j);
  290.                 fxg.drawLine(p.x, p.y, p.x+10, p.y-10);
  291.             }
  292.             break;
  293.         }
  294.     }
  295. }
  296.  
  297. class SortPoints
  298. {
  299.     public int xStart, yStart, width, height;
  300.  
  301.     public SortPoints(int xStart, int yStart, int xEnd, int yEnd)
  302.     {
  303.         width = Math.abs(xEnd - xStart);
  304.         height = Math.abs(yEnd - yStart);
  305.  
  306.         // Assume Start < End
  307.         this.xStart = xStart;
  308.         this.yStart = yStart;
  309.         if ( xStart > xEnd ) this.xStart = xEnd;
  310.         if ( yStart > yEnd ) this.yStart = yEnd;
  311.     }
  312. }
  313.  
  314. final class MakeArray implements SDKConsts
  315. {
  316.     public static final int[] x(Point[] points)
  317.     {
  318.         int[] elems = new int[MAX_POINTS];
  319.  
  320.         for ( int i = 0; i < MAX_POINTS; i++ )
  321.             elems[i] = points[i].x;
  322.  
  323.         return elems;
  324.     }
  325.  
  326.     public static final int[] y(Point[] points)
  327.     {
  328.         int[] elems = new int[MAX_POINTS];
  329.  
  330.         for ( int i = 0; i < MAX_POINTS; i++ )
  331.             elems[i] = points[i].y;
  332.  
  333.         return elems;
  334.     }
  335. }