?? figure.java
字號:
package drawfigure;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author unascribed * @version 1.0 */import java.awt.*;import java.awt.geom.*;public abstract class Figure { private Point2D startPoint = null; private Point2D endPoint = null; private boolean isFirstDraw = true; //是否第一次畫 private boolean isSelected = false; private boolean isMoving = false; private boolean isResizing = false; private boolean isDrawing = false; /** * 固定點,resize時使用 */ private int activePosi; public Point2D getStartPoint() { return startPoint; } public void setStartPoint(Point2D p) { //anchor=p; startPoint = p; } public Point2D getEndPoint() { return endPoint; } public void setEndPoint(Point2D p) { endPoint = p; } public boolean isFirstDraw() { return isFirstDraw; } public void setFirstDraw(boolean b) { isFirstDraw = b; } public boolean isSelected() { return isSelected; } public void selected() { isSelected = true; } public void unSelected() { isSelected = false; } public boolean isMoving() { return isMoving; } public void setMoving(boolean b) { isMoving = b; } public boolean isResizing() { return isResizing; } /** * @param b */ public void setResizing(boolean b) { isResizing = b; } public void setResizing(boolean b, int posi) { setResizing(b); setActivePosition(posi); } public boolean isDrawing() { return isDrawing; } public void setDrawing(boolean b) { isDrawing = b; } public int getActivePosition() { return activePosi; } public void setActivePosition(int posi) { activePosi = posi; } public void setStartEndPoint(Point2D start, Point2D end) { this.setStartPoint(start); this.setEndPoint(end); } public Figure() { }; public Figure(Point2D p) { this.setEndPoint((Point2D) p.clone()); this.setStartPoint((Point2D) p.clone()); } //抽象方法 /** * 判斷點p是否在本圖形的區域內 */ public abstract boolean pointInside(Point2D p); /** * 返回當前鼠標位置所在的控制塊, * 如果不在任何控制塊內,返回null */ public abstract boolean pointInControlRect(Point2D p); public abstract ControlRect getControlRect(Point2D p); /** * 同pointInResizeControl(Point2D p); * @param p * @return */ /** * 根據點坐標在哪個選擇塊中,得到改變大小的光標類型 */ public Cursor getCursor(Point2D p) { ControlRect ctrl = this.getControlRect(p); if (ctrl != null) { return ctrl.getCursor(); } return new Cursor(Cursor.DEFAULT_CURSOR); } /** * 畫圖 * @param g */ public abstract void draw(Graphics g); /** * 返回邊界 * @return */ public Rectangle2D getBounds() { double x, y, width, height; x = Math.min(getStartPoint().getX(), getEndPoint().getX()); y = Math.min(getStartPoint().getY(), getEndPoint().getY()); width = Math.abs(getStartPoint().getX() - getEndPoint().getX()); height = Math.abs(getStartPoint().getY() - getEndPoint().getY()); return new Rectangle2D.Double(x, y, width, height); } /** * 移動,使用之前,需要先g.setXORMode(Color); * 似乎有點不妥!!=== */ public void move(Graphics g, double offsetX, double offsetY) { if (isMoving()) { draw(g); } setStartEndPoint( new Point2D.Double( getStartPoint().getX() + offsetX, getStartPoint().getY() + offsetY), new Point2D.Double( getEndPoint().getX() + offsetX, getEndPoint().getY() + offsetY)); draw(g); setMoving(true); } /** * 移動,使用之前,需要先g.setXORMode(Color); * 似乎有點不妥!!=== * 不同的圖形,實現不同,放到子類實現 */ public void resize(Graphics g, double offsetX, double offsetY) { if (isResizing()) { draw(g); } Rectangle2D bound = getBounds(); switch (getActivePosition()) { case ControlRect.LEFT_TOP_POSITION : //左上 setStartPoint( new Point2D.Double( bound.getX() + offsetX, bound.getY() + offsetY)); setEndPoint( new Point2D.Double( bound.getX() + bound.getWidth(), bound.getY() + bound.getHeight())); break; case ControlRect.TOP_POSITION : //上 setStartPoint( new Point2D.Double( bound.getX(), bound.getY() + bound.getHeight())); setEndPoint( new Point2D.Double( bound.getX() + bound.getWidth(), bound.getY() + offsetY)); break; case ControlRect.RIGHT_TOP_POSITION : //右上 setStartPoint( new Point2D.Double( bound.getX(), bound.getY() + bound.getHeight())); setEndPoint( new Point2D.Double( bound.getX() + bound.getWidth() + offsetX, bound.getY() + offsetY)); break; case ControlRect.RIGHT_POSITION : //右 setStartPoint(new Point2D.Double(bound.getX(), bound.getY())); setEndPoint( new Point2D.Double( bound.getX() + bound.getWidth() + offsetX, bound.getY() + bound.getHeight())); break; case ControlRect.RIGHT_BOTTOM_POSITION : //右下角 setStartPoint(new Point2D.Double(bound.getX(), bound.getY())); setEndPoint( new Point2D.Double( bound.getX() + bound.getWidth() + offsetX, bound.getY() + bound.getHeight() + offsetY)); break; case ControlRect.BOTTOM_POSITION : //下 setStartPoint(new Point2D.Double(bound.getX(), bound.getY())); setEndPoint( new Point2D.Double( bound.getX() + bound.getWidth(), bound.getY() + bound.getHeight() + offsetY)); break; case ControlRect.LEFT_BOTTOM_POSITION : //左下 setStartPoint( new Point2D.Double( bound.getX() + offsetX, bound.getY() + bound.getHeight() + offsetY)); setEndPoint( new Point2D.Double( bound.getX() + bound.getWidth(), bound.getY() + bound.getHeight())); break; case ControlRect.LEFT_POSITION : //左 setStartPoint( new Point2D.Double(bound.getX() + offsetX, bound.getY())); setEndPoint( new Point2D.Double( bound.getX() + bound.getWidth(), bound.getY() + bound.getHeight())); break; } draw(g); setResizing(true, getActivePosition()); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -