home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / AFC102 / Samples / FxShapes / Src / PaintPnl.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  13.1 KB  |  337 lines

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