📄 dragdrop.new
字号:
drawEdge(canvas_g,last_v, n); } // erase vertex last_v.erase(canvas_g, offset_x, offset_y); // delete vertex from graph graph.deleteVertex(last_v); } }// else if (e.key == 63) // `?' pressed// System.out.println("DragDrop: graph "+graph); canvas_g.dispose(); return true; case Event.MOUSE_ENTER: // update status line DragDropObj ddotmp = getSelected(); if (ddotmp != null) status.setText(ddotmp.getObjectID()); return true; } } canvas_g.dispose(); return super.handleEvent(e); } /** * Called when panel size changes (e.g. on startup) */ public synchronized void setBounds(int x, int y, int width, int height) { // call setBounds() of superclass super.setBounds(x, y, width, height); // calculate virtual canvas size int hbar_size = hbar.getValue(); int vbar_size = vbar.getValue(); canvas_width = width - vbar_size; canvas_height = height - hbar_size; max_x = canvas_width; max_y = canvas_height; if (offset_x > max_x - canvas_width) offset_x = max_x - canvas_width; if (offset_y > max_y - canvas_height) offset_y = max_y - canvas_height; Enumeration vertices = graph.getAllVertices(); DragDropObj v; while (vertices.hasMoreElements()) { v = (DragDropObj)vertices.nextElement(); v.initImage(canvas, mt); int offset; int vx = v.getX()+10; if (vx > max_x) { offset = vx - max_x; if (canvas_width >= 2) { offset = (int)Math.ceil((double)offset / (double)(canvas_width / 2)); max_x += offset * (canvas_width / 2); } else max_x = vx; } int vy = v.getY()+10; if (vy > max_y) { offset = vy - max_y; if (canvas_height >= 2) { offset = (int)Math.ceil((double)offset / (double)(canvas_height / 2)); max_y += offset * (canvas_height / 2); } else max_y = vy; } } boolean interrupted = true; while (interrupted) { try { mt.waitForAll(); interrupted = false; } catch (Exception ex) { System.out.println("DragDrop.reshape: MediaTracker.waitForAll Exception "+ex); } } // initialize scrollbars updateScrollbars(width, height); } /** * Enable editing functions */ public void enableEdit() { if ((last_v != null) && selected) { Graphics canvas_g = canvas.getGraphics(); canvas_g.setXORMode(canvas.getBackground()); last_v.highlight(canvas_g, offset_x, offset_y, false); canvas_g.dispose(); selected = false; status.setText(""); } canvas.repaint(); editEnabled = true; } /** * Diable editing functions * @return true if successful, false if state != STATE_NONE */ public boolean disableEdit() { if (state != STATE_NONE) return false; if ((last_v != null) && selected) { Graphics canvas_g = canvas.getGraphics(); canvas_g.setXORMode(canvas.getBackground()); last_v.highlight(canvas_g, offset_x, offset_y, false); canvas_g.dispose(); selected = false; status.setText(""); } editEnabled = false; orig_v = null; return true; } /** * Get highlighted object * @return selected object or null if no object has been selected */ public DragDropObj getSelected() { if ((state == STATE_NONE) && (last_v != null) && selected) return last_v; else return null; } /** * Set the class for creation of new objects * @param newObjectClass class for object creation */ public void setClass(Class newObjectClass) { createClass = newObjectClass; } /** * Get graph object; note that you have to call repaint() * after making changes in the graph topology manually * @return graph object * @see Graph */ public Graph getGraph() { return graph; } // private methods private void updateScrollbars(int width, int height) { if (canvas_width >= 1 && canvas_height >= 1) { //hbar.setValues(offset_x, canvas_width, 0, max_x - canvas_width); //vbar.setValues(offset_y, canvas_height, 0, max_y - canvas_height); //hbar.setBlockIncrement(canvas_width / 2); //vbar.setBlockIncrement(canvas_height / 2); //hbar.setUnitIncrement(canvas_width / 10); //vbar.setUnitIncrement(canvas_height / 10); } } /** * Called to update the window when dragging objects or drawing lines * @param x current horizontal position * @param y current vertical position * @extend if true, the virtual size will be extended when moving out * @scroll if true, the panel scrolls so that the current position is visible */ private void updateVirtual(int x, int y, boolean extend, boolean scroll) { int offset; // extend virtual size if necessary if (extend && ((x + offset_x) > max_x || (y + offset_y) > max_y)) { if ((x+offset_x) > max_x) { offset = x + offset_x - max_x; if (canvas_width >= 2) { offset = (int)Math.ceil((double)offset / (double)(canvas_width / 2)); max_x += offset * (canvas_width / 2); } else max_x = x+offset_x+10; } if ((y+offset_y) > max_y) { offset = y + offset_y - max_y; if (canvas_height >= 2) { offset = (int)Math.ceil((double)offset / (double)(canvas_height / 2)); max_y += offset * (canvas_height / 2); } else max_y = y+offset_y+10; } updateScrollbars(canvas_width, canvas_height); } // scroll to current position if necessary if (scroll) { int oldx = offset_x; int oldy = offset_y; if (x < 0) { offset = ((-x) / (canvas_width / 10)) + 1; offset_x -= offset * (canvas_width / 10); if (offset_x < 0) offset_x = 0; } if (y < 0) { offset = ((-y) / (canvas_height / 10)) + 1; offset_y -= offset * (canvas_height / 10); if (offset_y < 0) offset_y = 0; } if (x >= canvas_width) { offset = ((x - canvas_width) / (canvas_width / 10)) + 1; offset_x += offset * (canvas_width / 10); if (offset_x > max_x - canvas_width) offset_x = max_x - canvas_width; } if (y >= canvas_height) { offset = ((y - canvas_height) / (canvas_height / 10)) + 1; offset_y += offset * (canvas_height / 10); if (offset_y > max_y - canvas_height) offset_y = max_y - canvas_height; } if (oldx != offset_x || oldy != offset_y) { hbar.setValue(offset_x); vbar.setValue(offset_y); Graphics canvas_g = canvas.getGraphics(); canvas.update(canvas_g); canvas_g.dispose(); } } } /** * Get object at the specified position. * @param x Current horizontal position * @param y Current vertical position * @param except Object to ignore */ private DragDropObj find(int x, int y, DragDropObj except) { Enumeration vertices = graph.getAllVertices(); DragDropObj v; while (vertices.hasMoreElements()) { v = (DragDropObj)vertices.nextElement(); if ((v != except) && (v.hasFocus(x,y))) return v; } return null; } /** * Move an object to a different position. * @param canvas_g Graphics context of canvas * @param obj Object to move * @param x New horizontal position * @param y New vertical position */ private void moveto(Graphics canvas_g, DragDropObj obj, int x, int y) { Enumeration neighbors = graph.findNeighbors(obj); DragDropObj n; while (neighbors.hasMoreElements()) { n = (DragDropObj)neighbors.nextElement(); drawEdge(canvas_g, obj, n); } obj.erase(canvas_g, offset_x, offset_y); obj.setCoordinates(x, y); obj.draw(canvas_g, offset_x, offset_y); neighbors = graph.findNeighbors(obj); while (neighbors.hasMoreElements()) { n = (DragDropObj)neighbors.nextElement(); drawEdge(canvas_g, obj, n); } } /** * Draw edge between two objects. * @param g Graphics context * @param V1 First object * @param V1 Second object */ protected void drawEdge(Graphics g, DragDropObj V1, DragDropObj V2) { Point v1_center = new Point(V1.getX(), V1.getY()); Point v2_center = new Point(V2.getX(), V2.getY()); Point v1_conn = V1.connectionCoord(v2_center); Point v2_conn = V2.connectionCoord(v1_center); g.drawLine(v1_conn.x-offset_x, v1_conn.y-offset_y, v2_conn.x-offset_x, v2_conn.y-offset_y); } // constants private static final int STATE_NONE = 0; private static final int STATE_DRAW = 1; private static final int STATE_DRAG = 2; // data protected Graph graph; // graph to store vertices and edges protected int offset_x = 0; // current scroll position (x) protected int offset_y = 0; // current scroll position (y) private MediaTracker mt; // MediaTracker private Class createClass; // used to create new objects private DragDropCanvas canvas; // drag and drop area //private Scrollbar hbar, vbar; // scrollbars private Adjustable hbar; private Adjustable vbar; private ScrollPane sp; // South Panel (Scrollbar + Status) private Label status; // Status line private int last_x, last_y; // last mouse pointer position private DragDropObj last_v; // last selected object private int orig_x, orig_y; // origin position for line drawing private DragDropObj orig_v; // origin object for drawing and dragging private int max_x; // x size of drag and drop area private int max_y; // y size of drag and drop area private int canvas_width; // width of currently visible area private int canvas_height; // height of currently visible area private int state = STATE_NONE; // state (see constants above) private boolean selected = false; // is there any selected object? private boolean dragged = false; // did user move the mouse since mouse-down? private boolean editEnabled = false; // enable edit functions} // DragDrop// The class DragDropCanvas implements the drawing area of the panel.class DragDropCanvas extends Canvas { public synchronized void paint(Graphics g) { if (g==null) return; DragDrop dd = (DragDrop)getParent().getParent(); // erase screen Rectangle r = getBounds(); g.clearRect(r.x, r.y, r.width, r.height); g.setXORMode(getBackground()); // draw vertices Enumeration vertices = dd.graph.getAllVertices(); DragDropObj v; while (vertices.hasMoreElements()) { v = (DragDropObj)vertices.nextElement(); v.draw(g, dd.offset_x, dd.offset_y); } // draw edges Enumeration edges = dd.graph.getAllEdges(); Edge e; while (edges.hasMoreElements()) { e = (Edge)edges.nextElement(); dd.drawEdge(g, (DragDropObj)e.vertex1, (DragDropObj)e.vertex2); } // request input focus requestFocus(); } public void update(Graphics g) { paint(g); }} // DragDropCanvas
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -