home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / Pen.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  6.8 KB  |  218 lines

  1. /*
  2.  * Copyright (c) 1997 Krumel & Associates, Inc. All Rights Reserved.
  3.  *
  4.  * www.krumel.com - controls@krumel.com
  5.  *
  6.  * Permission is given to the buyer of this package for one software
  7.  * developer to use this software on one CPU (one workstation) and to make
  8.  * one backup copy.  You may uitilize and/or modify this class for use in your
  9.  * projects.  You may distribute or sell any executable which results from
  10.  * using this code in yur application, except a utility or class of similar
  11.  * nature to this product.  You may distribute this product in compiled
  12.  * form only, but soley to be used with your cmpiled executable product
  13.  * for the puposes of dynamic loading. You may NOT redistribute the source
  14.  * code in any form or make it accessible through a network or other
  15.  * distribution media to others. Please refer to the file "copyright.html"
  16.  * for further important copyright and licensing information.
  17.  *
  18.  * The source code is the confidential and proprietary information
  19.  * of Krumel & Associates, Inc. ("Confidential Information").  You shall
  20.  * not disclose such Confidential Information and shall use it only in
  21.  * accordance with the terms of the license agreement you entered into
  22.  * with Krumel & Associates, Inc..
  23.  
  24.  * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
  25.  * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
  26.  * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  27.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
  28.  * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  29.  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  30.  */
  31.  
  32.  
  33. package symantec.itools.db.awt.awt;
  34.  
  35. import java.awt.*;
  36.  
  37. public class Pen implements java.io.Serializable {
  38.     Graphics graphics;
  39.  
  40.     //not implemented yet
  41.     int penWidth = 1;
  42.     Color color = Color.black;
  43.     int dashLength = SHORT_DASH;
  44.     int dashSeparation = 2;
  45.  
  46.     public static final int SHORT_DASH = 2;
  47.     public static final int MED_DASH = 5;
  48.     public static final int LONG_DASH = 8;
  49.  
  50.     public Pen() {}
  51.  
  52.     public Pen(Color c) {
  53.         this(null, 1, c);
  54.     }
  55.  
  56.     public Pen(Graphics g) {
  57.         this(g, 1, g.getColor());
  58.     }
  59.  
  60.     public Pen(Graphics g, Color c) {
  61.         this(g, 1, c);
  62.     }
  63.  
  64.     Pen(Graphics g, int w, Color c) {
  65.         graphics = g;
  66.         penWidth = w;
  67.         color = c;
  68.     }
  69.  
  70.     public void setGraphics(Graphics g) { graphics = g; }
  71.  
  72.     public Graphics getGraphics() { return graphics; }
  73.  
  74. //    public void setPenWidth(int w) { penWidth = w; }
  75.  
  76. //    public int getPenWidth() { return penWidth; }
  77.  
  78.     public void setColor(Color c) { color = c; }
  79.  
  80.     public Color getColor() { return color; }
  81.  
  82.     public void setDashLength(int len) { dashLength = len; }
  83.  
  84.     public int getDashLength() { return dashLength; }
  85.  
  86.     public void setDashSeparation(int sep) { dashSeparation = sep; }
  87.  
  88.     public int getDashSeparation() { return dashSeparation; }
  89.  
  90.     public void drawDashLine(int x1, int y1, int x2, int y2) {
  91.         synchronized(graphics) {
  92.             for(int i=0; i<penWidth; i++) {
  93.                 Color origC = graphics.getColor();
  94.                 graphics.setColor(color);
  95.                 drawDashLine0(x1, y1, x2, y2);
  96.                 graphics.setColor(origC);
  97.             }
  98.         }
  99.     }
  100.  
  101.     public void drawLine(int x1, int y1, int x2, int y2) {
  102.         synchronized(graphics) {
  103.             for(int i=0; i<penWidth; i++) {
  104.                 Color origC = graphics.getColor();
  105.                 graphics.setColor(color);
  106.                 graphics.drawLine(x1, y1, x2, y2);
  107.                 graphics.setColor(origC);
  108.             }
  109.         }
  110.     }
  111.  
  112.     public void drawDashRect(int x, int y, int w, int h) {
  113.         drawDashLine(x, y, x, y+h-1);
  114.         drawDashLine(x, y, x+w-1, y);
  115.         drawDashLine(x+w, y, x+w, y+h-1);
  116.         drawDashLine(x, y+h-1, x+w-1, y+h-1);
  117.     }
  118.  
  119.     public void drawRect(int x, int y, int w, int h) {
  120.         synchronized(graphics) {
  121.             for(int i=0; i<penWidth; i++) {
  122.                 Color origC = graphics.getColor();
  123.                 graphics.setColor(color);
  124.                 graphics.drawRect(x, y, w, h);
  125.                 graphics.setColor(origC);
  126.             }
  127.         }
  128.     }
  129.  
  130.     public void fillRect(int x, int y, int w, int h) {
  131.         synchronized(graphics) {
  132.             Color origC = graphics.getColor();
  133.             graphics.setColor(color);
  134.             graphics.fillRect(x, y, w, h);
  135.             graphics.setColor(origC);
  136.         }
  137.     }
  138.  
  139.     private void drawDashLine0(int x1, int y1, int x2, int y2) {
  140.         if (x1>x2) {
  141.             //the algorithm assumes x1 <= x2 so swap points
  142.             int t = x1;
  143.             x1 = x2;
  144.             x2 = t;
  145.             t = y1;
  146.             y2 = y1;
  147.             y1 = t;
  148.         }
  149.  
  150.         int deltaX = x1 - x2;
  151.         int deltaY = y1 - y2;
  152.         double distX, distY, skipX, skipY;
  153.         int x = x1, y=y1, times;
  154.  
  155.         if (x1 == x2) {
  156.             distX = 0;
  157.             distY = dashLength;
  158.             skipX = 0;
  159.             skipY = dashSeparation;
  160.             times = Math.abs(deltaY/(dashLength+dashSeparation))+1;
  161.         } else if (y1 == y2) {
  162.             distX = dashLength;
  163.             distY = 0;
  164.             skipX = dashSeparation;
  165.             skipY = 0;
  166.             times = Math.abs(deltaX/(dashLength+dashSeparation))+1;
  167.         } else {
  168.             double tan = deltaY/(double)deltaX;
  169.             double radians = Math.atan(tan);
  170.             double cos = Math.cos(radians);
  171.             double sin = Math.sin(radians);
  172.  
  173.             distX = dashLength*cos;
  174.             distY = dashLength*sin;
  175.             skipX = dashSeparation*cos;
  176.             skipY = dashSeparation*sin;
  177.             times = (int)Math.abs(deltaX/(distX+skipX))+1;
  178.         }
  179.  
  180.         for(int i=0; i<times; i++) {
  181.             x = x1 + (int)(i*(distX+skipX));
  182.             y = y1 + (int)(i*(distY+skipY));
  183.             graphics.drawLine(x, y, (int)(x+distX), (int)(y+distY));
  184.         }
  185.  
  186.         //make sure go the last little way
  187.         graphics.drawLine(x2, y2, (int)(x2-distX), (int)(y2-distY));
  188.     }
  189.  
  190. //    public static void main(String args[]) {
  191. //        new PenFrame();
  192. //    }
  193. }
  194.  
  195. /*
  196. class PenFrame extends Frame {
  197.     Pen pen;
  198.  
  199.     public PenFrame() {
  200.         super("Pen Test");
  201.         resize(300, 300);
  202.         show();
  203.     }
  204.  
  205.     public void addNotify() {
  206.         super.addNotify();
  207.         pen = new Pen(getGraphics(), Color.black);
  208.     }
  209.  
  210.     public boolean mouseDown(Event e, int x, int y) {
  211.         pen.drawDashLine(100, 100, 300, 300);
  212.         pen.drawDashLine(10, 100, 200, 100);
  213.         pen.drawDashLine(50, 10, 50, 200);
  214.         pen.drawDashRect(25, 25, 200, 200);
  215.         return true;
  216.     }
  217. }
  218. */