package structure; import java.util.*; import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class Node { public double x; // center coords public double y; public double w; // width and height public double h; String lbl; // name String []text; // description Color nclr; Color bclr; Color tclr; int shape; // determins shape of a node Bubble hint; //hint that works on mouse over boolean firstTime = true; Graph innerGraph; public Node(){ hint = new Bubble(); } public boolean contains(Point p){ int xval = (int)(x-w/2); int yval = (int)(y-h/2); Rectangle box = new Rectangle(xval,yval,(int)w,(int)h); return (box.contains(p)); } public Bubble addBubbleAttr(String[]s, Color c){ hint.text = s; hint.setColor(c); return hint; } public void setColor(Color nc, Color bc, Color tc){ nclr=nc; bclr=bc; tclr=tc; } //draws node on a screen public void paintComponent (Graphics g){ Graphics2D g2 = (Graphics2D) g; Font f = g2.getFont(); FontMetrics fm = g2.getFontMetrics(); //Toolkit.getDefaultToolkit().getFontMetrics(f); BasicStroke stroke = new BasicStroke(2.0f); int ascent = fm.getAscent(); int height = fm.getHeight(); int x1=0,y1=0; if(firstTime){ int maxwidth=0,maxheight=0; for (int j = 0; j < text.length; j ++) { int width = fm.stringWidth(text[j]); if(maxwidth < width) maxwidth = width; maxheight = (int)(maxheight + 1.2*height); } maxheight=maxheight + height; maxwidth= maxwidth+20; w = maxwidth; h = maxheight; firstTime = false; } int xval = (int)(x-w/2); int yval = (int)(y-h/2); //draw rectangle //GradientPaint clrtogray = new GradientPaint(xval,yval,clr,xval,yval+(int)h,Color.gray); g2.setPaint(nclr); switch (shape) { case 1: g2.fill(new Rectangle2D.Double(xval,yval,(int)w,(int)h)); break; case 2: g2.fill(new Ellipse2D.Double(xval,yval,(int)w,(int)h)); break; } //draw border g2.setStroke(stroke); g2.setPaint(bclr); switch (shape) { case 1: g2.draw(new Rectangle2D.Double(xval,yval,(int)w,(int)h)); break; case 2: g2.draw(new Ellipse2D.Double(xval,yval,(int)w,(int)h)); break; } //draw text g2.setPaint(tclr); for (int j = 0; j < text.length; j ++) { x1 = xval + ((int)w - fm.stringWidth(text[j]))/2; y1 = (int)(yval + 1.2*(j+1)*height); g.drawString(text[j], x1, y1); } } public Graph getGraph(){ return innerGraph; } public void addGraph(Graph gr){ innerGraph = gr; } public void drawBubble(Graphics g){ //draw bubble if(hint.text!=null) hint.paintComponent(g,(int)(x+w/2+5),(int)(y-h/2-5)); } public void drawDocument(){} public void drawTriangle(){} public void drawCircle(){} }