Thread: [Pfc-prolog-cvs] prolix/src/org/asturlinux/frade/prolix/web/servlets SvgGenerator.java,1.1,1.2
Status: Beta
Brought to you by:
ivanfrade
From: <iva...@us...> - 2003-08-01 18:07:45
|
Update of /cvsroot/pfc-prolog/prolix/src/org/asturlinux/frade/prolix/web/servlets In directory sc8-pr-cvs1:/tmp/cvs-serv3193/src/org/asturlinux/frade/prolix/web/servlets Modified Files: SvgGenerator.java Log Message: Svg adapt rectangle size to contained text Index: SvgGenerator.java =================================================================== RCS file: /cvsroot/pfc-prolog/prolix/src/org/asturlinux/frade/prolix/web/servlets/SvgGenerator.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SvgGenerator.java 30 Jul 2003 20:12:49 -0000 1.1 --- SvgGenerator.java 1 Aug 2003 18:07:41 -0000 1.2 *************** *** 4,9 **** import java.io.Writer; import java.io.IOException; - //import java.io.FileWriter; - //import java.io.FileReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; --- 4,7 ---- *************** *** 22,26 **** public class SvgGenerator { - /*************************** * Search methods --- 20,23 ---- *************** *** 92,99 **** public static final int marginX = 10; ! public static final int marginY = 5; public static final int nodeSizeX = 60; public static final int nodeSizeY = 40; private Dimension drawNode(Node raiz, String number, Point leftTopCorner, --- 89,98 ---- public static final int marginX = 10; ! public static final int marginY = 15; public static final int nodeSizeX = 60; public static final int nodeSizeY = 40; + public int currentNodeSizeX = nodeSizeX; + private Dimension drawNode(Node raiz, String number, Point leftTopCorner, *************** *** 106,117 **** Dimension boundingBox = new Dimension(0,0); ! int childYBaseLine = leftTopCorner.y+marginY+nodeSizeY; - //Vector childPoints = new Vector(); Vector childBoxes = new Vector(); for (int i = 0; i < result2.length ; i++) { String destiny = result2[i].getAttributes().getNamedItem("destiny").getNodeValue(); ! Point childPoint = new Point(leftTopCorner.x+ boundingBox.width, childYBaseLine); Dimension currentChildBoundingBox = drawNode(raiz,destiny,childPoint,output); --- 105,116 ---- Dimension boundingBox = new Dimension(0,0); ! int childYBaseLine = leftTopCorner.y + marginY + nodeSizeY; Vector childBoxes = new Vector(); for (int i = 0; i < result2.length ; i++) { String destiny = result2[i].getAttributes().getNamedItem("destiny").getNodeValue(); ! Point childPoint = new Point(leftTopCorner.x + boundingBox.width, ! childYBaseLine); Dimension currentChildBoundingBox = drawNode(raiz,destiny,childPoint,output); *************** *** 123,131 **** boundingBox.width = Math.max(boundingBox.width, ! nodeSizeX); boundingBox.height += nodeSizeY + marginY; System.out.println("Aqui pinto la cajita"); ! Point nodePoint = new Point(boundingBox.width / 2 + leftTopCorner.x , leftTopCorner.y ); --- 122,131 ---- boundingBox.width = Math.max(boundingBox.width, ! Math.max(nodeSizeX, currentNodeSizeX)); boundingBox.height += nodeSizeY + marginY; System.out.println("Aqui pinto la cajita"); ! Point nodePoint = new Point(boundingBox.width / 2 ! + leftTopCorner.x , leftTopCorner.y ); *************** *** 145,151 **** try { ! output.write("<path d=\"M " + (origen.x + nodeSizeX/2) + " " + (origen.y+nodeSizeY) ! + " L " + (destino.origin.x + destino.size.width/2 + nodeSizeX/2) + " " + destino.origin.y + "\" \n"); output.write("id=\"" + origen.x*origen.y + "\"\n"); --- 145,153 ---- try { ! output.write("<path d=\"M " ! + origen.x // + currentNodeSizeX / 2 + " " + (origen.y+nodeSizeY) ! + " L " ! + (destino.origin.x + destino.size.width/2) + " " + destino.origin.y + "\" \n"); output.write("id=\"" + origen.x*origen.y + "\"\n"); *************** *** 169,194 **** try { output.write("<rect \n " ! + "width=\"" + nodeSizeX + "\" \n" ! + "height=\"" + nodeSizeY + "\" \n" ! + "x=\"" + leftTopPoint.x + "\" \n" ! + "y=\"" + leftTopPoint.y + "\" \n" ! + "id=\""+node.getAttributes().getNamedItem("number").getNodeValue()+"\" \n" ! + "style=\"fill:#00ca00;fill-opacity:1;\" " ! + "/> \n"); - Node label = node.getAttributes().getNamedItem("label"); if (label != null) output.write("<text \n" ! + "x=\"" + (leftTopPoint.x + nodeSizeX/10) + "\" \n" ! + "y=\"" + (leftTopPoint.y + nodeSizeY/2 ) + "\" \n" ! + "id=\"text" + node.getAttributes().getNamedItem("number").getNodeValue() + "\">\n" ! + "<tspan id=\"" + node.getAttributes().getNamedItem("number").getNodeValue() ! + "\" >\n" + label.getNodeValue() ! + "</tspan>" + "</text>"); --- 171,205 ---- try { + Node label = node.getAttributes().getNamedItem("label"); + String text; + if (label != null) + { + text = label.getNodeValue(); + currentNodeSizeX = Math.max(text.length() * 7, nodeSizeX); + } + + output.write("<rect \n " ! + " width=\"" + currentNodeSizeX + "\" \n" ! + " height=\"" + nodeSizeY + "\" \n" ! + " x=\"" + (leftTopPoint.x - (currentNodeSizeX / 2)) + "\" \n" ! + " y=\"" + leftTopPoint.y + "\" \n" ! + " id=\""+node.getAttributes().getNamedItem("number").getNodeValue()+"\" \n" ! + " style=\"fill:#00ca00;fill-opacity:1;\" " ! + " /> \n"); ! if (label != null) output.write("<text \n" ! + " x=\"" + (leftTopPoint.x - currentNodeSizeX/2.5) + "\" \n" ! + " y=\"" + (leftTopPoint.y + nodeSizeY/2 ) + "\" \n" ! + " id=\"text" + node.getAttributes().getNamedItem("number").getNodeValue() + "\">\n" ! + " <tspan id=\"" + node.getAttributes().getNamedItem("number").getNodeValue() ! + "\" >\n " + label.getNodeValue() ! + "\n </tspan>\n" + "</text>"); *************** *** 203,209 **** ***/ ! private void initWriter(Writer w) throws IOException { w.write("<?xml version=\"1.0\" " + "encoding=\"UTF-8\" standalone=\"no\"?>\n" --- 214,221 ---- ***/ ! private String svgInit(int height, int width) throws IOException { + StringWriter w = new StringWriter(); w.write("<?xml version=\"1.0\" " + "encoding=\"UTF-8\" standalone=\"no\"?>\n" *************** *** 213,223 **** w.write("<svg xmlns=\"http://www.w3.org/2000/svg\"\n " + " xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n " ! + " version=\"1\"\n " + " x=\"30\" y=\"30\">\n "); } ! private void finishWriter(Writer w) throws IOException { ! w.write("\n</svg>\n"); } --- 225,238 ---- w.write("<svg xmlns=\"http://www.w3.org/2000/svg\"\n " + " xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n " ! + " version=\"1\"\n " ! + " x=\"30\" y=\"30\"\n " ! + " height=\"" + height + "\" width=\"" + width + "\"> \n"); ! return w.toString(); } ! private String svgEnd() throws IOException { ! return new String("\n</svg>\n"); } *************** *** 238,242 **** // Works with generic Writer. StringWriter svg = new StringWriter(); - initWriter(svg); Dimension total = drawNode(document,"1",new Point(30,30),svg); --- 253,256 ---- *************** *** 244,251 **** + total.height); ! finishWriter(svg); ! svg.close(); System.out.println("Pues no paso nada malo"); ! return svg.toString(); } catch (FactoryConfigurationError e) { --- 258,264 ---- + total.height); ! String result = svgInit(total.height, total.width).concat(svg.toString()).concat(svgEnd()); System.out.println("Pues no paso nada malo"); ! return result; } catch (FactoryConfigurationError e) { |