Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-19-2008, 04:21 PM
Member
 
Join Date: May 2008
Posts: 1
sfaiz is on a distinguished road
Nodes and edges..making visible and invisible based on distance
hi all, I've got this java applet code which is a graph containing edges and nodes. I just to implement a scheme so that everytime we click on a node, only the nearest nodes are visible, the rest is invisible, i.e. nodes that are more than 1 edge away should be invisible and nodes that are within 1 edge should be visible. any help on this.

this is the code for applet:
Code:
import java.util.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; 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, MouseListener, MouseMotionListener { Graph graph; int nnodes; Node nodes[] = new Node[100]; int nedges; Edge edges[] = new Edge[200]; Thread relaxer; 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(); while (relaxer == me) { relax(); //if (random && (Math.random() < 0.03)) { if (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; } } 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 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); } 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, ItemListener { GraphPanel panel; Panel controlPanel; public void init() { setLayout(new BorderLayout()); panel = new GraphPanel(this); add("Center", panel); controlPanel = new Panel(); 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 = getSize(); 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 destroy() { remove(panel); remove(controlPanel); } public void start() { panel.start(); } public void stop() { panel.stop(); } public void actionPerformed(ActionEvent e) { Object src = e.getSource(); } public void itemStateChanged(ItemEvent e) { Object src = e.getSource(); boolean on = e.getStateChange() == ItemEvent.SELECTED; } public String[][] getParameterInfo() { String[][] info = { {"edges", "delimited string", "A comma-delimited list of all the edges. It takes the form of 'C-N1,C-N2,C-N3,C-NX,N1-N2/M12,N2-N3/M23,N3-NX/M3X,...' where C is the name of center node (see 'center' parameter) and NX is a node attached to the center node. For the edges connecting nodes to eachother (and not to the center node) you may (optionally) specify a length MXY separated from the edge name by a forward slash."}, {"center", "string", "The name of the center node."} }; return info; } }
and here is the code for the html page

HTML Code:
<html> <head> <title>Assignment 2</title> </head> <body> <applet code="Graph.class" width=1000 height=1000> <param name=edges value="Groceries-Frozen Food/90,Groceries-Fresh Food/90,Groceries-Beverages/90,Groceries-Home Health/90,Groceries-Pet Food/90,Home Health-Medicine/90,Home Health-Laundry/90,Home Health-Garbage/90,Home Health-Bath/90,Home Health-Groceries/90,Medicine-Panadol(Pack 24)/90,Medicine-Panadol(Bottle 50)/90,Medicine-Home Health/90,Bath-Bath Soap/90,Garbage-Garbage Bags(Small)/90,Garbage-Garbage Bags(Large)/90,Laundry-Washing Powder/90,Laundry-Laundry Bleach/90,Pet Food-Dog Food/90,Pet Food-Bird Food/90,Pet Food-Cat Food/90,Pet Food-Fish Food/90,Dog Food-Dry Dog Food(1 KG)/90,Dog Food-Dry Dog Food(5 KG)/90,Beverages-Tea/90,Tea-Earl Grey Tea Bags(Pack 25)/90,Tea-Earl Grey Tea Bags(Pack 100)/90,Tea-Earl Grey Tea Bags(Pack 200)/90,Beverages-Coffee/90,Coffee-Instant Coffee(200 gram)/90,Coffee-Instant Coffee(500 gram)/90,Beverages-Chocolate Bar(500 gram)/90,Fresh Food-Cheese/90,Fresh Food-Meat/90,Fresh Food-Fruit/90,Cheese-Cheddar Cheese(500 gram)/90,Cheese-Cheddar Cheese(1000 gram)/90,Meat-T'Bone Steak/90,Fruit-Naval Oranges/90,Fruit-Bananas/90,Fruit-Peaches/90,Fruit-Grapes/90,Fruit-Apples/90,Frozen Food-Sea Food/90,Frozen Food-Milk Products/90,Frozen Food-Other/90,Sea Food-Fish Fingers(Large)/90,Sea Food-Fish Fingers(Small)/90,Sea Food-Shelled Prawns/90,Milk Products-Tub Ice Cream(1 Litre)/90,Milk Products-Tub Ice Cream(2 Litre)/90,Other-Hamburger Patties/90"> <param name=center value="Groceries"> Your browser probably doesn't understand the <OBJECT> tag so it isn't running the applet or perhaps you need a Java Plugin Your browser is completely ignoring the <OBJECT> tag! </applet> <hr> </body> </html>
just wanted to know how can a node be made invisible based on the fact that nodes within 1 edge(lines) are only going to be visible and nodes that are going to be more than 1 edge away are invisible?? i just need a head start on this...any help on this???
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
i need an example of JSR179 ((Location based Ser)implementation for CDC based device talk_to_vivekmishra CDC and Personal Profile 1 06-22-2008 10:45 PM
nodes in java ahsan New To Java 0 12-26-2007 04:09 PM
How to make the JTextArea background invisible bradder AWT / Swing 1 12-05-2007 07:30 PM
difference between code based security and role based security boy22 New To Java 1 07-24-2007 12:59 AM
Rectangle with rounded edges?? orchid Java 2D 1 05-10-2007 03:31 AM


All times are GMT +3. The time now is 12:49 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org