I only ask this since all the examples I find are for "charting" and what I want to do is not really a chart, but rather a simple SVG or a "drawing in general".
I want to write a microservice that generates SVG files similar to this website: https://avatars.dicebear.com/styles/initials
Would JFreeSVG work well for this? Is there a "draw text" Hello World example somewhere that I can start with?
Also, I would like to draw diagonal lines on icons and also draw other symbols.
Can JFreeSVG create square icons containing text?
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: Can JFreeSVG create square icons containing text?
Yes, in fact JFreeSVG is able to create SVG output for anything you can "draw" via Java's Graphics2D API. It's not limited to charts. When I get time I'll post a small sample program.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: Can JFreeSVG create square icons containing text?
Here is a sample - you would probably want to add some code to compute the string bounds and align the text accordingly, but the hard-coded positions here worked OK for me:
Code: Select all
public static void main(String[] args) throws IOException {
SVGGraphics2D g2 = new SVGGraphics2D(200, 200);
g2.setPaint(Color.RED);
g2.fill(new Rectangle(10, 10, 180, 180));
g2.setFont(new Font(Font.MONOSPACED, Font.BOLD, 136));
g2.setPaint(Color.WHITE);
g2.drawString("DG", 20, 140);
SVGUtils.writeToSVG(new File("demo.svg"), g2.getSVGElement());
}
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader

