Examples of generating SVG via com-hfg objects.

The SVG classes in the com-hfg library are designed to let you construct or parse SVG content in a modular, object-oriented way.

  1. W3C Polygon Example in SVG & PNG
  2. W3C Elliptical Arc Example #1 in SVG & PNG
  3. W3C Elliptical Arc Example #2 in SVG & PNG
  4. Glowing Hello World

Example 1: W3C Polygon Example in SVG & PNG

Replicates the first arc example from the W3C SVG docs.
  1. //--------------------------------------------------------------------------
  2. @Test
  3. public void testDrawPolygon()
  4. throws Exception
  5. {
  6. SVG svg = new SVG().setViewBox(new Rectangle(0, 0, 1200, 400));
  7.  
  8. // Border
  9. svg.addRect(new Rectangle(1, 1, 1198, 398))
  10. .setFill(null)
  11. .setStroke(Color.BLUE)
  12. .setStrokeWidth(2);
  13.  
  14. // Star
  15. svg.addPolygon()
  16. .setPoints("350,75 379,161 469,161 397,215 423,301 350,250 277,301 303,215 231,161 321,161")
  17. .setFill(Color.RED)
  18. .setStroke(Color.BLUE)
  19. .setStrokeWidth(10);
  20.  
  21. // Hexagon
  22. svg.addPolygon()
  23. .setPoints("850,75 958,137.5 958,262.5 850,325 742,262.6 742,137.5")
  24. .setFill(HTMLColor.LIME_GREEN)
  25. .setStroke(Color.BLUE)
  26. .setStrokeWidth(10);
  27.  
  28. SvgDoc doc = new SvgDoc(svg);
  29.  
  30. // Write out the SVG
  31. doc.toIndentedSVG(System.out, 0, 3);
  32.  
  33. // Generate a PNG image via the Java2D support built into the SVG objects
  34. BufferedImage bufferedImage = new BufferedImage(svg.getWidth(), svg.getHeight(), BufferedImage.TYPE_INT_RGB);
  35. Graphics2D g2 = (Graphics2D) bufferedImage.getGraphics();
  36.  
  37. RenderingHints hints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  38. hints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  39. g2.setRenderingHints(hints);
  40.  
  41. svg.draw(g2);
  42.  
  43. File pngFile = new File("polygon.png");
  44. ImageIO.write(bufferedImage, "png", new FileOutputStream(pngFile));
  45. }
would produce the following SVG:
  1. <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
  2. <svg height='399' viewBox='0 0 1200 400' width='1199' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns='http://www.w3.org/2000/svg'>
  3. <rect fill='none' height='398' stroke='#0000ff' stroke-width='2' width='1198' x='1' y='1' />
  4. <polygon fill='#ff0000' points='350,75 379,161 469,161 397,215 423,301 350,250 277,301 303,215 231,161 321,161' stroke='#0000ff' stroke-width='10' />
  5. <polygon fill='#32cd32' points='850,75 958,137.5 958,262.5 850,325 742,262.6 742,137.5' stroke='#0000ff' stroke-width='10' />
  6. </svg>
and this PNG image:

Example 2: W3C Elliptical Arc Example #1 in SVG & PNG

