|
| 1 | +package de.doubleslash.keeptime.common; |
| 2 | + |
| 3 | +import javafx.geometry.Bounds; |
| 4 | +import javafx.scene.Node; |
| 5 | +import javafx.scene.shape.SVGPath; |
| 6 | +import org.w3c.dom.Document; |
| 7 | +import org.w3c.dom.NodeList; |
| 8 | +import org.xml.sax.SAXException; |
| 9 | + |
| 10 | +import javax.xml.parsers.DocumentBuilder; |
| 11 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 12 | +import javax.xml.parsers.ParserConfigurationException; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | + |
| 16 | +public class SvgNodeProvider { |
| 17 | + |
| 18 | + private static String getSvgPathWithXMl(Resources.RESOURCE resource) { |
| 19 | + String svgPath; |
| 20 | + Document document; |
| 21 | + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 22 | + DocumentBuilder db = null; |
| 23 | + |
| 24 | + |
| 25 | + try { |
| 26 | + db = dbf.newDocumentBuilder(); |
| 27 | + } catch (ParserConfigurationException e) { |
| 28 | + throw new RuntimeException(e); |
| 29 | + } |
| 30 | + |
| 31 | + try (InputStream inputStream = Resources.getResource(resource).openStream()) { |
| 32 | + document = db.parse(inputStream); |
| 33 | + NodeList nodeList = document.getElementsByTagName("path"); |
| 34 | + svgPath = nodeList.item(0).getAttributes().getNamedItem("d").getNodeValue(); |
| 35 | + } catch (IOException | SAXException e) { |
| 36 | + throw new RuntimeException(e); |
| 37 | + } |
| 38 | + return svgPath; |
| 39 | + } |
| 40 | + |
| 41 | + public static Node getSvgNode(Resources.RESOURCE resource){ |
| 42 | + SVGPath IconSvg = new SVGPath(); |
| 43 | + IconSvg.setContent(getSvgPathWithXMl(resource)); |
| 44 | + Bounds bounds = IconSvg.getBoundsInParent(); |
| 45 | + double scale = Math.min(15 / bounds.getWidth(), 15 / bounds.getHeight()); |
| 46 | + IconSvg.setScaleX(scale); |
| 47 | + IconSvg.setScaleY(scale); |
| 48 | + return IconSvg; |
| 49 | + } |
| 50 | +} |
0 commit comments