用JAVA画个简单的拓扑图

https://zhidao.baidu.com/question/151408808.html?qbl=relate_question_0&word=%D3%C3Java%BB%E6%D6%C6%BD%DA%B5%E3%D6%AE%BC%E4%B5%C4%CD%BC


问题描述:

用JAVA帮我画个简单的拓扑图,图形如图所示,要用java实现,麻烦写出代码,采用的话追加高分,谢谢
如果可以的话,帮我在图上随机生成参数,链路的参数(1~10),节点上2个参数,也是1~10
我要用这些参数来计算,最后生成一条路径。
生成的路径最好是动态生成的。
能做多少是多少,必有重谢!
Q-5369248

那个图我自己会画了,请教一下,在Eclipse里怎么关联两个JAVA程序?
先显示这个图,然后显示另外一个有路径的图(在此图基础上有一段路径加粗)。
在同一个窗口或者不在都行
用JAVA画个简单的拓扑图_第1张图片



解决方法:

如果需要的话留个邮箱,我把程序发给你。


晕~~我索性写个最短路径的给你吧,等着~

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.Random;

class Diagram_Panel extends JPanel {

        private Rectangle btnNew = new Rectangle(10, 10, 82, 22);

        private int state;

        private Random rnd = new Random();

        private int[][] vertices;

        private int[][] params;

        private int[][] linked;

        public Diagram_Panel () {

                setBackground(Color.WHITE);

                addMouseMotionListener(new MouseMotionAdapter() {

                        @Override public void mouseMoved (MouseEvent me) {

                                if (me.getX() >= btnNew.x && me.getX() <= btnNew.x + btnNew.width && me.getY() >= btnNew.y && me.getY() <= btnNew.y + btnNew.height) {

                                        if (state != 1) {

                                                state = 1;

                                                repaint();

                                        }

                                } else {

                                        if (state != 0) {

                                                state = 0;

                                                repaint();

                                        }

                                }

                        }

                        @Override public void mouseDragged (MouseEvent me) {

                                if (state == 2 || state == 1) {

                                        if (me.getX() >= btnNew.x && me.getX() <= btnNew.x + btnNew.width && me.getY() >= btnNew.y && me.getY() <= btnNew.y + btnNew.height)

                                                if (state != 2)

                                                        state = 2;

                                        else

                                                if (state != 1)

                                                        state = 1;

                                        repaint();

                                }

                        }

                });

                addMouseListener(new MouseAdapter() {

                        @Override public void mousePressed (MouseEvent me) {

                                if (state == 1) {

                                        state = 2;

                                        newGraph();

                                }

                        }

                        @Override public void mouseReleased (MouseEvent me) {

                                if (me.getX() >= btnNew.x && me.getX() <= btnNew.x + btnNew.width && me.getY() >= btnNew.y && me.getY() <= btnNew.y + btnNew.height)

                                        state = 1;

                                else

                                        state = 0;

                                repaint();

                        }

                });

        }

        private void mouseOver (Rectangle btn, String str, Graphics g) {

                Color tmp = g.getColor();

                g.setColor(Color.GRAY);

                g.fillRect(btn.x, btn.y, btn.width+1, btn.height+1);

                g.setColor(Color.WHITE);

                g.drawLine(btn.x, btn.y, btn.x, btn.y+btn.height-1);

                g.drawLine(btn.x, btn.y, btn.x+btn.width-1, btn.y);

                g.drawString(str, btn.x+10, btn.y+15);

                g.setColor(tmp);

        }

        private void mouseOff (Rectangle btn, String str, Graphics g) {

                Color tmp = g.getColor();

                g.setColor(Color.BLACK);

                g.drawRect(btn.x+1, btn.y+1, btn.width, btn.height);

                g.setColor(Color.WHITE);

                g.fillRect(btn.x, btn.y, btn.width, btn.height);

                g.setColor(Color.BLACK);

                g.drawRect(btn.x, btn.y, btn.width, btn.height);

                g.drawString(str, btn.x+10, btn.y+15);

                g.setColor(tmp);

        }

        private void mouseDown (Rectangle btn, String str, Graphics g) {

                Color tmp = g.getColor();

                g.setColor(Color.DARK_GRAY);

                g.fillRect(btn.x+1, btn.y+1, btn.width+1, btn.height+1);

                g.setColor(Color.WHITE);

                g.drawLine(btn.x+2, btn.y+btn.height+1, btn.x+btn.width+1, btn.y+btn.height+1);

                g.drawLine(btn.x+btn.width+1, btn.y+2, btn.x+btn.width+1, btn.y+btn.height+1);

                g.drawString(str, btn.x+11, btn.y+16);

                g.setColor(tmp);

        }

        @Override public void paint (Graphics g) {

                super.paint(g);

                switch (state) {

                        case 0:

                                mouseOff(btnNew, "New Graph", g);

                                break;

                        case 1:

                                mouseOver(btnNew, "New Graph", g);

                                break;

                        case 2:

                                mouseDown(btnNew, "New Graph", g);

                                break;

                }

                for (int i=0; i 0) {

                                        g.drawLine(vertices[i][0]+5, vertices[i][1]+5, vertices[j][0]+5, vertices[j][1]+5);

                                        g.drawString(linked[i][j]+"", (vertices[i][0]+vertices[j][0])/2, (vertices[i][1]+vertices[j][1])/2);

                                }

                g.setColor(Color.WHITE);

                for (int i=0; i 20)

                                        linked[i][j] = rnd.nextInt(10)+1;

                for (int i=0; i

运行结果截图为:

用JAVA画个简单的拓扑图_第2张图片




你可能感兴趣的:(java)