import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.GeneralPath;
import java.lang.Thread;
import java.lang.Runnable;
public class Graph extends JFrame
{
public static int wait=0;
JButton stop= new JButton("STOP"); //暂停按钮
JButton go= new JButton("GO"); //开始按钮
public Graph ()throws Exception
{
setBounds(0, 0, 1380, 800); //设定大小
this.setBackground(Color.WHITE); //背景色
this.setTitle("基于OSPF协议的有向图最短路Dijkstra算法动态过程演示系统");
this.setLayout(null);
JLabel lblNewLabel = new JLabel("<html>最<br>短<br>路<br>算<br>法<br>动<br>态<br>过<br>程");
lblNewLabel.setFont(new Font("长城行楷体", Font.BOLD, 60)); //标签相关设置
lblNewLabel.setVerticalAlignment(SwingConstants.TOP); //置顶
lblNewLabel.setForeground(new Color(0, 0, 0));
lblNewLabel. setBounds(20,0,90,800);
add(lblNewLabel);
Gra temp=new Gra(); // 建图
temp.setBounds(90, 0, 1290, 800);
temp.readin();
add(temp);
this.setVisible(true);
temp.play(); //演示算法过程
}
}
class Gra extends JPanel
{
int [] head; //链式前向星存图
int [][] edge;
int nume;
int maxn; //顶点数
int maxm; //边数
int inf=0x3f3f3f3f; //无穷大
int ss,tt; //源汇点
int change; //开关量
int []mark; //最短路的标记
int []dis; //距离
int []got; //是否需要闪烁标记(正在被更新)
points [] dian; //点类
int [][] lines;
public Gra() //初始化构造函数
{
head=new int [1005];
edge=new int [5000][3];
lines= new int [10000][5];
dis=new int [1006];
got=new int [5000];
mark=new int [1006];
nume=0;
change=0;
for(int i=0;i<1000;i++)
{
head[i]=-1;dis[i]=inf;got[i]=mark[i]=0;
}
}
public void adde(int i,int j,int w) //添加一条有向边
{
edge[nume][0]=j;edge[nume][1]=head[i];head[i]=nume;
edge[nume++][2]=w;
}
void magic0() //延时重画函数
{
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
}
void magic() //闪烁控制函数
{
int cur=5; //变换次数
while(cur!=0)
{
change=1; //灯开
magic0();
change=0; //灯灭
magic0();
cur--;
//System.out.println(cur);
}
}
public void readin ()throws Exception //读入图函数
{
java.io.File file= new java.io.File("scores.txt"); //读入数据
// java.io.File file= new java.io.File("scores2.txt"); //读入数据
// java.io.File file= new java.io.File("radscores.txt");
Scanner input =new Scanner(file);
while(input.hasNext())
{
maxn=input.nextInt();
maxm=input.nextInt();
int from,to,w;
for(int i=0;i<maxm;i++)
{
from=input.nextInt();
to=input.nextInt();
w=input.nextInt();
adde(from,to,w);
}
ss=input.nextInt(); tt=input.nextInt();
dis[ss]=0;
}
input.close();
java.io.File file2 = new java.io.File("points.txt"); //读入坐标点
// java.io.File file2 = new java.io.File("points2.txt");
//java.io.File file2= new java.io.File("radpoints.txt");
Scanner input2 =new Scanner(file2);
dian =new points [maxn+1];
for(int i=1;i<=maxn;i++)
{
dian[i]=new points(input2.nextInt(),input2.nextInt());
}
}
public void play() //Dijkstra算法
{
int marks=1;
while(marks==1)
{
int mins=inf;
marks=0;
int cur=0;
for(int i=1;i<=maxn;i++)
{
if(mark[i]==0&&dis[i]<mins)
{
mins=dis[i];
cur=i;
marks=1;
}
}
got[cur]=1; //点需要闪
magic();
got[cur]=0; //关闭该点,之后就不再闪
mark[cur]=1;
for(int j=head[cur];j!=-1;j=edge[j][1])
{
int to=edge[j][0];
if(mark[to]==0&&dis[to]>dis[cur]+edge[j][2])
{
dis[to]=dis[cur]+edge[j][2];
lines[j][0]=1; //该线已变色
lines[j][1]=1; //该线需要闪
magic();
lines[j][1]=0; //无需再闪
}
}
}
}
protected void paintComponent( Graphics g) //画图函数
{
super.paintComponent(g); //清屏
g.setColor(Color.green);
Font font = new Font("Arial", Font.BOLD, 25); //字体大小
g.setFont(font);
for(int i=1;i<=maxn;i++)
{
if(got[i]==1) //该点在闪
{
if(change==1) //灯开
g.setColor(Color.red);
else g.setColor(Color.GREEN); //灯灭
}
else
{
if(mark[i]==1) //该点已变
g.setColor(Color.red);
else g.setColor(Color.GREEN);
}
g.fillOval(dian[i].x, dian[i].y, 50, 50);
String ts;
if(dis[i]==inf)
ts="+∞";
else ts=new String().valueOf(dis[i]);
g.drawString(ts,dian[i].x,dian[i].y);
}
g.setColor(Color.GRAY);
for(int i=1;i<=maxn;i++)
{
for(int j=head[i];j!=-1;j=edge[j][1])
{
if(lines[j][1]==1) //需要闪烁
{
if(change==1)
g.setColor(Color.BLUE);
else g.setColor(Color.GRAY);
}
else
{
if(lines[j][0]==1) //已经变色
g.setColor(Color.BLUE);
else g.setColor(Color.GRAY);
}
// g.drawString(new String().valueOf(dis[edge[j][2]]),dian[i].x,dian[i].y);
Graphics2D g2= (Graphics2D)g;
drawAL(dian[i].x+25,dian[i].y+25, dian[edge[j][0]].x+25, dian[edge[j][0]].y+25,g2); //划边
// g.drawLine(dian[i].x+25,dian[i].
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
使用JAVA SWing实现鼠标点击拖拽绘制图,自行设置边权,自行设置源点,动态演示Dijkstra求单源最短路的过程。成品效果见https://blog.csdn.net/qq_44643644/article/details/118517988
资源推荐
资源详情
资源评论
收起资源包目录
ViewDijkstra.zip (30个子文件)
ViewDijkstra
bin
Gra.class 6KB
points.class 401B
Arithmetic
DijkstraArithmetic.class 3KB
PanleAndFrame
ShowPanle$2.class 719B
MainFrame.class 2KB
DrawPanle$2.class 868B
DrawPanle$1.class 862B
ShowPanle.class 3KB
DrawPanle.class 6KB
MainFrame$1.class 1KB
ShowFrame.class 903B
ShowPanle$1.class 929B
Graph.class 2KB
init.class 456B
ShowBase
Relation.class 478B
Point.class 760B
Borde.class 1KB
.settings
org.eclipse.jdt.core.prefs 781B
src
Arithmetic
DijkstraArithmetic.java 2KB
PanleAndFrame
DrawPanle.java 6KB
ShowPanle.java 3KB
MainFrame.java 2KB
ShowFrame.java 436B
init.java 559B
ShowBase
Point.java 533B
Relation.java 404B
Borde.java 994B
Graph.java 10KB
.project 388B
.classpath 396B
共 30 条
- 1
资源评论
- 雨蛏2022-03-01用户下载后在一定时间内未进行评价,系统默认好评。
- 残月初见2022-06-30用户下载后在一定时间内未进行评价,系统默认好评。
- m0_532205552022-06-30用户下载后在一定时间内未进行评价,系统默认好评。
赴星辰大海
- 粉丝: 5993
- 资源: 6
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功