📄 drawingpanel.java
字号:
* going to be drawn. * @return Inset of the drawing panel, * i.e. the edge pads on the four sides of * of the panel that are not to be drawn. */ public int getOffset() { return offset; } /** * Set if the animation is in the 'SKIP' . . 'UNTIL' mode. * @param skip TRUE if the algorithm animation is to be fast forwarded * to a specific point of execution. FALSE otherwise. */ public void setSkip(boolean skip) { this.skip = skip; } /** * Set if the animation is disable. * @param noAnim TRUE is animation is disabled; FALSE otherwise. */ public void setNoAnim(boolean noAnim) { this.noAnim = noAnim; } /** * Check if the noAnim flag is set. * @return If the noAnim flag is set. */ public boolean getNoAnim() { return noAnim; } /** * Check if the animationis in the skip mode. * @return Returns if the animation is in the 'SKIP' . . 'UNTIL' mode. */ public boolean getSkip() { return skip; } /** * Adds a drawing object to the canvas. * @param obj The object to be added. */ public void addDrawingObj(DrawingObj obj) { if (isObjExist(obj)) removeObj(obj); drawingObj.addElement(obj); } /** * Checks if a certain object has already been added to the drawing * panel. * @return Returns TRUE if the drawing object has been added; * FALSE otherwise. * @param obj The drawing object to be checked. */ public boolean isObjExist(DrawingObj obj) { boolean res = false; for (int i = 0; i < drawingObj.size(); i++) { DrawingObj dobj = (DrawingObj)drawingObj.elementAt(i); if (dobj.equals(obj)) { res = true; break; } } return res; } /** * Remove a drawing object which matches the parameter. * @param obj The drawing object to be removed. * @return TRUE if the object has been successfully deleted; FALSE * otherwise. */ public boolean removeObj(DrawingObj obj) { if (obj == null) return false; for (int i = 0; i < drawingObj.size(); i++) { DrawingObj ob = (DrawingObj)drawingObj.elementAt(i); if (ob.equals(obj)) { drawingObj.removeElementAt(i); return true; } } return false; } /** * Adds a commentary box to the drawing panel. * @param com The commentary box to be added. */ public void addCom(ComBox com) { if (isComExist(com)) removeCom(com); comment.addElement(com); } /** * Check if a commentary box has already been added to the drawing panel. * @return Returns TRUE if the commentary box has been added; * FALSE otherwise. * @param com The commentary box to be checked. */ public boolean isComExist(ComBox com) { boolean res = false; for (int i = 0; i < comment.size(); i++) { ComBox com2 = (ComBox)comment.elementAt(i); if (com2.equals(com)) { res = true; break; } } return res; } /** * Remove the commentary box. * @param com The combox to be removed. */ public void removeCom(ComBox com) { if (com == null) return; for (int i = 0; i < comment.size(); i++) { ComBox com2 = (ComBox)comment.elementAt(i); if (com2.equals(com)) { comment.removeElementAt(i); break; } } }/* ----------------------- Animation methods ----------------------- */ int animStep = 5; /** * Set the animation step, i.e. the number of step for an object * to move from one point to another. * @param step The number of animation step. */ public void setAnimStep(int step) { this.animStep = step; } /** * Animate a single drawing object through a sequence of points * specified by the <code>Vector</code> construction. * Each element of the vector is of type Point, where the first * is the source and the last is the destination of the trajectory * to be animated on the object. * @param obj The drawing object to be move. * @param pts The trajectory the drawing object is to be moved. */ public void animate(DrawingObj obj, Vector pts) { if (skip) return; if (pts.size() > 1) { Point src = (Point)pts.firstElement(); for (int i = 1; i < pts.size(); i++) { if (noAnim || skip) { Point finalPt = (Point)pts.lastElement(); obj.move(finalPt.x, finalPt.y); break; } Point dest = (Point)pts.elementAt(i); for (int j = 0; j < animStep; j++) { int x = src.x + (dest.x - src.x)*(j+1) / animStep; int y = src.y + (dest.y - src.y)*(j+1) / animStep; obj.move(x, y); repaint(); delay(); } obj.move(dest.x, dest.y); repaint(); delay(); src = new Point(dest.x, dest.y); } } else if (pts.size() > 0) { Point pt = (Point)pts.firstElement(); obj.move(pt.x, pt.y); repaint(); } } /** * Animate a list of drawing objects store in an array. * The trajectories of these objects are passed in through the * second parameter. The size of the arrays passed in through * both parameters should be equal. Otherwise, nothing will be performed. * The number of points in all the trajectories must be equal too. * @param objs Array holding the drawing objects to be animated. * @param pts Array holding the trajectories of the drawing objects. */ public void animate(DrawingObj[] objs, Vector[] pts) { if (skip) return; if (objs.length != pts.length) return; else if (objs.length < 1) return; // check if the number of points for all trajectories are equal for (int i = 1; i < objs.length; i++) { if (pts[i].size() != pts[i-1].size()) return; } if (pts[0].size() > 1) { if (noAnim || skip) { for (int i = 0; i < objs.length; i++) { Point finalPt = (Point)pts[i].lastElement(); objs[i].move(finalPt.x, finalPt.y); } return; } Point[] src = new Point[objs.length]; for (int i = 0; i < objs.length; i++) { if (pts[i].size() > 1) { src[i] = (Point)pts[i].firstElement(); } else src[i] = new Point(-1, -1); } for (int i = 1; i < pts[0].size(); i++) { Point[] dest = new Point[objs.length]; for (int j = 0; j < objs.length; j++) { dest[j] = (Point)pts[j].elementAt(i); } for (int j = 0; j < animStep; j++) { for (int k = 0; k < objs.length; k++) { int x = src[k].x + (dest[k].x - src[k].x)*(j+1) / animStep; int y = src[k].y + (dest[k].y - src[k].y)*(j+1) / animStep; objs[k].move(x, y); } repaint(); delay(); } for (int k = 0; k < objs.length; k++) { objs[k].move(dest[k].x, dest[k].y); src[k] = new Point(dest[k].x, dest[k].y); } repaint(); delay(); } for (int i = 0; i < objs.length; i++) { if (pts[i].size() > 0) { Point pt = (Point)pts[i].lastElement(); objs[i].move(pt.x, pt.y); } } repaint(); } else if (pts[0].size() > 0) { for (int i = 0; i < objs.length; i++) { if (pts[i].size() > 0) { Point pt = (Point)pts[i].lastElement(); objs[i].move(pt.x, pt.y); } } repaint(); } }} // class DrawingPanel
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -