ImageVerifierCode 换一换
格式:DOCX , 页数:22 ,大小:22.25KB ,
资源ID:7794856      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7794856.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java深度优先.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

java深度优先.docx

1、java深度优先package wg;import java.awt.BorderLayout;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class Graphic exte

2、nds JFrame implements ActionListener MyPanel mp = null; JPanel jp = null; JButton add = null; JButton add1 = null; JButton tra = null; JButton cancel = null; JLabel jl = null; JComboBox jList = null;/ 下拉列表 String travel = 判断是否连接图, 查看拓扑排序,判断是否有环,判断是否双向连通的 ; JLabel jlb = null;/ 装饰标签 public Graphic() m

3、p = new MyPanel(); jl = new JLabel(此处显示结果:); jList = new JComboBox(travel); / jList.addItemListener(this); add = new JButton(加节点); add.addActionListener(this); add.setActionCommand(a); add1 = new JButton(加边); add1.addActionListener(this); add1.setActionCommand(aa); jlb = new JLabel( ); tra = new JBu

4、tton(确认); tra.addActionListener(this); tra.setActionCommand(b); cancel = new JButton(删除节点); cancel.addActionListener(this); cancel.setActionCommand(c); jp = new JPanel(); jp.add(add); jp.add(add1); jp.add(cancel); jp.add(jlb); /jp.add(jl); jp.add(jList); jp.add(tra); mp.addMouseListener(mp); mp.addM

5、ouseMotionListener(mp); this.setFocusable(true); this.add(mp); this.add(jp, BorderLayout.NORTH); this.add(jl, BorderLayout.AFTER_LAST_LINE); this.setSize(600, 500); this.setLocation(350, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); public static void main(String

6、args) new Graphic(); Override public void actionPerformed(ActionEvent e) boolean iscycle=false; if (e.getActionCommand().equals(a) / 加节点 mp.isTravel = false; mp.isAdd1 = false; mp.isAdd = true; else if (e.getActionCommand().equals(aa) / 加边 mp.isTravel = false; mp.isAdd = false; mp.isAdd1 = true; els

7、e if (e.getActionCommand().equals(c) / 删除 mp.isTravel = true; mp.isAdd = false; mp.isAdd1 = false; mp.delete(); else if (e.getActionCommand().equals(b) / 确认 mp.isTravel = true; for (int i = 0; i mp.nodes.size(); i+) / 遍历后把正在访问均重新变为空 Node node = mp.nodes.get(i); node.isVisit = false; this.update(this

8、.getGraphics(); String s = jList.getSelectedItem().toString();/ 得到选项 if (s = 判断是否连接图) if(mp.isconnect() jl.setText(此图是连接的); else jl.setText(此处显示结果:此图不是连接的,可分为+(mp.num+1)+个子图); else if (s= 查看拓扑排序) String temp= ; /mp.Topological_sort0=1 mp.Dfs(); for(int i=0;imp.nodes.size();i+) temp=temp+ +mp.Topolog

9、ical_sorti;/拓扑排序的节点访问次数 jl.setText(此处显示结果:拓扑排序为:+temp); else if (s = 判断是否有环) iscycle= mp.iscycle(); if(!iscycle)jl.setText(此处显示结果:这个图没有环的); else if (s = 判断是否双向连通的) / System.out.print(mp.isbiconnect()+ +mp.flag); if(mp.isbiconnect() jl.setText(此处显示结果:这个无向图是双向连通的); else jl.setText(此处显示结果:这个无向图不是双向连通的)

10、; for (int i = 0; i mp.nodes.size(); i+) / 遍历后把正在访问均重新变为空 Node node = mp.nodes.get(i); node.isVisit = false; if(iscycle)/判断有环的话 环的节点找到 并且加亮显示 this.update(this.getGraphics(); try Thread.sleep(1500); catch (InterruptedException e1) / TODO Auto-generated catch block e1.printStackTrace(); jl.setText(此处显

11、示结果:这个图是有环的,共:+mp.flag+个); int j=0; while(mp.cyclej!=10000)/10000是环的最后一个节点 表示结束,后面没有节点了 mp.nodes.get(mp.cyclej).isVisit=true; this.update(this.getGraphics(); / System.out.print(mp.cyclej); try Thread.sleep(1500); catch (InterruptedException e1) / TODO Auto-generated catch block e1.printStackTrace();

12、 j+; package wg;import java.awt.Color;import java.awt.Graphics;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.util.Vector;import javax.swing.JLabel;import javax.swing.JPanel;public class MyPanel extends JPanel implements Mou

13、seListener, MouseMotionListener int x, y; int x1, y1; int width = 36; int height = 36; int total = 1000; int X = new inttotaltotal;/ 邻接矩阵,判断x方向是否相连 int Y = new inttotaltotal;/ 邻接矩阵,判断y方向是否相连 int Topological_sort = new inttotal; int cycle = new inttotal; boolean visit = new booleantotaltotal;/ 判断结点是否

14、访问过 boolean isOther = false;/ 画关系时候判断是否找另一个点 boolean isTravel = false;/ 判断是否正在遍历 Vector nodes = new Vector(); Vector lines = new Vector(); Vector label = new Vector(); Node start = new Node(0, 0); boolean isAdd = false; boolean isAdd1 = false; boolean isFirst = true; int xx, yy, ii; Vector choose =

15、new Vector();/ 拖拽时候用,用来把所有与拖拽节点有边的的直线放进来 public MyPanel() / 初始化 for (int i = 0; i total; i+) for (int j = 0; j total; j+) /访问为置0 Xij = 0; Yij = 0; visitij = false; public void paint(Graphics g) /记住这个每一次调用 this.repaint()的时候都要自动调用这个函数 super.paint(g); g.setColor(Color.DARK_GRAY);/将panel的背景设置灰色 就是在背景色灰色

16、上画的图 g.fillRect(0, 0, 600, 450);/画一个矩形画布也就是panel for (int i = 0; i nodes.size(); i+) / 画节点 Node node = nodes.get(i); node.num = i + 1; this.draw(g, node.x, node.y);/在鼠标的当前位置画一个节点,draw是我写的函数 g.setColor(Color.BLUE); g.drawString( + (i + 1), node.x, node.y);/画上节点的编号,因为数组是从0开始的 所以图示从1开始的所以 i+1 if (node.

17、isVisit) / 如果节点正在访问,则图上红色 g.setColor(Color.RED);/设置话节点的颜色 g.fill3DRect(node.x, node.y, width, height, false);/画节点 for (int i = 0; i lines.size(); i+) / 画直线 LineNode line = lines.get(i); this.draw1(g, line.x1, line.y1, line.x, line.y);/从鼠标的起始位置到终止位置 画一条直线,这个函数也是我自己定义的 if (isAdd) /添加一个新的节点 this.draw(g

18、, x, y); / if (isAdd1) /添加一条心的直线 if (isOther) /如果这条直线有了其实位置 this.draw1(g, x1, y1, x, y); public boolean isconnect() /判断是否连接函数 flag = 0; Dfs(); if (num != 0)/如果一个图里有多个连通分量 则不是连接的 return false; else return true; public void draw(Graphics g, int x, int y) / 画节点 g.setColor(Color.GREEN);/画节点的颜色 g.draw3DR

19、ect(x, y, width, height, false);/这个是系统函数 public void draw1(Graphics g, int x1, int y1, int x, int y) / 画边 g.setColor(Color.ORANGE);/画边的颜色 g.drawLine(x1, y1, x, y);/这个是系统函数 Override public void mouseClicked(MouseEvent e) / 监听鼠标单击事件 / TODO Auto-generated method stub if (isAdd) / 如果加节点 Node node = new

20、Node(e.getX(), e.getY(); nodes.add(node); start = nodes.get(0);/ 初始节点默认赋值为第一个节点 Topological_sort0 = 1; else if (isAdd1) / 加边 if (isOther = false) / 绘制直线第一个点的话 x1 = e.getX();/当前鼠标所在的坐标 y1 = e.getY(); for (int i = 0; i = node.x & x1 = node.y & y1 = node.y + height) x1 = node.x + width / 2;/ 将边的两个端点均赋值

21、为节点的中心 y1 = node.y + height / 2; break; isOther = true; else / 绘制直线第二个点 x = e.getX();/当前鼠标所在的坐标 y = e.getY(); for (int i = 0; i = node.x & x = node.y & y = node.y + height) x = node.x + width / 2; y = node.y + height / 2; break; / LineNode line = new LineNode(x1, y1, x, y);/ 新建一条直线,并加入到直线集合中 其中参数是两个

22、端点的值 lines.add(line); Xxx1 = 1; / 用于表示两个节点之间有连接,用于后面遍历 Xx1x = 1; Yyy1 = 1; Yy1y = 1; isOther = false; if (e.getClickCount() = 2) / 双击将该节点颜色变为红色,可以从当前节点开始遍历 x = e.getX(); y = e.getY(); flag = 1; for (int i = 0; i = node.x & x = node.y & y = node.y + height) node.isVisit = true; Graphics g = this.getG

23、raphics();/ 这样得到画笔 g.setColor(Color.RED); g.fill3DRect(node.x, node.y, width, height, false); start = node;/ 将初始节点赋值为当前 break; this.repaint(); Override public void mouseEntered(MouseEvent e) Override public void mouseExited(MouseEvent e) / TODO Auto-generated method stub Override public void mousePr

24、essed(MouseEvent e) / TODO Auto-generated method stub Override public void mouseReleased(MouseEvent e) / TODO Auto-generated method stub this.isFirst = true;/ 设置回真 Override public void mouseDragged(MouseEvent e) / 监听拖拽事件/当鼠标在面板里拖动时候 自动执行这个函数 / TODO Auto-generated method stub x = e.getX(); y = e.getY

25、(); if (this.isFirst) / 设置这个变量目的是完成一次整个拖拽过程只判断一次在哪个节点里 for (ii = 0; ii = node.x & x = node.y & y = node.y + height) xx = x - node.x;/ 把当前间距分别算出来 yy = y - node.y; choose.clear();/ 每一次判断节点的时候要新清空 for (int jj = 0; jj lines.size(); jj+) LineNode l = lines.get(jj); if (node.x + width / 2) = l.x1 & (node.

26、y + height / 2) = l.y1) Choose c = new Choose(jj, false);/ 如果该点是直线第一个定的点即为x1,y1,则为假 choose.add(c); int x4 = l.x;/这个表示直线的两个端点 int y4 = l.y; int x5 = l.x1; int y5 = l.y1; Xx4x5 = 0; / 重新刷新连接关系 Xx5x4 = 0; Yy4y5 = 0; Yy5y4 = 0; if (node.x + width / 2) = l.x & (node.y + height / 2) = l.y) / 如果是第二个点 Choose c = new Choose(jj, true); choose.add(c); int x4 = l.x; int y4

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1