Replicates the first arc example from the W3C SVG docs.
  1. //--------------------------------------------------------------------------
  2. @Test
  3. public void testDrawArc2()
  4. throws Exception
  5. {
  6. SVG svg = new SVG().setViewBox(new Rectangle(0, 0, 1200, 400));
  7.  
  8. // Border
  9. svg.addRect(new Rectangle(1, 1, 1198, 398))
  10. .setFill(null)
  11. .setStroke(Color.BLUE)
  12. .setStrokeWidth(1);
  13.  
  14. // Pie
  15. svg.addPath("M300,200 h-150 a150,150 0 1,0 150,-150 z")
  16. .setFill(Color.RED)
  17. .setStroke(Color.BLUE)
  18. .setStrokeWidth(5);
  19.  
  20. // Wedge
  21. svg.addPath("M275,175 v-150 a150,150 0 0,0 -150,150 z")
  22. .setFill(Color.YELLOW)
  23. .setStroke(Color.BLUE)
  24. .setStrokeWidth(5);
  25.  
  26. // Path
  27. svg.addPath("M600,350 l 50,-25 a25,25 -30 0,1 50,-25 l 50,-25 a25,50 -30 0,1 50,-25 l 50,-25 a25,75 -30 0,1 50,-25 l 50,-25 a25,100 -30 0,1 50,-25 l 50,-25")
  28. .setFill(null)
  29. .setStroke(Color.RED)
  30. .setStrokeWidth(5);
  31.  
  32. SvgDoc doc = new SvgDoc(svg);
  33.  
  34. // Write out the SVG
  35. doc.toIndentedSVG(System.out, 0, 3);
  36.  
  37. // Generate a PNG image via the Java2D support built into the SVG objects
  38. BufferedImage bufferedImage = new BufferedImage(svg.getWidth(), svg.getHeight(), BufferedImage.TYPE_INT_RGB);
  39. Graphics2D g2 = (Graphics2D) bufferedImage.getGraphics();
  40.  
  41. RenderingHints hints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  42. hints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  43. g2.setRenderingHints(hints);
  44.  
  45. svg.draw(g2);
  46.  
  47. File pngFile = new File("arc1.png");
  48. ImageIO.write(bufferedImage, "png", new FileOutputStream(pngFile));
  49. }
would produce the following SVG:
  1. <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
  2. <svg height='399' viewBox='0 0 1200 400' width='1199' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns='http://www.w3.org/2000/svg'>
  3. <rect fill='none' height='398' stroke='#0000ff' stroke-width='1' width='1198' x='1' y='1' />
  4. <path d='M300,200 h-150 a150,150 0 1,0 150,-150 z' fill='#ff0000' stroke='#0000ff' stroke-width='5' />
  5. <path d='M275,175 v-150 a150,150 0 0,0 -150,150 z' fill='#ffff00' stroke='#0000ff' stroke-width='5' />
  6. <path d='M600,350 l 50,-25 a25,25 -30 0,1 50,-25 l 50,-25 a25,50 -30 0,1 50,-25 l 50,-25 a25,75 -30 0,1 50,-25 l 50,-25 a25,100 -30 0,1 50,-25 l 50,-25' fill='none' stroke='#ff0000' stroke-width='5' />
  7. </svg>
and this PNG image:

Example 3: W3C Elliptical Arc Example #2 in SVG & PNG

