home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 4.5 KB | 227 lines |
- import java.awt.*;
- /**
- * A class that help to resize the drawing
- *
- * @version 1.0 03/24/96
- * @author Kang, Dae Woong (namu@star.elim.net)
- */
-
- class Tracker
- {
- Draw draw;
- int px, py;
- int ptSelected;
-
- int w, h;
-
- final int size = 8;
-
- int stateSelect = 0;
- final int OFF = 0;
- final int MOVE = 1;
- final int RESIZE = 2;
- final int POINT = 3;
- final int SELECT = 4;
-
- int stateHeight = 0;
- final int TOP = 1;
- final int BOTTOM = 2;
-
- int stateWidth = 0;
- final int LEFT = 1;
- final int RIGHT = 2;
-
- Rectangle rcOut = new Rectangle();
-
- boolean isInTracker(int x, int y)
- {
- if (stateSelect == OFF)
- {
- stateSelect = OFF;
- return false;
- }
- else
- {
- if (getClipRect().inside(x, y) || draw.method != Draw.IMAGE)
- {
- int count = draw.getPointsCount();
- for (int i = 0; i < count; i++)
- {
- if(getPointRect(draw.getPoint(i)).inside(x, y))
- {
- stateSelect = POINT;
- ptSelected = i;
- px = x;
- py = y;
- return true;
- }
- }
- }
-
- if (draw.inside(x, y))
- {
- stateSelect = MOVE;
- px = x;
- py = y;
- return true;
- }
- else if (!draw.isPointsOnlyType())
- {
- for (int xx = 0; xx < 3; xx++)
- {
- for (int yy = 0; yy < 3; yy++)
- {
- if(getSmallRect(xx, yy).inside(x, y))
- {
- stateWidth = xx;
- stateHeight = yy;
- stateSelect = RESIZE;
- px = x;
- py = y;
- return true;
- }
- }
- }
- }
- }
- stateSelect = OFF;
- return false;
- }
-
- public boolean drag(int x, int y)
- {
- w = x - px;
- h = y - py;
-
- px = x;
- py = y;
-
- switch (stateSelect)
- {
- case MOVE:
- return true;
- case RESIZE:
- int xx = 0, yy = 0;
-
- switch (stateHeight)
- {
- case TOP: yy = h; h = -h; break;
- case BOTTOM: break;
- default: h = 0;
- }
- if (draw.height + h <= 0)
- {
- h = 0; yy = 0;
- }
-
- switch (stateWidth)
- {
- case LEFT: xx = w; w = -w; break;
- case RIGHT: break;
- default: w = 0;
- }
- if (draw.width + w <= 0)
- {
- w = 0; xx = 0;
- }
-
- draw.reshape(draw.x + xx, draw.y + yy, draw.width + w, draw.height + h);
- return true;
- case POINT:
- Point pt = draw.getPoint(ptSelected);
-
- draw.setPoint(ptSelected, pt.x + w, pt.y + h);
- if (draw.isPointsOnlyType())
- {
- draw.setRect(draw.getPolygon().getBoundingBox());
-
- if (draw.method == Draw.LINE)
- draw.setLineAngle();
- if (draw.isEmpty())
- draw.grow((draw.width == 0) ? 2 : 0, (draw.height == 0) ? 2 : 0);
- }
- px = pt.x + w;
- py = pt.y + h;
-
- return true;
- }
- return false;
- }
-
- public Rectangle getSmallRect(int xx, int yy)
- {
- int x, y;
- if (xx == 0 && yy == 0) //center rect
- return new Rectangle(-1, -1, 0, 0);
-
- switch (xx)
- {
- case LEFT: x = draw.x - 1 - size; break;
- case RIGHT: x = draw.x + draw.width + 1; break;
- default: x = draw.x + (draw.width - size)/2; break; // center
- }
-
- switch (yy)
- {
- case TOP: y = draw.y - 1 - size; break;
- case BOTTOM: y = draw.y + draw.height + 1; break;
- default: y = draw.y + (draw.height - size)/2; break; // center
- }
-
- return new Rectangle(x, y, size, size);
- }
-
- public Rectangle getPointRect(Point pt)
- {
- return new Rectangle(pt.x - size/2, pt.y - size/2, size, size);
- }
-
- public Rectangle getClipRect()
- {
- return (stateSelect == OFF) ? new Rectangle(-1, -1, -1, -1)
- : new Rectangle(draw.x - size - 1, draw.y - size - 1, draw.width + 2 + size*2, draw.height + 2 + size*2);
- }
-
- public void draw(Graphics g)
- {
- if (stateSelect != OFF)
- {
- g.setColor(Color.white);
- g.setXORMode(Color.black);
- rcOut.reshape(draw.x - 1, draw.y - 1, draw.width + 2, draw.height + 2);
- g.drawRect(rcOut.x, rcOut.y, rcOut.width, rcOut.height);
- if (!draw.isPointsOnlyType() && draw.method != Draw.IMAGE)
- {
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- Rectangle rc = getSmallRect(x, y);
- g.fillRect(rc.x, rc.y, rc.width, rc.height);
- }
- }
- }
-
- g.setXORMode(Color.red);
- int count = draw.getPointsCount();
- for (int i = 0; i < count; i++)
- {
- Rectangle rc = getPointRect(draw.getPoint(i));
- g.fillRect(rc.x, rc.y, rc.width, rc.height);
- }
- g.setPaintMode();
- }
- }
-
-
- public void setDraw(Draw draw, int x, int y)
- {
- this.draw = draw;
- stateSelect = MOVE;
- stateWidth = 0;
- stateHeight = 0;
- px = x;
- py = y;
- }
- }
-