⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vertexview.java

📁 用JGraph编的软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		// Double Buffers by David Larsson		protected void initOffscreen() {			try {				Rectangle rect = graph.getBounds();				//RepaintManager repMan = RepaintManager.currentManager(graph);				//offscreen = repMan.getVolatileOffscreenBuffer(getGraph(), (int) rect.getWidth(), (int) rect.getHeight());				offscreen =					new BufferedImage(						rect.width,						rect.height,						BufferedImage.TYPE_INT_RGB);				offgraphics = offscreen.getGraphics();				offgraphics.setClip(					0,					0,					(int) rect.getWidth(),					(int) rect.getHeight());				offgraphics.setColor(graph.getBackground());				offgraphics.fillRect(					0,					0,					(int) rect.getWidth(),					(int) rect.getHeight());				graph.getUI().paint(offgraphics, graph);			} catch (Error e) {				offscreen = null;				offgraphics = null;			}		}		public void overlay(Graphics g) {			if (!firstOverlayInvocation) {				if (cachedBounds != null) {					g.setColor(Color.black);					Rectangle tmp = graph.toScreen(new Rectangle(cachedBounds));					g.drawRect(tmp.x, tmp.y, tmp.width - 2, tmp.height - 2);				} else if (!initialBounds.equals(vertex.getBounds())) {					Graphics2D g2 = (Graphics2D) g;					AffineTransform oldTransform = g2.getTransform();					g2.scale(graph.getScale(), graph.getScale());					graph.getUI().paintCell(						g,						vertex,						vertex.getBounds(),						true);					if (contextViews != null)						for (int i = 0; i < contextViews.length; i++) {							graph.getUI().paintCell(								g,								contextViews[i],								contextViews[i].getBounds(),								true);						}					g2.setTransform(oldTransform);					if (portViews != null && graph.isPortsVisible())						graph.getUI().paintPorts(g, portViews);				}			}			firstOverlayInvocation = false;		}		/**		 * Invoked when the mouse pointer has been moved on a component		 * (with no buttons down).		 */		public void mouseMoved(MouseEvent event) {			if (vertex != null) {				for (int i = 0; i < r.length; i++) {					if (r[i].contains(event.getPoint())) {						graph.setCursor(new Cursor(cursors[i]));						event.consume();						return;					}				}			}		}		/** Process mouse pressed event. */		public void mousePressed(MouseEvent event) {			if (!graph.isSizeable())				return;			for (int i = 0; i < r.length; i++) {				if (r[i].contains(event.getPoint())) {					Set set = new HashSet();					set.add(vertex.getCell());					contextViews = context.createTemporaryContextViews(set);					Object[] all =						AbstractCellView.getDescendantViews(							new CellView[] { vertex });					if (all.length						>= org.jgraph.plaf.basic.BasicGraphUI.MAXHANDLES)						cachedBounds = new Rectangle(initialBounds);					event.consume();					index = i;					return;				}			}		}		/** Process mouse dragged event. */		public void mouseDragged(MouseEvent event) {			if (firstDrag				&& graph.isDoubleBuffered()				&& cachedBounds == null) {				initOffscreen();				firstDrag = false;			}			Rectangle dirty = null;			Graphics g =				(offgraphics != null) ? offgraphics : graph.getGraphics();			if (index == -1)				return;			Rectangle newBounds = computeBounds(event);			g.setColor(graph.getForeground());			g.setXORMode(graph.getBackground().darker());			overlay(g);			if (offgraphics != null) {				dirty = graph.toScreen(new Rectangle(vertex.getBounds()));				Rectangle t =					graph.toScreen(AbstractCellView.getBounds(contextViews));				if (t != null)					dirty.add(t);			}			if (cachedBounds != null)				cachedBounds = newBounds;			else {				// Reset old Bounds				CellView[] all =					AbstractCellView.getDescendantViews(						new CellView[] { vertex });				for (int i = 0; i < all.length; i++) {					CellView orig =						graph.getGraphLayoutCache().getMapping(							all[i].getCell(),							false);					Map origAttr =						GraphConstants.cloneMap(orig.getAllAttributes());					all[i].setAttributes(origAttr);					all[i].refresh(false);				}				vertex.setBounds(newBounds);				if (vertex != null)					graph.getGraphLayoutCache().update(vertex);				if (contextViews != null)					graph.getGraphLayoutCache().update(contextViews);			}			overlay(g);			if (offscreen != null) {				dirty.add(graph.toScreen(new Rectangle(vertex.getBounds())));				Rectangle t =					graph.toScreen(AbstractCellView.getBounds(contextViews));				if (t != null)					dirty.add(t);				dirty.grow(2, 2);				int sx1 =					(GraphConstants.NEGATIVE_ALLOWED)						? dirty.x						: Math.max(0, dirty.x);				int sy1 =					(GraphConstants.NEGATIVE_ALLOWED)						? dirty.y						: Math.max(0, dirty.y);				int sx2 = sx1 + dirty.width;				int sy2 = sy1 + dirty.height;				graph.getGraphics().drawImage(					offscreen,					sx1,					sy1,					sx2,					sy2,					sx1,					sy1,					sx2,					sy2,					graph);			}		}		protected Rectangle computeBounds(MouseEvent event) {			int left = initialBounds.x;			int right = initialBounds.x + initialBounds.width - 1;			int top = initialBounds.y;			int bottom = initialBounds.y + initialBounds.height - 1;			Point p = graph.fromScreen(graph.snap(new Point(event.getPoint())));			// Not into negative coordinates			if (!GraphConstants.NEGATIVE_ALLOWED) {				p.x = Math.max(0, p.x);				p.y = Math.max(0, p.y);			}			// Bottom row			if (index > 4)				bottom = p.y;			// Top row			else if (index < 3)				top = p.y;			// Left col			if (index == 0 || index == 3 || index == 5)				left = p.x;			// Right col			else if (index == 2 || index == 4 || index == 7)				right = p.x;			int width = right - left;			int height = bottom - top;			if (isConstrainedSizeEvent(event)) {				if (index == 3 || index == 4 || index == 5)					height = width;				else if (index == 1 || index == 6 || index == 2 || index == 7)					width = height;				else {					height = width;					top = bottom - height;				}			}			if (width < 0) { // Flip over left side				left += width;				width = Math.abs(width);			}			if (height < 0) { // Flip over top side				top += height;				height = Math.abs(height);			}			return new Rectangle(left, top, width + 1, height + 1);		}		// Dispatch the edit event		public void mouseReleased(MouseEvent e) {			if (index != -1) {				cachedBounds = computeBounds(e);				vertex.setBounds(cachedBounds);				CellView[] views =					AbstractCellView.getDescendantViews(						new CellView[] { vertex });				Map attributes = GraphConstants.createAttributes(views, null);				graph.getGraphLayoutCache().edit(attributes, null, null, null);			}			e.consume();			cachedBounds = null;			initialBounds = null;			firstDrag = true;		}		private void invalidate() {			// Retrieve current bounds and set local vars			Rectangle tmp = graph.getCellBounds(vertex.getCell());			if (tmp != null) {				tmp = new Rectangle(tmp);				graph.toScreen(tmp);				int handlesize = graph.getHandleSize();				int s2 = 2 * handlesize;				int left = tmp.x - handlesize;				int top = tmp.y - handlesize;				int w2 = tmp.x + (tmp.width / 2) - handlesize;				int h2 = tmp.y + (tmp.height / 2) - handlesize;				int right = tmp.x + tmp.width - handlesize;				int bottom = tmp.y + tmp.height - handlesize;				// Update control point positions				r[0].setBounds(left, top, s2, s2);				r[1].setBounds(w2, top, s2, s2);				r[2].setBounds(right, top, s2, s2);				r[3].setBounds(left, h2, s2, s2);				r[4].setBounds(right, h2, s2, s2);				r[5].setBounds(left, bottom, s2, s2);				r[6].setBounds(w2, bottom, s2, s2);				r[7].setBounds(right, bottom, s2, s2);			}		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -