📄 rect.java
字号:
import java.awt.*;public class Rect implements ShapeInterface {int x,y,w,h,origX,origY,origW,origH;int x1,x2,y1,y2;private int index,nodeResize;private boolean selected,highlighted;public String url="",name;public Rect(int x, int y) { x1 = x2 = x; y1 = y2 = y;}public void draw(Graphics s) { int i; s.setColor(Color.blue); if (isHighlighted() || isSelected()) { s.setColor(Color.red); // draw highlight lines if (w>h) { // draw lines for wide box for (i=0;i<h;i+=5) { s.drawLine(x,y+i,x+i,y); } for (i=i;i<w;i+=5) { s.drawLine(x+i,y,x+i-h,y+h); } for (i=i-h;i<w;i+=5) { s.drawLine(x+i,y+h,x+w,y+h-w+i); } } else { // draw lines for tall box for (i=0;i<w;i+=5) { s.drawLine(x+i,y,x,y+i); } for (i=i;i<h;i+=5) { s.drawLine(x,y+i,x+w,y+i-w); } for (i=i-w;i<h;i+=5) { s.drawLine(x+w,y+i,x+w-h+i,y+h); } } } if (isSelected()) { // draw resize blocks s.fillRect(x,y,7,7); // nw s.fillRect(x+w-6,y,7,7); // ne s.fillRect(x,y+h-6,7,7); // sw s.fillRect(x+w-6,y+h-6,7,7); // se s.fillRect(x+w/2-3,y,7,7); // n s.fillRect(x+w/2-3,y+h-6,7,7); // s s.fillRect(x,y+h/2-3,7,7); // w s.fillRect(x+w-6,y+h/2-3,7,7); // e } s.drawRect(x,y,w,h);}public void newPoint(int x, int y) { x2 = x; y2 = y; this.x = Math.min(x1,x2); this.y = Math.min(y1,y2); this.w = Math.abs(x1-x2); this.h = Math.abs(y1-y2);}public void moveTo(int x, int y) { this.x = x; this.y = y;}public void storeDimensions() { origX = x; origY = y; origW = w; origH = h;}public int getResize(int x, int y) { if (checkWithin(x,y, this.x, this.y, this.x+7, this.y+7)) nodeResize = Frame.NW_RESIZE_CURSOR; //nw else if (checkWithin(x,y, this.x+w-7, this.y, this.x+w, this.y+7)) nodeResize = Frame.NE_RESIZE_CURSOR; //ne else if (checkWithin(x,y, this.x, this.y+h-7, this.x+7, this.y+h)) nodeResize = Frame.SW_RESIZE_CURSOR; //sw else if (checkWithin(x,y, this.x+w-7, this.y+h-7, this.x+w, this.y+h)) nodeResize = Frame.SE_RESIZE_CURSOR; //se else if (checkWithin(x,y, this.x+w/2-3, this.y, this.x+w/2+3, this.y+7)) nodeResize = Frame.N_RESIZE_CURSOR; //n else if (checkWithin(x,y, this.x+w/2-3, this.y+h-7, this.x+w/2+3, this.y+h)) nodeResize = Frame.S_RESIZE_CURSOR; //s else if (checkWithin(x,y, this.x, this.y+h/2-3, this.x+7, this.y+h/2+3)) nodeResize = Frame.W_RESIZE_CURSOR; //w else if (checkWithin(x,y, this.x+w-7, this.y+h/2-3, this.x+w, this.y+h/2+3)) nodeResize = Frame.E_RESIZE_CURSOR; //e else nodeResize = Frame.DEFAULT_CURSOR; storeDimensions(); return nodeResize;}public boolean resize (int x, int y) { boolean r=true; if (nodeResize == Frame.SE_RESIZE_CURSOR) {resizeSE(x,y);} else if (nodeResize == Frame.SW_RESIZE_CURSOR) {resizeSW(x,y);} else if (nodeResize == Frame.NW_RESIZE_CURSOR) {resizeNW(x,y);} else if (nodeResize == Frame.NE_RESIZE_CURSOR) {resizeNE(x,y);} else if (nodeResize == Frame.N_RESIZE_CURSOR) resizeN(y); else if (nodeResize == Frame.S_RESIZE_CURSOR) resizeS(y); else if (nodeResize == Frame.W_RESIZE_CURSOR) resizeW(x); else if (nodeResize == Frame.E_RESIZE_CURSOR) resizeE(x); else r=false; return r;}public void resizeSE(int x, int y) { resizeS(y); resizeE(x);}public void resizeSW(int x, int y) { resizeS(y); resizeW(x);}public void resizeNW(int x, int y) { resizeN(y); resizeW(x);}public void resizeNE(int x, int y) { resizeN(y); resizeE(x);}public void resizeN(int y) { this.y = Math.min(origY+origH,y); h = Math.max(origY+origH-y,2);}public void resizeS(int y) { h = Math.max(y-origY,2);}public void resizeW(int x) { this.x = Math.min(origX+origW,x); w = Math.max(origX+origW-x,2);}public void resizeE(int x) { this.w = Math.max(x-origX,2);}public void setSelected(boolean b) { setHighlighted(false); selected = b;}public boolean isSelected() { return selected;}public void setHighlighted(boolean b) { this.highlighted = b;}public boolean isHighlighted() { return highlighted;}public boolean contains(int x, int y) { if (x>=this.x && x<=this.x+this.w+1 && y>=this.y && y<=this.y+this.h+1) return true; else return false;}public boolean checkWithin(int x, int y, int x1, int y1, int x2, int y2) { if (x>=x1 && x<=x2+1 && y>=y1 && y<=y2+1) return true; else return false;}public int getX() {return x;}public int getY() {return y;}public int getW() {return w;}public int getH() {return h;}public String getType() {return "rect";}public void setIndex(int i) { index = i;}public int getIndex() { return index;}public void setURL(String url) { this.url = url;}public String getURL() { return url;}public void setName(String name) { if (name != getType()+String.valueOf(index)) this.name = name;}public String getName() { if (name!=null) return name; else return getType()+String.valueOf(index);}public String getCoords(String type) { if (type=="client") return String.valueOf(x)+","+String.valueOf(y)+","+String.valueOf(x+w)+","+String.valueOf(y+h); else if (type=="server") return String.valueOf(x)+","+String.valueOf(y)+" "+String.valueOf(x+w)+","+String.valueOf(y+h); else return null;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -