Can JFreeSVG create square icons containing text?

A discussion forum for JFreeSVG (a fast, lightweight, SVG generator for the Java platform).
Locked
djangofan
Posts: 1
Joined: Fri Dec 18, 2020 8:29 pm
antibot: No, of course not.

Can JFreeSVG create square icons containing text?

Post by djangofan » Fri Dec 18, 2020 8:38 pm

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.

david.gilbert
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?

Post by david.gilbert » Sat Dec 19, 2020 1:25 pm

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

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

david.gilbert
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?

Post by david.gilbert » Sat Dec 19, 2020 2:06 pm

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

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked