📄 graph.java
字号:
import java.util.*;import java.awt.*;import java.applet.Applet;import java.awt.event.*;class Node { double x;//横坐标 double y;//纵坐标 double dx; double dy;//x,y方向上的单位向量 boolean fixed;//判断是否固定 String lbl;//结点名 int time=0;}class Edge { int from; int to; double len;}//画面面板class GraphPanel extends Panel implements Runnable, MouseListener, MouseMotionListener { Graph graph; int nnodes; //结点个数 Node nodes[] = new Node[100];//结点数组 int nedges;//边的个数 Edge edges[] = new Edge[200];//边的数组 Thread relaxer;//松开 boolean stress;//拉伸度 boolean random;//随机坐标变量 //给画板添加鼠标监视器 GraphPanel(Graph graph) { this.graph = graph; addMouseListener(this); } //按名字找到一个结点,并把找到的这个结点添加到新的位置 int findNode(String lbl) { for (int i = 0 ; i < nnodes ; i++) { if (nodes[i].lbl.equals(lbl)) { return i; } } return addNode(lbl); } //新建一个结点,把它添加到随机的位置,返回结点个数加一 int addNode(String lbl) { Node n = new Node(); n.x = 10 + 380*Math.random(); n.y = 10 + 380*Math.random(); n.lbl = lbl; nodes[nnodes] = n; return nnodes++; } //给指定的两个结点连边 void addEdge(String from, String to, int len) { Edge e = new Edge(); e.from = findNode(from); e.to = findNode(to); e.len = len; edges[nedges++] = e; } //线程 public void run() { Thread me = Thread.currentThread();//返回CPU正在使用的线程 while (relaxer == me) { relax(); if (random && (Math.random() < 0.03)) { Node n = nodes[(int)(Math.random() * nnodes)]; if (!n.fixed) { n.x += 100*Math.random() - 50; n.y += 100*Math.random() - 50; } graph.play(graph.getCodeBase(), "audio/drip.au");//获取graph中的URL资源 } try { Thread.sleep(100); } catch (InterruptedException e) { break; } } } // synchronized void relax() { //使用互斥锁 for (int i = 0 ; i < nedges ; i++) { Edge e = edges[i]; double vx = nodes[e.to].x - nodes[e.from].x; double vy = nodes[e.to].y - nodes[e.from].y; double len = Math.sqrt(vx * vx + vy * vy); //算边长 len = (len == 0) ? .0001 : len; double f = (edges[i].len - len) / (len * 3); double dx = f * vx; double dy = f * vy; nodes[e.to].dx += dx; nodes[e.to].dy += dy; nodes[e.from].dx += -dx; nodes[e.from].dy += -dy; } for (int i = 0 ; i < nnodes ; i++) { Node n1 = nodes[i]; double dx = 0; double dy = 0; for (int j = 0 ; j < nnodes ; j++) { if (i == j) { continue; } Node n2 = nodes[j]; double vx = n1.x - n2.x; double vy = n1.y - n2.y; double len = vx * vx + vy * vy; if (len == 0) { dx += Math.random(); dy += Math.random(); } else if (len < 100*100) { dx += vx / len; dy += vy / len; } } double dlen = dx * dx + dy * dy; if (dlen > 0) { dlen = Math.sqrt(dlen) / 2; n1.dx += dx / dlen; n1.dy += dy / dlen; } } Dimension d = getSize(); for (int i = 0 ; i < nnodes ; i++) { Node n = nodes[i]; if (!n.fixed) { n.x += Math.max(-5, Math.min(5, n.dx)); n.y += Math.max(-5, Math.min(5, n.dy)); } if (n.x < 0) { n.x = 0; } else if (n.x > d.width) { n.x = d.width; } if (n.y < 0) { n.y = 0; } else if (n.y > d.height) { n.y = d.height; } n.dx /= 2; n.dy /= 2; } repaint(); } Node pick; boolean pickfixed; Image offscreen; Dimension offscreensize; Graphics offgraphics; final Color fixedColor = Color.red; final Color selectColor = Color.pink; final Color edgeColor = Color.black; final Color nodeColor = new Color(250, 220, 100); final Color stressColor = Color.darkGray; final Color arcColor1 = Color.black; final Color arcColor2 = Color.pink; final Color arcColor3 = Color.red; public void paintNode(Graphics g, Node n, FontMetrics fm) { int x = (int)n.x; int y = (int)n.y; g.setColor((n == pick) ? selectColor : (n.fixed ? fixedColor : nodeColor)); int w = fm.stringWidth(n.lbl) + 10; int h = fm.getHeight() + 4; g.fillRect(x - w/2, y - h / 2, w, h); g.setColor(Color.black); g.drawRect(x - w/2, y - h / 2, w-1, h-1); g.drawString(n.lbl, x - (w-10)/2, (y - (h-4)/2) + fm.getAscent()); } public synchronized void update(Graphics g) { Dimension d = getSize(); if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) { offscreen = createImage(d.width, d.height); offscreensize = d; if (offgraphics != null) { offgraphics.dispose(); } offgraphics = offscreen.getGraphics(); offgraphics.setFont(getFont()); } offgraphics.setColor(getBackground()); offgraphics.fillRect(0, 0, d.width, d.height); for (int i = 0 ; i < nedges ; i++) { Edge e = edges[i]; int x1 = (int)nodes[e.from].x; int y1 = (int)nodes[e.from].y; int x2 = (int)nodes[e.to].x; int y2 = (int)nodes[e.to].y; int len = (int)Math.abs(Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) - e.len); offgraphics.setColor((len < 10) ? arcColor1 : (len < 20 ? arcColor2 : arcColor3)) ; offgraphics.drawLine(x1, y1, x2, y2); if (stress) { String lbl = String.valueOf(len); offgraphics.setColor(stressColor); offgraphics.drawString(lbl, x1 + (x2-x1)/2, y1 + (y2-y1)/2); offgraphics.setColor(edgeColor); } } FontMetrics fm = offgraphics.getFontMetrics(); for (int i = 0 ; i < nnodes ; i++) { paintNode(offgraphics, nodes[i], fm); } g.drawImage(offscreen, 0, 0, null); } //1.1 event handling public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { addMouseMotionListener(this); double bestdist = Double.MAX_VALUE; int x = e.getX(); int y = e.getY(); for (int i = 0 ; i < nnodes ; i++) { Node n = nodes[i]; double dist = (n.x - x) * (n.x - x) + (n.y - y) * (n.y - y); if (dist < bestdist) { pick = n; bestdist = dist; } } pickfixed = pick.fixed; pick.fixed = true; pick.x = x; pick.y = y; repaint(); e.consume(); } public void mouseReleased(MouseEvent e) { removeMouseMotionListener(this); if (pick != null) { pick.x = e.getX(); pick.y = e.getY(); pick.fixed = pickfixed; pick = null; } repaint(); e.consume(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { pick.x = e.getX(); pick.y = e.getY(); repaint(); e.consume(); } public void mouseMoved(MouseEvent e) { } public void start() { relaxer = new Thread(this); relaxer.start(); } public void stop() { relaxer = null; }}public class Graph extends Applet implements ActionListener { int z; GraphPanel panel[]; TextArea text; Panel controlPanel,showPanel; Button tianjia = new Button("填加/删除"); Button shenqing = new Button("申请设备"); Button huishou = new Button("回收设备"); TextField cen,sq,hs; public void init() { text=new TextArea("CPU-内存,CPU-通道1,CPU-通道2,"+"通道1-控制器1/70,通道1-控制器2/70,通道2-控制器3/70,控制器3-鼠标/70,控制器3-键盘/70,控制器1-显示器/70,控制器2-打印机/70",7,70,text.SCROLLBARS_VERTICAL_ONLY); setLayout(new BorderLayout()); cen=new TextField("CPU"); sq=new TextField(7); hs=new TextField(7); panel = new GraphPanel[100]; for(z=0;z<100;z++) panel[z]=new GraphPanel(this); add("Center", panel[0]);//中部和南部面板 controlPanel = new Panel();//南部面板为控制面板 add("South", controlPanel); showPanel=new Panel(); add("North",showPanel); controlPanel.add(tianjia); tianjia.addActionListener(this); controlPanel.add(sq); controlPanel.add(shenqing); shenqing.addActionListener(this); controlPanel.add(hs); controlPanel.add(huishou); huishou.addActionListener(this); controlPanel.add(cen); showPanel.add(text); String edges = text.getText(); for (StringTokenizer t = new StringTokenizer(edges, ",") ; t.hasMoreTokens() ; ) { String str = t.nextToken(); int i = str.indexOf('-'); if (i > 0) { int len = 50; int j = str.indexOf('/'); if (j > 0) { len = Integer.valueOf(str.substring(j+1)).intValue();//从右端空格开始"/"后面的字符串,并取整 str = str.substring(0, j);//把str付为开始到"/"前面的字符串 } panel[0].addEdge(str.substring(0,i), str.substring(i+1), len);//在panel上给"-"前到"-"后的结点连边 } } Dimension d = getSize();//返回与该对象有同样大小的新事例(精确到整数) String center = cen.getText();//中心固定结点 if (center != null){ Node n = panel[0].nodes[panel[0].findNode(center)]; n.x = d.width / 2; n.y = d.height / 2; n.fixed = true; } } public void start() { panel[0].start(); } public void stop() { panel[0].stop(); } int r=0,change,bian;//r为面板块号;change:找到改变颜色结点的号;bian为寻找的边的数组号 String app,free;//app是申请字符串;free为回收字符串 //int ch1=0,ch2=0,co1=0,co2=0,co3=0; public void actionPerformed(ActionEvent e) { Object src = e.getSource();//建立事件触发对象 if(src==tianjia) { remove(panel[r]); r++; add("Center", panel[r]); String edges = text.getText(); for (StringTokenizer t = new StringTokenizer(edges, ",") ; t.hasMoreTokens() ; ) { String str = t.nextToken(); int i = str.indexOf('-'); if (i > 0) { int len = 50; int j = str.indexOf('/'); if (j > 0) { len = Integer.valueOf(str.substring(j+1)).intValue();//从右端空格开始"/"后面的字符串,并取整 str = str.substring(0, j);//把str付为开始到"/"前面的字符串 } panel[r].addEdge(str.substring(0,i), str.substring(i+1), len);//在panel上给"-"前到"-"后的结点连边 } } Dimension d = getSize();//返回与该对象有同样大小的新事例(精确到整数) String center = cen.getText();//中心固定结点 if (center != null){ Node n = panel[r].nodes[panel[r].findNode(center)]; n.x = d.width / 2; n.y = d.height / 2; n.fixed = true; } validate(); //repaint(); // System.out.println(text.getText()); panel[r].start(); // start(); // panel.repaint();*/ } else if(src==shenqing) { // System.out.println(r); app=sq.getText(); panel[r].nodes[panel[r].findNode(app)].fixed=true; String edges = text.getText(); for (StringTokenizer t = new StringTokenizer(edges, ",") ; t.hasMoreTokens() ; ) { String str = t.nextToken(); int i = str.indexOf('-'); if (i > 0) { int j = str.indexOf('/'); if (j > 0) { str = str.substring(0, j);//把str付为开始到"/"前面的字符串 } } String sss=str.substring(i+1); String mmm; // System.out.println(str); // System.out.println(sss); if(app.equals(sss)) { mmm=str.substring(0,i); // System.out.println(mmm); panel[r].nodes[panel[r].findNode(mmm)].fixed=true; panel[r].nodes[panel[r].findNode(mmm)].time++; app=mmm; // System.out.println(app); } } //System.out.println(app); edges = text.getText(); for (StringTokenizer t = new StringTokenizer(edges, ",") ; t.hasMoreTokens() ; ) { String str = t.nextToken(); int i = str.indexOf('-'); if (i > 0) { int j = str.indexOf('/'); if (j > 0) { str = str.substring(0, j);//把str付为开始到"/"前面的字符串 } } String sss=str.substring(i+1); String mmm; // System.out.println(str); // System.out.println(sss); if(app.equals(sss)) { mmm=str.substring(0,i); // System.out.println(mmm); panel[r].nodes[panel[r].findNode(mmm)].fixed=true; panel[r].nodes[panel[r].findNode(mmm)].time++; app=mmm; // System.out.println(app); } } //System.out.println(app); } else if(src==huishou) { // System.out.println(r); free=hs.getText(); panel[r].nodes[panel[r].findNode(free)].fixed=false; String edges = text.getText(); for (StringTokenizer t = new StringTokenizer(edges, ",") ; t.hasMoreTokens() ; ) { String str = t.nextToken(); int i = str.indexOf('-'); if (i > 0) { int j = str.indexOf('/'); if (j > 0) { str = str.substring(0, j);//把str付为开始到"/"前面的字符串 } } String sss=str.substring(i+1); String mmm; if(free.equals(sss)) { mmm=str.substring(0,i); if(panel[r].nodes[panel[r].findNode(mmm)].time==1) { panel[r].nodes[panel[r].findNode(mmm)].fixed=false; free=mmm; } else {} } } edges = text.getText(); for (StringTokenizer t = new StringTokenizer(edges, ",") ; t.hasMoreTokens() ; ) { String str = t.nextToken(); int i = str.indexOf('-'); if (i > 0) { int j = str.indexOf('/'); if (j > 0) { str = str.substring(0, j);//把str付为开始到"/"前面的字符串 } } String sss=str.substring(i+1); String mmm; if(free.equals(sss)) { mmm=str.substring(0,i); if(panel[r].nodes[panel[r].findNode(mmm)].time==1) { panel[r].nodes[panel[r].findNode(mmm)].fixed=false; free=mmm; } else {} } } } } }/**操作系统 作业Ⅲ-设备管理*//**作者: 辛华 20054451*//**2007年5月11日*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -