有关GUI建立两图标之间连线的例子(完整)
2008-05-23 04:44
357 查看
google_ad_client = "pub-8800625213955058";
/* 336x280, 创建于 07-11-21 */
google_ad_slot = "0989131976";
google_ad_width = 336;
google_ad_height = 280;
//
首先实现图标的拖拉(技术相当简单、使用)
实现图标间添加连线的方法
连线可自动适应其起始、终止接点位置变化
在做计算机网管软件中应用,很好
具体源码如下:
本程序的运行方式:java -jar LinkeExample.jar
/* 336x280, 创建于 07-11-21 */
google_ad_slot = "0989131976";
google_ad_width = 336;
google_ad_height = 280;
//
首先实现图标的拖拉(技术相当简单、使用)
实现图标间添加连线的方法
连线可自动适应其起始、终止接点位置变化
在做计算机网管软件中应用,很好
具体源码如下:
/* * Copyright 29 Jun 2001 :), Inc. All Rights Reserved * $Id: LinkExample.java,v 1.5 2001/06/29 09:19:27 root Exp $ * $Author hujian Beijing . China $ * $Contact : cutestfox@263.net $ */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Vector; public class LinkExample { public static void main(String argv[]) { JFrame f = new JFrame("LinkExaple"); JLayeredPane container = new MapContainer(); f.getContentPane().add(container); container.setLayout(null); IconNode node[] = new IconNode[4]; for(int i = 0; i < 4; i ) { node[i] = new IconNode(); node[i].setSize(new Dimension(50,50)); container.add(node[i]); } node[0].appendLink(node[2]); node[0].appendLink(node[1]); node[0].appendLink(node[3]); f.setSize(new Dimension(200,200)); f.setVisible(true); } } class MapContainer extends JLayeredPane { public MapContainer() { super(); } public void drawLine(Graphics2D g, Point start, Point end) { g.drawLine(start.x, start.y, end.x, end.y); } public void paint(Graphics g) { Vector links = IconNode.links ; IconNode node1 = null, node2 = null; Point pp = null; for(int i = 0; i < links.size(); i ) { node1 = ((NodeLink)links.elementAt(i)).node1; node2 = ((NodeLink)links.elementAt(i)).node2; int[] p = Tools.getClosestPart( node1, node2 ); if( p != null && p[0] != -1 && p[1] != -1) { Graphics2D g2 = (Graphics2D)g; g2.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND)); g2.setColor(Color.darkGray); drawLine(g2, node1.getPartPoint(p[0]), node1.getProlongationPoint(p[0])); drawLine(g2, node1.getProlongationPoint(p[0]), node2.getProlongationPoint(p[1])); drawLine(g2, node2.getProlongationPoint(p[1]), node2.getPartPoint(p[1])); g2.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND)); g2.setColor(Color.gray); drawLine(g2, node1.getPartPoint(p[0]), node1.getProlongationPoint(p[0])); drawLine(g2, node1.getProlongationPoint(p[0]), node2.getProlongationPoint(p[1])); drawLine(g2, node2.getProlongationPoint(p[1]), node2.getPartPoint(p[1])); } } super.paint(g); } } class IconNode extends JComponent { public static Vector links = new Vector(); private int focusx =0, focusy =0; private Vector targets = new Vector(); public static final int PART_TOP = 0 ; public static final int PART_RIGHT = 1 ; public static final int PART_BOTTOM = 2 ; public static final int PART_LEFT = 3 ; private int prolongation ; public int getProlongation() { return ( prolongation = getWidth()/4 ); } public Point getCenter() { return new Point(getX() getWidth()/2,getY() getHeight()/2); } public Point getProlongationPoint(int part) { getProlongation(); switch(part) { case PART_TOP: return new Point(getX() getWidth()/2, getY()-prolongation); case PART_RIGHT: return new Point(getX() getWidth() prolongation, getY() getHeight()/2); case PART_BOTTOM: return new Point(getX() getWidth()/2, getY() getHeight() prolongation); case PART_LEFT: return new Point(getX()-prolongation, getY() getHeight()/2); default: return null; } } public Point getPartPoint(int part) { switch(part) { case PART_TOP: return new Point(getX() getWidth()/2, getY()); case PART_RIGHT: return new Point(getX() getWidth(), getY() getHeight()/2); case PART_BOTTOM: return new Point(getX() getWidth()/2, getY() getHeight()); case PART_LEFT: return new Point(getX(), getY() getHeight()/2); default: return null; } } public IconNode() { setBorder(BorderFactory.createRaisedBevelBorder()); addMouseListener(new MouseProcess()); addMouseMotionListener(new MouseMotionProcess()); } public void paint(Graphics g) { super.paint(g); } private class MouseProcess extends MouseAdapter { public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { focusx = e.getX(); focusy = e.getY(); if( getParent() instanceof JLayeredPane ) { ((JLayeredPane)getParent()).moveToFront(IconNode.this); } } } private class MouseMotionProcess extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { Container container = getParent(); int w = container.getWidth(); int h = container.getHeight(); int x = getX() e.getX()-focusx; int y = getY() e.getY()-focusy; if( x getWidth() > w ) x = w-getWidth(); if( y getHeight() > h ) y = h-getHeight(); if( x < 0 ) x = 0; if( y < 0 ) y = 0; setLocation(x, y); container.repaint(); container = null; } } public NodeLink findLink(IconNode node) { return null; } public boolean appendLink(IconNode node) { if( node == null || targets.contains(node) || node == this ) return false; targets.addElement(node); NodeLink link = null; for(int i = 0; i < links.size(); i ) { link = (NodeLink)links.elementAt(i); if( this.hashCode() < node.hashCode() ) { if( link.node1 == this && link.node2 == node ) break; } else { if( link.node2 == this && link.node1 == node ) break; } link = null; } if( link == null ) links.addElement(new NodeLink(this,node)); return node.appendLink(this); } } class NodeLink { public IconNode node1 = null; public IconNode node2 = null; public NodeLink(IconNode n1, IconNode n2) { if( n1.hashCode() < n2.hashCode() ) { node1 = n1; node2 = n2; }else if( n1.hashCode() > n2.hashCode() ) { node1 = n2; node2 = n1; } } } class Tools { public static Rectangle createRectangle(Point p1, Point p2) { if( p1 == null || p2 == null ) return null; int w = Math.abs(p1.x-p2.x); int h = Math.abs(p1.y-p2.y); return new Rectangle(Math.min(p1.x,p2.x)-1, Math.min(p1.y,p2.y)-1,w 2,h 2); } public static Rectangle createRectangle(IconNode node1, IconNode node2) { if( node1 == null || node2 == null ) return null; Point p1 = new Point(node1.getX() node1.getWidth()/2, node1.getY() node1.getHeight()/2); Point p2 = new Point(node2.getX() node2.getWidth()/2, node2.getY() node2.getHeight()/2); Rectangle rect = createRectangle(p1, p2); p1 = null; p2 = null; return rect; } public static Point[] getContainsPoints(IconNode node, Rectangle rect) { if( node == null || rect == null ) return null; Vector v = new Vector(); Point p = node.getPartPoint(IconNode.PART_TOP); if( rect.contains(p) ) v.addElement(p); p = node.getPartPoint(IconNode.PART_RIGHT); if( rect.contains(p) ) v.addElement(p); p = node.getPartPoint(IconNode.PART_BOTTOM); if( rect.contains(p) ) v.addElement(p); p = node.getPartPoint(IconNode.PART_LEFT); if( rect.contains(p) ) v.addElement(p); Point[] ret = new Point[v.size()]; for(int i=0;i < v.size();i ) ret[i] = (Point)v.elementAt(i); v.clear(); v = null; return ret; } public static Point[] getClosestPoint(Point[] p1, Point[] p2) { if( p1 == null || p2 == null ) return null; Point ret[] = new Point[2]; ret[0] = ret[1] = null; int mind = Integer.MAX_VALUE , distance; for(int i = 0; i < p1.length; i ) { for(int j = 0; j < p2.length; j ) { distance = (int)p1[i].distance(p2[j]); if( distance < mind ) { ret[0] = p1[i]; ret[1] = p2[j]; mind = distance; } } } return ret; } public static int getPart(IconNode node, Point p) { if( node == null || p == null ) return -1; if( p.x == node.getX() node.getWidth()/2 && p.y == node.getY() ) return IconNode.PART_TOP; else if( p.x == node.getX() node.getWidth() && p.y == node.getY() node.getHeight()/2 ) return IconNode.PART_RIGHT; else if( p.x == node.getX() node.getWidth()/2 && p.y == node.getY() node.getHeight() ) return IconNode.PART_BOTTOM; else if( p.x == node.getX() && p.y == node.getY() node.getHeight()/2 ) return IconNode.PART_LEFT; else return -1; } public static int[] getClosestPart(IconNode node1, IconNode node2) { Point[] p = getClosestPoint(node1, node2); if( p == null ) return null; int[] ret = new int[2]; ret[0] = ret[1] = -1; ret[0] = getPart(node1, p[0]); ret[1] = getPart(node2, p[1]); return ret; } public static Point[] getClosestPoint(IconNode node1,IconNode node2) { if( node1 == null || node2 == null ) return null; Rectangle rect = createRectangle(node1, node2); Point[] p1 = getContainsPoints(node1, rect); Point[] p2 = getContainsPoints(node2, rect); Point[] p = getClosestPoint(p1, p2); p1 = null; p2 = null; return p; } } |
相关文章推荐
- 有关GUI建立两图标之间连线的例子
- 使用GUI建立两图标之间连线的例子
- 和S5933比较起来,开发PLX9054比较不幸,可能是第一次开发PCI的缘故吧。因为,很多PCI的例子都是对S5933,就连微软出版的《Programming the Microsoft Windows Driver Model》都提供了一个完整的S5933的例子。 在这篇有关DDK的开发论文里。
- QT小例子GUI(主)线程与子线程之间的通信
- QT小例子GUI(主)线程与子线程之间的通信
- QT小例子GUI(主)线程与子线程之间的通信
- c#,arcengine,Arcobjects建立拓扑的完整例子
- EXTJS动态树,非常完整的一个例子
- Matlab GUI,窗体之间传递数据
- C#调用存储过程简单完整例子
- IOCP完整例子
- 配置IIS--通过多台设备之间建立局域网--测试Application_onStart事件计算当前网站程序在线人数
- JAVA操作XML的完整例子——W3C DOM篇
- 【JSP开发】有关session的登录注销的一个小例子
- UnityEditor中建立两个Color,实现mesh的颜色渐变(Gradient)(GUI.changed,SceneView.RepaintAll())
- Struts2 + uploadify 多文件上传完整的例子!
- (转)有关Auto encoder 和Denoising auto encoder分别和PCA之间的关系(学习笔记)
- eclipse+maven搭建cxf webservice 完整例子
- 使用Hibernate的一个完整例子
- 【JSP开发】有关session的登录注销的一个小例子