有关GUI建立两图标之间连线的例子(完整)

java,Swing中单击实现两点相连 这是老师给我们的一个作业,做完了,当做自己的第一篇博文吧。       程序运行后,弹出黑色界面,有三个白圈,标有北京,上海,广州,点击任意一圈,即出现云另外两全的连线。Java学的不精,之前一直在思考如何实现在已有的图上的线条绘制。后采用八个参数将其解决。       import java.awt.BasicStroke;import java.awt.BorderLayout; 阅读详情
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 首先实现图标的拖拉(技术相当简单、使用) 实现图标间添加连线的方法 连线可自动适应其起始、终止接点位置变化 在做计算机网管软件中应用,很好 具体源码如下:



/*

 *  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;

	}

}

本程序的运行方式:java -jar LinkeExample.jar
Qt之连线、绘图(一)QGraphicsView架构 QGraphicsView架构主要由场景、视图、图形项三部分组成,关于基础入门的学习 阅读详情

相关推荐

Visual Basic图标资源库 - 1100个GUI图标

本文还有配套的精品资源,点击获取 简介:图形用户界面(GUI)对于软件的用户体验至关重要,图标作为GUI的关键部分,能提升应用的直观性和美观度。本资源包含1100个专为Visual Basic开发者设计的常用图标,涵盖了多种功能和场景,如文件操作、编辑、帮助等。图标以ICO格式提供,易于下载并集成到VB应用程序中。开发者无需自行设计或寻找图标,可直接应用于项目,从而提高开发...

weixin_32925455的博客 861

两点画线

已知:x[1],y[1];x[2],y[2] ,运用python 连接两点 import matplotlib.pyplot as plt plt.plot([x[1], x[2]], [yy[1],yy[2]],linewidth=0.5)

pingfan2014的博客 2451

qt实现qgaphicsitem的拖动连线等操作

支持qgraphicitem在scene中拖动和缩放,支持qgraphicsitem间的连线操作,代码可扩展。

XML家族图谱(图)

google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//<script type="text/java

java专栏 1007

GUI 中的拖动线:GUI 中的线可以拖动-matlab开发

运行 DragLineGUI.m 这个演示模仿道格·赫尔的优秀演示@: http://blogs.mathworks.com/pick/2008/05/27/advanced-matlab-capture-mouse-movement/ 但已修改为在使用 GUIDE 创建的文件中工作。 可以用鼠标拖动这两行,并且可以通过按下按钮将位置传递到命令窗口。 它还展示了如何以与 GUIDE 相同的方式将 GUI 句柄传递给用户定义的回调函数。

Kali下RTL8812AU网卡5G抓包精准适配指南

WiFi监听模式是无线安全测试的基础能力,其核心在于网卡驱动对802.11a/n/ac协议栈的完整支持。RTL8812AU系列USB无线网卡因成本低、兼容性广被广泛用于渗透测试,但其芯片版本碎片化(如VA/VC/ACM)与Linux内核演进(6.1~6.5)存在严重适配断层,导致5GHz频段Beacon帧捕获失败、信道切换异常、注入功能残缺等典型问题。技术价值在于突破‘即插即用’幻觉,建立从USB设备ID识别、固件版本校验到射频寄存器级补丁的全链路可控流程。典型应用场景包括CTF无线赛题实战、红蓝对抗中的W

weixin_33783283的博客 774

有关GUI建立两图标之间连线的例子

google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//<script type="text/java

java专栏 398

Unity 第一人称视角控制移动、旋转

第一人称视角操作此功能主要是借鉴YouTube,写的蛮好我就搬运过来让大家也试试。

qq_36515472的博客 4207

使用GUI建立两图标之间连线的例子

google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//<script type="text/java

java专栏 386

UE5新手必看:如何在UMG控件中嵌入浏览器实现Web页面展示(附完整步骤)

本文为UE5新手提供了在UMG控件中无缝集成Web浏览器的完整实战指南。详细介绍了从启用WebBrowser插件、创建并配置WebBrowser控件蓝图,到编写动态加载与交互逻辑,以及在游戏关卡中实例化UI的全过程。文章还分享了性能优化技巧与常见问题排查方法,帮助开发者高效实现游戏UI内的Web页面展示与交互。

weixin_29243063的博客 716

SPSS&Mplus—共同方法偏差(Common Method Bias)控制与检验

共同方法偏差(Common Method Bias)的控制方法:程序控制(Procedural)和统计控制(Statistical)。

m0_54401011的博客 1万+

Tkinter GUI界面添加图标及窗口图标

前言:本文代码运行环境 win10+python2 一、添加窗口图标   因为Tkinter打包不支持将图片引入程序中,所有采用的线转换成ASCII码的形式。 生成ASCII码 import base64 open_icon = open("icon2.ico","rb") #选择图标文件 b64str = base64.b64encode(open_ico...

weixin_30384217的博客 5519

工业机器人关节拆装流程_一文读懂模块化直驱电机及机器人关节模组

私信“干货”二字,即可领取138G伺服与机器人专属及电控资料!如果说芯片是智能行业最为重要的核心元件,那么电机几乎可以说是支撑了整个自动化和大工业生产以及现代社会最为重要的部件,全世界90%的能源是由电机发出来的,而70%是由电机消耗调的,电机可以说是整个人类社会能源循环的占有绝对地位的产品。普通伺服电机伺服电机其实已经算是电动机中比较高端的类型,特别是永磁同步类型的伺服电机,由于其启动快速,惯量...

weixin_32159771的博客 2163

【Canvas与图标】GUI图标

用Html5/Canvas绘制GUI图标。

一念非凡 1179

ICNN实现入侵检测的思路与源码(CIC-IDS-2017)

基于ICNN实现入侵检测,可用于现实网络

t1274171989的博客 8421

【25届数字IC秋招总结】面试经验15——比特大陆-算能

25届数字ic面试记录——比特大陆-算能

lalala0635的博客 561

matlab gui修改图标,Matlab的GUI中左上角修改为自己想要的图标

有时候,自己做好的GUI界面,希望有自己的标志,一个很好的办法就是在GUI界面的左上角加上自己独有的图标:如自己的照片或者是自己学校的照片,下面以我们河海大学为例子说明:修改方法如下:%GUI左上角图标的更改,假设GUI的Tag为figure1,在其OpeningFcn里添加h = handles.figure1; %返回其句柄newIcon = javax.swing.ImageIcon('河...

weixin_39593247的博客 1475

100天精通Python(基础篇)——第3天:运算符全解析(算术运算符、比较运算符、逻辑运算符、赋值运算符及其优先级规则)

100天精通Python(基础篇)——第3天:运算符全解析(算术运算符、比较运算符、逻辑运算符、赋值运算符及其优先级规则)

努力让自己发光,对的人才能迎着光而来 14万+

matlab gui 按钮背景,Matlab gui教程0x8–自定义GUI背景图案和按钮图标

美化您的GUI外观,会使用您的GUI程序更加吸引大家的视线!特别是对商业程序,终端用户可能会很注重程序的界面布局(当然功能必须首先达标),爱美之心人皆有之,这个不需要我解释吧!在本节教程我将想大家显示,如何为您的GUI添加一个漂亮背景,以及为一些按钮(不是所有的控件都可以)添加图标!其实原理很简单,大家不妨先了解下:1.背景,其实就是将图片显示在axes,而axes覆盖整个GUI2.图标,其实将控...

weixin_39576018的博客 2642

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

matlab gui修改图标,[转载]Matlab的GUI中左上角修改为自己想要的图标

有时候,自己做好的GUI界面,希望有自己的标志,一个很好的办法就是在GUI界面的左上角加上自己独有的图标:如自己的照片或者是自己学校的照片,下面以我们河海大学为例子说明:修改方法如下:%GUI左上角图标的更改,假设GUI的Tag为figure1,在其OpeningFcn里添加h = handles.figure1; %返回其句柄newIcon = javax.swing.ImageIcon('河...

weixin_32746931的博客 1052

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

GUI编程之图标Icon

一、主要实现 在label上添加图标icon package lesson03; import javax.swing.*; import java.awt.*; public class TestIcon extends JFrame implements Icon { private int Width; private int Height; //无参构造 public TestIcon() { } //有参构造 public Tes.

qq_38723455的博客 932

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

matlab如何修改GUI的图标 左上角的logo

在使用matlab画图或者制作GUI的时候,我们常常需要定制自己专属的图标,或者用图标表示其作用,但是默认的GUI图标都是matlab的标志,如图:

肆拾伍的博客 9675

Matlab GUI 界面编程:界面左上角图标更改

Matlab GUI 界面编程:界面左上角图标更改 在界面的m文件中,找到界面的OpeningFcn函数 % --- Executes just before day1 is made visible. function day1_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args...

qq_26126831的博客 4147

GUI是什么

GUI是什么 1.GUI是什么–简介   GUI的全称为Graphical User Interface,图形化界面或图形用户接口,是指采用图形方式显示的计算机操作环境用户接口。与早期计算机使用的命令行界面相比,图形界面对于用户来说更为简便易用。GUI的广泛应用是当今计算机发展的重大成就之一,它极大地方便了非专业用户的使用人们从此不再需要死记硬背大量的命令,取而代之的是通过窗口、菜单、按键等方式来...

风清竹叶青 2万+

GUI移动图标

#include #include #include #include #Region ### START Koda GUI section ### Form=$Form1 = GUICreate("Form1", 383, 361, 330, 191)$Icon1 = GUICtrlCreateIcon("F:/E/17292个精品ICO图标下载(最全的图标集合)(1)/17292个精品IC

lw35888894的专栏 801
上一篇: java.net.URL如何通过proxy访问internet资源
下一篇: 一个Scocket实例程序
java169
博客等级 码龄19年 27粉丝 2568原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址