home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 6.8 KB | 218 lines |
- /*
- * Copyright (c) 1997 Krumel & Associates, Inc. All Rights Reserved.
- *
- * www.krumel.com - controls@krumel.com
- *
- * Permission is given to the buyer of this package for one software
- * developer to use this software on one CPU (one workstation) and to make
- * one backup copy. You may uitilize and/or modify this class for use in your
- * projects. You may distribute or sell any executable which results from
- * using this code in yur application, except a utility or class of similar
- * nature to this product. You may distribute this product in compiled
- * form only, but soley to be used with your cmpiled executable product
- * for the puposes of dynamic loading. You may NOT redistribute the source
- * code in any form or make it accessible through a network or other
- * distribution media to others. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * The source code is the confidential and proprietary information
- * of Krumel & Associates, Inc. ("Confidential Information"). You shall
- * not disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Krumel & Associates, Inc..
-
- * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
- * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
- * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
- * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
- * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
-
- package symantec.itools.db.awt.awt;
-
- import java.awt.*;
-
- public class Pen implements java.io.Serializable {
- Graphics graphics;
-
- //not implemented yet
- int penWidth = 1;
- Color color = Color.black;
- int dashLength = SHORT_DASH;
- int dashSeparation = 2;
-
- public static final int SHORT_DASH = 2;
- public static final int MED_DASH = 5;
- public static final int LONG_DASH = 8;
-
- public Pen() {}
-
- public Pen(Color c) {
- this(null, 1, c);
- }
-
- public Pen(Graphics g) {
- this(g, 1, g.getColor());
- }
-
- public Pen(Graphics g, Color c) {
- this(g, 1, c);
- }
-
- Pen(Graphics g, int w, Color c) {
- graphics = g;
- penWidth = w;
- color = c;
- }
-
- public void setGraphics(Graphics g) { graphics = g; }
-
- public Graphics getGraphics() { return graphics; }
-
- // public void setPenWidth(int w) { penWidth = w; }
-
- // public int getPenWidth() { return penWidth; }
-
- public void setColor(Color c) { color = c; }
-
- public Color getColor() { return color; }
-
- public void setDashLength(int len) { dashLength = len; }
-
- public int getDashLength() { return dashLength; }
-
- public void setDashSeparation(int sep) { dashSeparation = sep; }
-
- public int getDashSeparation() { return dashSeparation; }
-
- public void drawDashLine(int x1, int y1, int x2, int y2) {
- synchronized(graphics) {
- for(int i=0; i<penWidth; i++) {
- Color origC = graphics.getColor();
- graphics.setColor(color);
- drawDashLine0(x1, y1, x2, y2);
- graphics.setColor(origC);
- }
- }
- }
-
- public void drawLine(int x1, int y1, int x2, int y2) {
- synchronized(graphics) {
- for(int i=0; i<penWidth; i++) {
- Color origC = graphics.getColor();
- graphics.setColor(color);
- graphics.drawLine(x1, y1, x2, y2);
- graphics.setColor(origC);
- }
- }
- }
-
- public void drawDashRect(int x, int y, int w, int h) {
- drawDashLine(x, y, x, y+h-1);
- drawDashLine(x, y, x+w-1, y);
- drawDashLine(x+w, y, x+w, y+h-1);
- drawDashLine(x, y+h-1, x+w-1, y+h-1);
- }
-
- public void drawRect(int x, int y, int w, int h) {
- synchronized(graphics) {
- for(int i=0; i<penWidth; i++) {
- Color origC = graphics.getColor();
- graphics.setColor(color);
- graphics.drawRect(x, y, w, h);
- graphics.setColor(origC);
- }
- }
- }
-
- public void fillRect(int x, int y, int w, int h) {
- synchronized(graphics) {
- Color origC = graphics.getColor();
- graphics.setColor(color);
- graphics.fillRect(x, y, w, h);
- graphics.setColor(origC);
- }
- }
-
- private void drawDashLine0(int x1, int y1, int x2, int y2) {
- if (x1>x2) {
- //the algorithm assumes x1 <= x2 so swap points
- int t = x1;
- x1 = x2;
- x2 = t;
- t = y1;
- y2 = y1;
- y1 = t;
- }
-
- int deltaX = x1 - x2;
- int deltaY = y1 - y2;
- double distX, distY, skipX, skipY;
- int x = x1, y=y1, times;
-
- if (x1 == x2) {
- distX = 0;
- distY = dashLength;
- skipX = 0;
- skipY = dashSeparation;
- times = Math.abs(deltaY/(dashLength+dashSeparation))+1;
- } else if (y1 == y2) {
- distX = dashLength;
- distY = 0;
- skipX = dashSeparation;
- skipY = 0;
- times = Math.abs(deltaX/(dashLength+dashSeparation))+1;
- } else {
- double tan = deltaY/(double)deltaX;
- double radians = Math.atan(tan);
- double cos = Math.cos(radians);
- double sin = Math.sin(radians);
-
- distX = dashLength*cos;
- distY = dashLength*sin;
- skipX = dashSeparation*cos;
- skipY = dashSeparation*sin;
- times = (int)Math.abs(deltaX/(distX+skipX))+1;
- }
-
- for(int i=0; i<times; i++) {
- x = x1 + (int)(i*(distX+skipX));
- y = y1 + (int)(i*(distY+skipY));
- graphics.drawLine(x, y, (int)(x+distX), (int)(y+distY));
- }
-
- //make sure go the last little way
- graphics.drawLine(x2, y2, (int)(x2-distX), (int)(y2-distY));
- }
-
- // public static void main(String args[]) {
- // new PenFrame();
- // }
- }
-
- /*
- class PenFrame extends Frame {
- Pen pen;
-
- public PenFrame() {
- super("Pen Test");
- resize(300, 300);
- show();
- }
-
- public void addNotify() {
- super.addNotify();
- pen = new Pen(getGraphics(), Color.black);
- }
-
- public boolean mouseDown(Event e, int x, int y) {
- pen.drawDashLine(100, 100, 300, 300);
- pen.drawDashLine(10, 100, 200, 100);
- pen.drawDashLine(50, 10, 50, 200);
- pen.drawDashRect(25, 25, 200, 200);
- return true;
- }
- }
- */