home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 20.0 KB | 884 lines |
- import java.awt.*;
- import java.util.*;
- import java.io.*;
-
- /**
- * A class that implements vector drawing
- *
- * @version 1.0 03/24/96
- * @author Kang, Dae Woong (namu@star.elim.net)
- */
-
- class Draw extends Rectangle
- {
- int method;
- int content;
- int shape;
- Color color = new Color(0);
- /**
- * parameter saving place
- */
- Vector intParams = null;
-
- double lineAngle = 0;
-
- int fontStyle;
- String fontName = null;
- String text = null;
-
- String urlName = null;
-
- // used OBJECT Drawing
- ObjTank objTank;
-
- ///// used WorkPanel
- static int curMethod;
- static int curShape;
- static int curContent;
- static Color curColor;
-
- static int curFontStyle;
- static String curFontName;
-
- boolean isSelected = false;
-
- /**
- * final variables
- */
- static final int INVALID_PARAM = 0x7FFFFFFF;
-
- //draw method ======================
- static final int LINE = 0;
- static final int RECT = 1;
- static final int ROUND_RECT = 2;
- static final int POLYGON = 3;
- static final int OVAL = 4;
- static final int PIE = 5;
- static final int ARC = 6;
- static final int STRING = 7;
- static final int IMAGE = 8;
- static final int DRAW = 9;
- static final int SELECT = 10;
- static final int ANCHOR = 11;
-
- static final int METHOD_COUNT = 12;
-
- //draw content ======================
- static final int CONTENT_FILL = 0;
- static final int CONTENT_UNFILL = 1;
-
- static final int CONTENT_COUNT = 2;
-
- //draw shape =========================
- static final int SHAPE_PLAIN = 0;
- static final int SHAPE_UP = 1;
- static final int SHAPE_DOWN = 2;
-
- static final int SHAPE_COUNT = 3;
-
- //=====================
- static final int URL_FONT_HEIGHT = 20;
-
- //********** constructor's *********************************
- public Draw(int method, int content, int shape, Color color, int x, int y, int width, int height)
- {
- super(x, y, width, height);
- this.method = method;
- this.content = content;
- this.shape = shape;
- this.color = new Color(color.hashCode());
- }
- /**
- * used in reading
- */
- public Draw()
- {
- }
-
- public Draw(int x, int y, int width, int height)
- {
- super(x, y, width, height);
- this.method = curMethod;
- this.content = curContent;
- this.shape = curShape;
- color = new Color(curColor.hashCode());
- }
-
- public Draw(Draw dd)
- {
- reshape(dd.x, dd.y, dd.width, dd.height);
-
- method = dd.method;
- shape = dd.shape;
- content = dd.content;
- color = new Color(dd.color.hashCode());
-
- if (dd.hasText())
- {
- fontStyle = dd.fontStyle;
- fontName = new String(dd.fontName);
- text = new String(dd.text);
- }
- if (dd.hasURL())
- urlName = new String(dd.urlName);
-
- if (method == IMAGE || method == DRAW)
- {
- dd.objTank.refContent(this);
- width = dd.width;
- height = dd.height;
- }
- lineAngle = dd.lineAngle;
- if (dd.intParams != null)
- {
- Enumeration e = dd.intParams.elements();
- while (e.hasMoreElements())
- addParam( ( (Integer) e.nextElement() ).intValue() );
- }
- }
-
- //********** read/write *********************************
- public void write(DataOutputStream dos, Vector obj) throws IOException
- {
-
- try
- {
- dos.writeShort(x);
- dos.writeShort(y);
- dos.writeShort(width);
- dos.writeShort(height);
-
- dos.writeByte(method);
- dos.writeByte(shape);
- dos.writeByte(content);
- dos.writeInt(color.hashCode());
-
- int count = (intParams == null) ? 0 : intParams.size();
- int i = 0;
-
- dos.writeShort(count);
- while (i < count)
- {
- dos.writeShort(getParam(i));
- i++;
- }
-
- if (method == STRING)
- {
- dos.writeInt(fontStyle);
- dos.writeByte(fontName.length());
- dos.writeBytes(fontName);
- dos.writeByte(text.length());
- dos.writeBytes(text);
- }
-
- if (method == IMAGE || method == DRAW)
- dos.writeShort(obj.indexOf(objTank));
-
- if (hasURL())
- {
- dos.writeByte(urlName.length());
- dos.writeBytes(urlName);
- }
- else
- {
- dos.writeByte(0);
- }
- }
- catch (IOException ioe)
- throw ioe;
- }
-
- public void read(DataInputStream dis, Vector obj) throws IOException
- {
-
- try
- {
- x = dis.readShort();
- y = dis.readShort();
- width = dis.readShort();
- height = dis.readShort();
-
- method = dis.readByte();
- shape = dis.readByte();
- content = dis.readByte();
- color = new Color(dis.readInt());
-
- int count = dis.readShort();
- for (int i = 0; i < count; i++)
- addParam(dis.readShort() );
-
- if (method == STRING)
- {
- int len;
- byte[] buf;
-
- fontStyle = dis.readInt();
-
- len = dis.readByte();
- buf = new byte[len];
- dis.readFully(buf);
- fontName = new String(buf, 0);
-
- len = dis.readByte();
- buf = new byte[len];
- dis.readFully(buf);
- text = new String(buf, 0);
- }
-
- if (method == IMAGE || method == DRAW)
- {
- int index = dis.readShort();
- if (obj != null)
- {
- objTank = (ObjTank) obj.elementAt(index);
- }
- }
-
- int len;
- len = dis.readByte();
- if (len > 0)
- {
- byte[] buf = new byte[len];
- dis.readFully(buf);
- urlName = new String(buf, 0);
- }
-
- if (method == LINE)
- setLineAngle();
-
- }
- catch (IOException ioe)
- throw ioe;
- }
-
- //***************** parameter function ********************************
- public int getParaCount()
- {
- return intParams.size();
- }
-
- public void addParam(int p)
- {
- if (intParams == null)
- intParams = new Vector();
- intParams.addElement(new Integer(p));
- }
- public void addParam(int p1, int p2)
- {
- addParam(p1);
- addParam(p2);
- }
- public void addParam(int p1, int p2, int p3, int p4)
- {
- addParam(p1);
- addParam(p2);
- addParam(p3);
- addParam(p4);
- }
-
- /**
- * index of param
- * if error return INVALID_PARAM
- */
- public int getParam(int i)
- {
- try
- {
- return ((Integer) intParams.elementAt(i)).intValue();
- }
- catch (ArrayIndexOutOfBoundsException e)
- {
- System.err.println("Draw.getParam invalide index :" + i);
- return INVALID_PARAM;
- }
- }
-
-
- public void setParam(int i, int value)
- {
- try
- {
- intParams.setElementAt(new Integer(value), i);
- }
- catch (ArrayIndexOutOfBoundsException e)
- {
- System.err.println("param set error");
- }
- }
-
- public void setParam(int i, int value1, int value2)
- {
- setParam(i, value1);
- setParam(i + 1, value2);
- }
-
- //********************* point function *********************************
- public Point getCenterPoint() { return new Point(x + width/2, y + height/2); }
- public int bottom() { return y + height; }
- public int right() { return x + width; }
-
- public int getPointsCount()
- {
- switch (method)
- {
- case LINE:
- case POLYGON:
- return getParaCount()/2;
- case ROUND_RECT: return 1;
- case PIE:
- case ARC:
- return 2;
- default: return 0;
- }
- }
-
- public Point getPoint(int i)
- {
- switch (method)
- {
- case POLYGON:
- case LINE:
- return new Point(getParam(2*i), getParam(2*i + 1));
- case ROUND_RECT:
- return new Point(x + getParam(0)/2, y + getParam(1)/2);
- case PIE:
- case ARC:
- Point pt = getCenterPoint();
- double a = ((i == 0) ? getParam(0) : getParam(0) + getParam(1)) / 180.0 * Math.PI ;
- return new Point(pt.x + (int)(width/2*Math.cos(a)), pt.y - (int)(height/2*Math.sin(a)));
- default:
- return null;
- }
- }
-
- public void setPoint(int i, int x, int y)
- {
- switch (method)
- {
- case POLYGON:
- case LINE:
- setParam(2*i, x, y);
- break;
- case ROUND_RECT:
- setParam(0, (x - this.x)*2, (y - this.y)*2);
- break;
- case PIE:
- case ARC:
- Point pt = getCenterPoint();
- int a = (x == pt.x) ? ((y < pt.y) ? 90 : 270) : (int)(Math.atan( (double)(pt.y - y) / (x - pt.x) ) * 180 / Math.PI);
- if (x < pt.x)
- a += 180;
- if (a < 0)
- a += 360;
- if (i == 0)
- {
- if (getParam(1) + getParam(0) >= a)
- setParam(0, a, (getParam(1) + getParam(0) - a) % 360);
- else
- setParam(0, a, (getParam(1) + getParam(0) - a) + 360);
- }
- else
- {
- if (a < getParam(0))
- a += 360;
- setParam(1, (a - getParam(0)) % 360);
- }
- break;
- }
- }
-
- public void setLastPoint(int value1, int value2)
- {
- int i = intParams.size() - 2;
- setParam(i, value1);
- setParam(i + 1, value2);
- }
-
- public Polygon getPolygon()
- {
- Polygon plg = new Polygon();
-
- Enumeration e = intParams.elements();
- while (e.hasMoreElements())
- plg.addPoint(((Integer) e.nextElement()).intValue(), ((Integer) e.nextElement()).intValue());
-
- return plg;
- }
-
- //************************************************************************
- public void toggleSelect()
- {
- isSelected = !isSelected;
- }
- public boolean hasText()
- {
- return text != null && text.length() > 0;
- }
- public boolean hasURL()
- {
- return urlName != null && urlName.length() > 0;
- }
-
- public boolean isPointsOnlyType()
- {
- return method == LINE || method == POLYGON;
- }
-
- public boolean isLineType()
- {
- return method == LINE || method == ARC || (method == POLYGON && content == CONTENT_UNFILL);
- }
-
- public boolean is3D()
- {
- return shape != SHAPE_PLAIN;
- }
-
- public boolean isFill()
- {
- return content == CONTENT_FILL;
- }
-
- public boolean isYNearAngle(int i) // is it near y line?
- {
- double angle = (getParam(i + 2) == getParam(i)) ? Math.PI/2 : Math.atan((double)(getParam(i + 3) - getParam(i + 1))/(getParam(i + 2) - getParam(i)));
- return angle > Math.PI/4 || angle < -Math.PI/4;
- }
-
- public boolean isTopLeftAngle(int angle)
- {
- angle %= 360;
- return angle >= 45 && angle < 225;
- }
-
- public void setRect(Rectangle rc)
- {
- x = rc.x;
- y = rc.y;
- width = rc.width;
- height = rc.height;
- }
- public void setRect(int x, int y, int width, int height)
- {
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- }
-
- public void setLineAngle()
- {
- lineAngle = (getParam(2) == getParam(0)) ? Math.PI/2 : Math.atan((double)(getParam(3) - getParam(1))/(getParam(2) - getParam(0)));
- }
-
-
- public Rectangle getClipRect()
- {
- Rectangle rc = new Rectangle(x, y, width, height);
-
- rc.grow(2, 2);
- if (curMethod == ANCHOR && hasURL())
- {
- FontMetrics fm = WorkPanel.me.getFontMetrics(getURLFont());
- int len = fm.stringWidth(urlName);
- if (rc.width < len)
- {
- rc.x -= (len - rc.width)/2;
- rc.width = len;
- }
-
- }
-
- return rc;
- }
-
- public void transelate(int w, int h)
- {
- super.translate(w, h);
- if (isPointsOnlyType())
- {
- int count = getPointsCount();
- for (int i = 0; i < count; i++)
- {
- Point pt = getPoint(i);
- setPoint(i, pt.x + w, pt.y + h);
- }
- }
- }
-
- public void move(int xx, int yy)
- {
- int w = xx - x;
- int h = yy - y;
- if (isPointsOnlyType())
- {
- int count = getPointsCount();
- for (int i = 0; i < count; i++)
- {
- Point pt = getPoint(i);
- setPoint(i, pt.x + w, pt.y + h);
- }
- }
- super.move(xx, yy);
- }
-
- public void multiply(double w, double h)
- {
- x *= w;
- y *= h;
- width *= w;
- height *= h;
- if (isPointsOnlyType())
- {
- int count = getPointsCount();
- for (int i = 0; i < count; i++)
- {
- Point pt = getPoint(i);
- setPoint(i, (int)(pt.x*w), (int)(pt.y*h));
- }
- }
- }
-
- public int getFontSize()
- {
- return height*4/5;
- }
-
-
- public Font getURLFont()
- {
- return new Font("Dialog", Font.PLAIN, URL_FONT_HEIGHT*3/4);
- }
-
-
- public void drawAngleLine(Graphics g, int angle)
- {
- Point pt = getCenterPoint();
- double a = angle / 180.0 * Math.PI;
-
- g.drawLine(pt.x, pt.y, pt.x + (int)(width/2*Math.cos(a)), pt.y - (int)(height/2*Math.sin(a)));
- }
-
- public void draw(Graphics g)
- {
- Color clrTopLeft = null, clrBottomRight = null;
-
- if (shape == SHAPE_UP)
- {
- clrTopLeft = color.brighter();
- clrBottomRight = color.darker();
- }
- else if (shape == SHAPE_DOWN)
- {
- clrTopLeft = color.darker();
- clrBottomRight = color.brighter();
- }
-
- switch (method)
- {
- case LINE:
- g.setColor(color);
- g.drawLine(getParam(0), getParam(1), getParam(2), getParam(3));
- if (is3D())
- {
- if (lineAngle > Math.PI / 4 || lineAngle < -Math.PI / 4)
- {
- g.setColor(clrTopLeft);
- g.drawLine(getParam(0) - 1, getParam(1), getParam(2) - 1, getParam(3));
- g.setColor(clrBottomRight);
- g.drawLine(getParam(0) + 1, getParam(1), getParam(2) + 1, getParam(3));
- }
- else
- {
- g.setColor(clrTopLeft);
- g.drawLine(getParam(0), getParam(1) - 1, getParam(2), getParam(3) - 1);
- g.setColor(clrBottomRight);
- g.drawLine(getParam(0), getParam(1) + 1, getParam(2), getParam(3) + 1);
- }
- }
- break;
- case RECT:
- g.setColor(color);
- if (isFill())
- {
- g.fillRect(x, y, width, height);
- }
- switch (shape)
- {
- case SHAPE_PLAIN:
- if (!isFill())
- g.drawRect(x, y, width, height);
- break;
- case SHAPE_UP:
- g.draw3DRect(x, y, width, height, true);
- break;
- case SHAPE_DOWN:
- g.draw3DRect(x, y, width, height, false);
- break;
- }
- break;
- case ROUND_RECT:
- if (isFill())
- {
- g.setColor(color);
- g.fillRoundRect(x, y, width, height, getParam(0), getParam(1));
- }
-
- if (is3D())
- {
- Rectangle rcCorner = new Rectangle(x, y + height - getParam(1), getParam(0), getParam(1));
- // bottom left corner
- int wid = rcCorner.width/2;
- int hgt = rcCorner.height/2;
-
- g.setColor(clrBottomRight);
- g.drawArc(rcCorner.x, rcCorner.y, rcCorner.width, rcCorner.height, 225, 45);
- g.setColor(clrTopLeft);
- g.drawArc(rcCorner.x, rcCorner.y, rcCorner.width, rcCorner.height, 180, 45);
-
- g.drawLine(x, bottom() - hgt, x, y + hgt); // left line
-
- g.drawArc(x, y, rcCorner.width, rcCorner.height, 90, 90); // top left corner
-
- // top right corner
- rcCorner.move(right() - rcCorner.width, y);
- g.drawLine(x + wid, y, right() - wid, y); // top line
- g.drawArc(rcCorner.x, rcCorner.y, rcCorner.width, rcCorner.height, 45, 45);
- g.setColor(clrBottomRight);
- g.drawArc(rcCorner.x, rcCorner.y, rcCorner.width, rcCorner.height, 0, 45);
-
- // bottom right corner
- g.drawLine(right(), y + hgt, right(), bottom() - hgt); // right line
- g.drawArc(right() - rcCorner.width, bottom() - rcCorner.height, rcCorner.width, rcCorner.height, 270, 90); // bottom right corner
-
- g.drawLine(right() - rcCorner.width/2, bottom(), x + rcCorner.width/2, bottom()); // bottom line
- }
- else
- {
- if (!isFill())
- {
- g.setColor(color);
- g.drawRoundRect(x, y, width, height, getParam(0), getParam(1));
- }
- }
- break;
- case POLYGON:
- if (isFill())
- {
- g.setColor(color);
- if (getPointsCount() > 2)
- g.fillPolygon(getPolygon());
- else
- g.drawPolygon(getPolygon());
- if (is3D())
- {
- int count = getParaCount() - 2;
- int i = 0;
- while (i < count)
- {
- g.setColor
- (
- (
- (getParam(i + 3) > getParam(i + 1)) ||
- ( (getParam(i + 3) == getParam(i + 1)) && (getParam(i + 2) < getParam(i)) )
- ) ? clrBottomRight : clrTopLeft
- );
- g.drawLine(getParam(i), getParam(i + 1), getParam(i + 2), getParam(i + 3));
- i += 2;
- }
- //close line
- g.setColor
- (
- (
- (getParam(1) > getParam(i + 1)) ||
- ( (getParam(1) == getParam(i + 1)) && (getParam(0) < getParam(i)) )
- ) ? clrBottomRight : clrTopLeft
- );
- g.drawLine(getParam(i), getParam(i + 1), getParam(0), getParam(1));
- }
- }
- else
- {
- if (is3D())
- {
- int count = getParaCount() - 2;
- int i = 0;
- while (i < count)
- {
- if (isYNearAngle(i))
- {
- g.setColor(clrTopLeft);
- g.drawLine(getParam(i) - 1, getParam(i + 1), getParam(i + 2) - 1, getParam(i + 3));
- g.setColor(clrBottomRight);
- g.drawLine(getParam(i) + 1, getParam(i + 1), getParam(i + 2) + 1, getParam(i + 3));
- }
- else
- {
- g.setColor(clrTopLeft);
- g.drawLine(getParam(i), getParam(i + 1) - 1, getParam(i + 2), getParam(i + 3) - 1);
- g.setColor(clrBottomRight);
- g.drawLine(getParam(i), getParam(i + 1) + 1, getParam(i + 2), getParam(i + 3) + 1);
- }
- i += 2;
- }
- }
- g.setColor(color);
- g.drawPolygon(getPolygon());
- }
- break;
- case OVAL:
- if (isFill())
- {
- g.setColor(color);
- g.fillOval(x, y, width, height);
- }
-
- if (is3D())
- {
- g.setColor(clrTopLeft);
- g.drawArc(x, y, width, height, 45, 180);
- g.setColor(clrBottomRight);
- g.drawArc(x, y, width, height, 225, 180);
- }
- else
- {
- if (!isFill())
- {
- g.setColor(color);
- g.drawOval(x, y, width, height);
- }
- }
- break;
- case PIE:
- if (isFill())
- {
- g.setColor(color);
- g.fillArc(x, y, width, height, getParam(0), getParam(1));
- }
- if (is3D())
- {
- int[] angleSep = { 45, 225, 405, 585, 765};
- int angleCur = getParam(0);
- int angleEnd = angleCur + getParam(1);
- int angleSize = 0;
- int i = (angleCur + 135) / 180;
-
- // start line
- g.setColor( isTopLeftAngle(angleCur + 90) ? clrBottomRight : clrTopLeft);
- drawAngleLine(g, getParam(0));
-
- while (angleCur < angleEnd)
- {
- angleSize = ((angleSep[i] >= angleEnd) ? angleEnd : angleSep[i]) - angleCur;
-
- g.setColor( isTopLeftAngle(angleCur) ? clrTopLeft : clrBottomRight);
- g.drawArc(x, y, width, height, angleCur, angleSize);
-
- angleCur = angleSep[i++];
- }
-
- // end line
- g.setColor( isTopLeftAngle(getParam(0) + getParam(1) + 90) ? clrTopLeft : clrBottomRight);
- drawAngleLine(g, getParam(0) + getParam(1));
-
- }
- else
- {
- if (!isFill())
- {
- g.setColor(color);
- g.drawArc(x, y, width, height, getParam(0), getParam(1));
- drawAngleLine(g, getParam(0));
- drawAngleLine(g, getParam(0) + getParam(1));
- }
- }
-
- break;
- case ARC:
- g.setColor(color);
- g.drawArc(x, y, width, height, getParam(0), getParam(1));
- if (is3D())
- {
- int[] angleSep = { 45, 225, 405, 585, 765};
- int angleCur = getParam(0);
- int angleEnd = angleCur + getParam(1);
- int angleSize = 0;
- int i = (angleCur + 135) / 180;
-
- while (angleCur < angleEnd)
- {
- angleSize = ((angleSep[i] >= angleEnd) ? angleEnd : angleSep[i]) - angleCur;
-
- if ((angleCur % 360) >= 45 && (angleCur % 360) < 225)
- {
- g.setColor(clrTopLeft);
- g.drawArc(x - 1, y - 1, width + 2, height + 2, angleCur, angleSize);
- g.setColor(clrBottomRight);
- g.drawArc(x + 1, y + 1, width - 2, height - 2, angleCur, angleSize);
- }
- else
- {
- g.setColor(clrBottomRight);
- g.drawArc(x - 1, y - 1, width + 2, height + 2, angleCur, angleSize);
- g.setColor(clrTopLeft);
- g.drawArc(x + 1, y + 1, width - 2, height - 2, angleCur, angleSize);
- }
-
- angleCur = angleSep[i++];
- }
- }
- break;
- case STRING:
- if (hasText())
- {
- g.setFont( new Font(fontName, fontStyle, getFontSize()) );
- if (is3D())
- {
- g.setColor(clrTopLeft);
- g.drawString(text, x - 1, y + getFontSize() - 1);
- g.setColor(clrBottomRight);
- g.drawString(text, x + 1, y + getFontSize() + 1);
- }
- g.setColor(color);
- g.drawString(text, x, y + getFontSize());
-
- }
- break;
- case IMAGE:
- if (objTank.image != null)
- g.drawImage(objTank.image, x, y, WorkPanel.me);
- break;
- case DRAW:
- if (objTank.obj != null)
- {
- Enumeration enum = objTank.obj.elements();
-
- while (enum.hasMoreElements())
- {
- Draw dd = (Draw) enum.nextElement();
- Draw clone = new Draw(dd);
- clone.multiply((double)width/objTank.dim.width, (double)height/objTank.dim.height);
- clone.transelate(x, y);
- clone.draw(g);
- }
- }
- break;
- }
-
- if (curMethod == ANCHOR && hasURL())
- { // draw urlName
- g.setFont( getURLFont() );
-
- int swidth = g.getFontMetrics().stringWidth(urlName);
- int sx = x + (width - swidth) / 2;
- int sy = y + (height - URL_FONT_HEIGHT)/2;
-
- g.setColor(Color.white);
- g.fillRect(sx, sy, swidth, URL_FONT_HEIGHT);
- g.setColor(Color.blue);
- g.drawString(urlName, sx, sy + URL_FONT_HEIGHT*3/4);
- }
- }
- }
-