0

I have a Java Swing App, And I'm using an API, that API has all of the countries, their names and images. The images are in .SVG format. So I'm using FlatLaf libraries to show SVG images. I created a class named SVGImage.java and it is for adding an icon to JLabel, the code:

import com.formdev.flatlaf.extras.FlatSVGIcon;
import javax.swing.JLabel;

public class SVGImage extends JLabel {

   private FlatSVGIcon svgIcon;

   public void setSvgImage(String image, int width, int height) {
       svgIcon = new FlatSVGIcon(image, width, height);
       setIcon(svgIcon);
   }

}

I'm using this in my MainJFrame class like this:

sVGImage1.setSvgImage("https://media.api-sports.io/flags/gb.svg", 300, 300);

This works fine with local SVG images, but when I try to open a URL like in this example, it can't display the image. What should I do? How can I display the SVG images from an URL? Thank you.

  • 1
    Try using the FlatSVGIcon( URL url ) constructor instead – MadProgrammer Dec 26 '22 at 21:42
  • Thank you. I didn't know there was a constructor like this. Now it worked! Is there any way to resize those images?(For example, all of them will be 300x300) It's not a huge problem for me but would be cool. –  Dec 26 '22 at 22:02
  • 1
    There is a protected constructor; protected FlatSVGIcon( String name, int width, int height, float scale, boolean disabled, ClassLoader classLoader, URI uri ); which all other constructors call, you could extend the class and add your own support for it or use one of the derive methods - public FlatSVGIcon derive( int width, int height ) – MadProgrammer Dec 26 '22 at 22:11
  • 1
    I’ve never used the api and have just been reading the source code – MadProgrammer Dec 26 '22 at 22:11
  • Thank you :) I'm still a computer science student. I'll improve myself on reading the api docs and source codes. –  Dec 26 '22 at 22:18
  • Okay, here's something you might want to look into - "composition over inherency"; extending `JLabel` in this case has a code smell about it. Personally, I'd consider some kind of utility class which could take a SVG source of some kind and would return a pre-configured `JLabel`, but that's me – MadProgrammer Dec 26 '22 at 22:30

0 Answers0