advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Graph





import java.util.*;

import java.awt.*;

import java.applet.Applet;



class Node {

    double x;

    double y;



    double dx;

    double dy;



    boolean fixed;



    String lbl;

}



class Edge {

    int from;

    int to;



    double len;

}



class GraphPanel extends Panel implements Runnable {

    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;

    }



    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() {

	while (true) {

	    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");

	    }

	    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);

	    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 = size();

	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));

		//System.out.println("v= " + n.dx + "," + 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.gray;

    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 = size();

	if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {

	    offscreen = createImage(d.width, d.height);

	    offscreensize = d;

	    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);

    }



    public synchronized boolean mouseDown(Event evt, int x, int y) {

	double bestdist = Double.MAX_VALUE;

	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();

	return true;

    }



    public synchronized boolean mouseDrag(Event evt, int x, int y) {

	pick.x = x;

	pick.y = y;

	repaint();

	return true;

    }



    public synchronized boolean mouseUp(Event evt, int x, int y) {

	pick.x = x;

	pick.y = y;

	pick.fixed = pickfixed;

	pick = null;

	

	repaint();

	return true;

    }



    public void start() {

	relaxer = new Thread(this);

	relaxer.start();

    }

    public void stop() {

	relaxer.stop();

    }

}



public class Graph extends Applet {

    GraphPanel panel;



    public void init() {

	setLayout(new BorderLayout());



	panel = new GraphPanel(this);

	add("Center", panel);

	Panel p = new Panel();

	add("South", p);

	p.add(new Button("Scramble"));

	p.add(new Button("Shake"));

	p.add(new Checkbox("Stress"));

	p.add(new Checkbox("Random"));



	String edges = getParameter("edges");

	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);

		}

		panel.addEdge(str.substring(0,i), str.substring(i+1), len);

	    }

	}

	Dimension d = size();

	String center = getParameter("center");

	if (center != null){

	    Node n = panel.nodes[panel.findNode(center)];

	    n.x = d.width / 2;

	    n.y = d.height / 2;

	    n.fixed = true;

	}

    }



    public void start() {

	panel.start();

    }

    public void stop() {

	panel.stop();

    }

    public boolean action(Event evt, Object arg) {

	if (arg instanceof Boolean) {

	    if (((Checkbox)evt.target).getLabel().equals("Stress")) {

		panel.stress = ((Boolean)arg).booleanValue();

	    } else {

		panel.random = ((Boolean)arg).booleanValue();

	    }

	    return true;

	} 

	if ("Scramble".equals(arg)) {

	    play(getCodeBase(), "audio/computer.au");

	    Dimension d = size();

	    for (int i = 0 ; i < panel.nnodes ; i++) {

		Node n = panel.nodes[i];

		if (!n.fixed) {

		    n.x = 10 + (d.width-20)*Math.random();

		    n.y = 10 + (d.height-20)*Math.random();

		}

	    }

	    return true;

	}

	if ("Shake".equals(arg)) {

	    play(getCodeBase(), "audio/gong.au");

	    Dimension d = size();

	    for (int i = 0 ; i < panel.nnodes ; i++) {

		Node n = panel.nodes[i];

		if (!n.fixed) {

		    n.x += 80*Math.random() - 40;

		    n.y += 80*Math.random() - 40;

		}

	    }

	    return true;

	}

	return false;

    }

}


Back to Graph applet page

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 DevX Skillbuilding from IBM developerWorks
 Avaya DevConnect Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 48
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

SugarCRM's Latest is 'SaaS in a Box'
Apple Details iPhone-Mac Developer Event
RIM Ups Ante With Mobile Software Push
Novell Readies Silverlight Clone for Linux
Yahoo Pitches The 'Next Generation of Search'
Alfresco's Latest ECM: Prying Open a Sector?
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads for the JON 2.0

Keeping Up with the Joneses: Windows Mobile 6.1's New Upgrades
Nine Silverlight 2 Features Not to Be Missed
How Does Microsoft Use Team Foundation Server?
OpenSocial: The Power of Social Networks in Your Applications
Managing the Modern Network
Virtual Earth?What's New in the Latest Release
Everything You Need to Know About Your iPhone
PerformancePoint 2007: Installing Planning Server
Create Secure Java Applications Productively, Part 1: Use Rational Application Developer and Data Studio
.NET Building Blocks: Custom User Control Fundamentals

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES