import java.applet.Applet; import java.awt.*; public class GrafRezidual extends Applet implements Runnable { Thread th; int n; int m; int snode; int tnode; int flag; Node v[]; Muchie e[]; public void set(int i, int j, int k, int l, int i1, Node anode[], Muchie amuchie[]) { flag = i; n = j; m = k; snode = l; tnode = i1; v = anode; e = amuchie; } public void paintNode(Graphics g, Node node, FontMetrics fontmetrics) { int i = node.x; int j = node.y; int k = fontmetrics.stringWidth(node.nume) + 10; int l = fontmetrics.getHeight() + 4; node.w = k; node.h = l; Color color; if(node.dist < 0) { color = Color.gray; } else { color = Color.blue; } g.setColor(color); g.drawRect(i - k / 2, j - l / 2, k, l); g.setColor(getBackground()); g.fillRect((i - k / 2) + 1, (j - l / 2) + 1, k - 1, l - 1); g.setColor(color); g.drawString(node.nume, i - (k - 10) / 2, (j - (l - 4) / 2) + fontmetrics.getAscent()); } int[] xy(int i, int j, int k, int l, int i1, int j1) { int ai[] = new int[2]; k -= (i < 0 ? -2 : 2) * i1 - 3; l -= (j < 0 ? -2 : 2) * j1 - 2; if(Math.abs(k * j) >= Math.abs(l * i)) { ai[0] = ((j < 0 ? -1 : 1) * i * l) / j / 2; ai[1] = ((j < 0 ? -1 : 1) * l) / 2; } else { ai[0] = ((i < 0 ? -1 : 1) * k) / 2; ai[1] = ((i < 0 ? -1 : 1) * j * k) / i / 2; } ai[0] += i1; ai[1] += j1; return ai; } void drawArrow(Graphics g, int i, int j, int k, int l) { int i1 = i - k; int j1 = j - l; double d = Math.sqrt(i1 * i1 + j1 * j1) / 16D; double d1 = (double)j1 / d; d = (double)i1 / d; g.drawLine(k, l, k + (int)((d * 12D - d1 * 5D) / 13D), l + (int)((d * 5D + d1 * 12D) / 13D)); g.drawLine(i, j, k, l); } void drawMuchie(Graphics g, FontMetrics fontmetrics, int i, int j, int k, int l, int i1) { int j1 = i - k; int k1 = j - l; drawArrow(g, i, j, k, l); int l1 = fontmetrics.stringWidth("" + i1); int i2 = fontmetrics.getHeight(); int j2 = (i + k) / 2; int k2 = (j + l) / 2; if(k1 < 0 || k1 == 0 && j1 < 0) { j2 += fontmetrics.stringWidth(" "); } else { j2 -= l1 + fontmetrics.stringWidth(" "); } if(j1 > 0 || j1 == 0 && k1 < 0) { k2++; } else { k2 -= i2; } g.setColor(getBackground()); g.fillRect(j2, k2, l1, i2); if(i1 != 0) { g.setColor(Color.black); g.drawString("" + i1, j2, k2 + fontmetrics.getAscent()); } } public void paintMuchie(Graphics g, Muchie muchie, FontMetrics fontmetrics) { Node node = v[muchie.nod_plus]; Node node1 = v[muchie.nod_minus]; Color color; if(muchie.st > 0 && node.dist < node1.dist) { color = Color.red; } else if(muchie.st == 0 && node.dist < node1.dist) { color = Color.blue; } else { color = Color.gray; } int i = node.x - node1.x; int j = node.y - node1.y; double d = Math.sqrt(i * i + j * j) / 2D; double d1 = (double)(-j) / d; d = (double)i / d; int ai[] = xy(-i, -j, node.w, node.h, (int)d1, (int)d); int ai1[] = xy(i, j, node1.w, node1.h, (int)d1, (int)d); if(muchie.capacitate - muchie.flux == 0) { g.setColor(getBackground()); } else { g.setColor(color); } drawMuchie(g, fontmetrics, node.x + ai[0], node.y + ai[1], node1.x + ai1[0], node1.y + ai1[1], muchie.capacitate - muchie.flux); if(muchie.st > 0 && node.dist > node1.dist) { color = Color.red; } else if(muchie.st == 0 && node.dist > node1.dist) { color = Color.blue; } else { color = Color.gray; } ai = xy(i, j, node1.w, node1.h, -(int)d1, -(int)d); ai1 = xy(-i, -j, node.w, node.h, -(int)d1, -(int)d); if(muchie.flux == 0) { g.setColor(getBackground()); } else { g.setColor(color); } drawMuchie(g, fontmetrics, node1.x + ai[0], node1.y + ai[1], node.x + ai1[0], node.y + ai1[1], muchie.flux); } public void paint(Graphics g) { FontMetrics fontmetrics = g.getFontMetrics(); for(int i = 0; i < n; i++) { paintNode(g, v[i], fontmetrics); } for(int j = 0; j < m; j++) { paintMuchie(g, e[j], fontmetrics); } } public void update(Graphics g) { paint(g); } public void start() { if(th == null) { th = new Thread(this); th.start(); } } public void run() { do { do { try { Thread.sleep(100L); } catch(InterruptedException _ex) { } } while(flag-- <= 0); repaint(); } while(true); } public GrafRezidual() { flag = -1; } }