Replicates the second arc example from the W3C SVG docs.
  1. //--------------------------------------------------------------------------
  2. @Test
  3. public void testDrawArc2()
  4. throws Exception
  5. {
  6. SVG svg = new SVG().setViewBox(new Rectangle(0, 0, 1200, 525));
  7. SvgGroup g = svg.addGroup().setFontFamily("Verdana");
  8.  
  9. SvgDefs defs = g.addDefs();
  10. SvgGroup baseEllipses = defs.addGroup().setId("baseEllipses").setFontSize(new Pixels(20));
  11. baseEllipses.addEllipse().setCx(125).setCy(125).setRx(100).setRy(50).setFill(null).setStroke(HTMLColor.valueOf("#888888")).setStrokeWidth(2);
  12. baseEllipses.addEllipse().setCx(225).setCy(75).setRx(100).setRy(50).setFill(null).setStroke(HTMLColor.valueOf("#888888")).setStrokeWidth(2);
  13. baseEllipses.addText("Arc start").setX(35).setY(70);
  14. baseEllipses.addText("Arc end").setX(225).setY(145);
  15.  
  16. // Border
  17. g.addRect(new Rectangle(1, 1, 1198, 523)).setFill(null).setStroke(Color.BLUE).setStrokeWidth(1);
  18.  
  19. SvgGroup fontSizeGrp = g.addGroup().setFontSize("30");
  20.  
  21. SvgGroup legendGrp = fontSizeGrp.addGroup().setTransform("translate(0,0)");
  22. legendGrp.addUse("#baseEllipses");
  23.  
  24. SvgGroup grp1 = fontSizeGrp.addGroup().setTransform("translate(400,0)");
  25. grp1.addText("large-arc-flag=0").setX(50).setY(210);
  26. grp1.addText("sweep-flag=0").setX(50).setY(250);
  27. grp1.addUse("#baseEllipses");
  28. grp1.addPath("M 125,75 a100,50 0 0,0 100,50").setFill(null).setStroke(Color.RED).setStrokeWidth(6);
  29.  
  30. SvgGroup grp2 = fontSizeGrp.addGroup().setTransform("translate(800,0)");
  31. grp2.addText("large-arc-flag=0").setX(50).setY(210);
  32. grp2.addText("sweep-flag=1").setX(50).setY(250);
  33. grp2.addUse("#baseEllipses");
  34. grp2.addPath("M 125,75 a100,50 0 0,1 100,50").setFill(null).setStroke(Color.RED).setStrokeWidth(6);
  35.  
  36. SvgGroup grp3 = fontSizeGrp.addGroup().setTransform("translate(400,250)");
  37. grp3.addText("large-arc-flag=1").setX(50).setY(210);
  38. grp3.addText("sweep-flag=0").setX(50).setY(250);
  39. grp3.addUse("#baseEllipses");
  40. grp3.addPath("M 125,75 a100,50 0 1,0 100,50").setFill(null).setStroke(Color.RED).setStrokeWidth(6);
  41.  
  42. SvgGroup grp4 = fontSizeGrp.addGroup().setTransform("translate(800,250)");
  43. grp4.addText("large-arc-flag=1").setX(50).setY(210);
  44. grp4.addText("sweep-flag=1").setX(50).setY(250);
  45. grp4.addUse("#baseEllipses");
  46. grp4.addPath("M 125,75 a100,50 0 1,1 100,50").setFill(null).setStroke(Color.RED).setStrokeWidth(6);
  47.  
  48. SvgDoc doc = new SvgDoc(svg);
  49.  
  50. // Write out the SVG
  51. doc.toIndentedSVG(System.out, 0, 3);
  52.  
  53. // Generate a PNG image via the Java2D support built into the SVG objects
  54. BufferedImage bufferedImage = new BufferedImage(svg.getWidth(), svg.getHeight(), BufferedImage.TYPE_INT_RGB);
  55. Graphics2D g2 = (Graphics2D) bufferedImage.getGraphics();
  56.  
  57. RenderingHints hints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  58. hints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  59. g2.setRenderingHints(hints);
  60.  
  61. svg.draw(g2);
  62.  
  63. File pngFile = new File("arc2.png");
  64. ImageIO.write(bufferedImage, "png", new FileOutputStream(pngFile));
  65. }
would produce the following SVG:
  1. <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
  2. <svg height='524' viewBox='0 0 1200 525' width='1199' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns='http://www.w3.org/2000/svg'>
  3. <g style='font-family:Verdana'>
  4. <defs>
  5. <g font-size='20.0px' id='baseEllipses'>
  6. <ellipse cx='125' cy='125' fill='none' rx='100' ry='50' stroke='#888888' stroke-width='2' />
  7. <ellipse cx='225' cy='75' fill='none' rx='100' ry='50' stroke='#888888' stroke-width='2' />
  8. <text x='35' y='70'>Arc start</text>
  9. <text x='225' y='145'>Arc end</text>
  10. </g>
  11. </defs>
  12. <rect fill='none' height='523' stroke='#0000ff' stroke-width='1' width='1198' x='1' y='1' />
  13. <g font-size='30'>
  14. <g transform='translate(0,0)'>
  15. <use xlink:href='#baseEllipses' />
  16. </g>
  17. <g transform='translate(400,0)'>
  18. <text x='50' y='210'>large-arc-flag=0</text>
  19. <text x='50' y='250'>sweep-flag=0</text>
  20. <use xlink:href='#baseEllipses' />
  21. <path d='M 125,75 a100,50 0 0,0 100,50' stroke='#ff0000' stroke-width='6' />
  22. </g>
  23. <g transform='translate(800,0)'>
  24. <text x='50' y='210'>large-arc-flag=0</text>
  25. <text x='50' y='250'>sweep-flag=1</text>
  26. <use xlink:href='#baseEllipses' />
  27. <path d='M 125,75 a100,50 0 0,1 100,50' stroke='#ff0000' stroke-width='6' />
  28. </g>
  29. <g transform='translate(400,250)'>
  30. <text x='50' y='210'>large-arc-flag=1</text>
  31. <text x='50' y='250'>sweep-flag=0</text>
  32. <use xlink:href='#baseEllipses' />
  33. <path d='M 125,75 a100,50 0 1,0 100,50' stroke='#ff0000' stroke-width='6' />
  34. </g>
  35. <g transform='translate(800,250)'>
  36. <text x='50' y='210'>large-arc-flag=1</text>
  37. <text x='50' y='250'>sweep-flag=1</text>
  38. <use xlink:href='#baseEllipses' />
  39. <path d='M 125,75 a100,50 0 1,1 100,50' stroke='#ff0000' stroke-width='6' />
  40. </g>
  41. </g>
  42. </g>
  43. </svg>
and this PNG image:

Example 4: Glowing Hello World

  1. //--------------------------------------------------------------------------
  2. @Test
  3. public void testGlow()
  4. {
  5. SVG svg = new SVG();
  6.  
  7. SvgDefs defs = svg.addDefs();
  8. defs.addSubtag(createGlowFilter());
  9.  
  10. SvgGroup group = svg.addGroup()
  11. .setFontFamily("Garamond")
  12. .setFontWeight("normal")
  13. .addStyle(CSS.textAlign(Align.CENTER));
  14.  
  15. group.addRect(new Rectangle(0, 0, 265, 75)).setFill(Color.black);
  16. group.addText("Hello World!").setX(25).setY(50)
  17. .setFilter("url(#glow)")
  18. .setFill(Color.white).addStyle("font-size: 2.5em");
  19.  
  20. SvgDoc doc = new SvgDoc(svg);
  21.  
  22. doc.toIndentedSVG(new File("TestSvgFilters_testGlow.svg"), 0, 3);
  23. }
  24.  
  25. //--------------------------------------------------------------------------
  26. private SvgNode createGlowFilter()
  27. {
  28. SvgFilter filter = new SvgFilter().setId("glow").setHeight("150%").setY("-25%");
  29. filter.addFeFlood().setFloodColor(HTMLColor.LIGHT_GREEN);
  30. filter.addFeComposite().setIn2(FeInput.SourceAlpha).setOperator(CompositingOperatorType.in);
  31. filter.addFeGaussianBlur().setStdDeviation(4);
  32.  
  33. SvgFeComponentTransfer componentTransfer = filter.addFeComponentTransfer();
  34. componentTransfer.addFeFuncA().setType(ComponentTransferType.linear).setSlope(3).setIntercept(0);
  35.  
  36. SvgFeMerge merge = filter.addFeMerge();
  37. merge.addFeMergeNode();
  38. merge.addFeMergeNode().setIn(FeInput.SourceGraphic);
  39.  
  40. return filter;
  41. }
would produce the following
SVG:
  1. <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
  2. <svg height='75' width='265' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns='http://www.w3.org/2000/svg'>
  3. <defs>
  4. <filter height='150%' id='glow' y='-25%'>
  5. <feFlood flood-color='#90ee90' />
  6. <feComposite in2='SourceAlpha' operator='in' />
  7. <feGaussianBlur stdDeviation='4' />
  8. <feComponentTransfer>
  9. <feFuncA intercept='0.0' slope='3.0' type='linear' />
  10. </feComponentTransfer>
  11. <feMerge>
  12. <feMergeNode />
  13. <feMergeNode in='SourceGraphic' />
  14. </feMerge>
  15. </filter>
  16. </defs>
  17. <g style='font-family:Garamond;font-weight:normal;text-align:center'>
  18. <rect fill='#000000' height='75' width='265' x='0' y='0' />
  19. <text fill='#ffffff' filter='url(#glow)' style='font-size: 2.5em' x='25' y='50'>Hello World!</text>
  20. </g>
  21. </svg>
which looks like this:

Return to Main Page