package xyz.wbsite.jmacro.base; import cn.hutool.cache.Cache; import cn.hutool.cache.CacheUtil; import cn.hutool.core.convert.Convert; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.ReUtil; import xyz.wbsite.jmacro.util.ImageUtil; import xyz.wbsite.jmacro.util.Logger; import java.awt.image.BufferedImage; import java.io.File; import java.util.List; /** * 图例 *

* 图例分带坐标和不带坐标图例 *

* 其中带坐标图例,通过将坐标融合进文件名称实现,定义如下 * 以something#L0,0.png为例 * 图例名称:something * 图例坐标:#L0,0(在实例化时转为location) * * @author wangbing * @version 0.0.1 * @since 1.8 */ public class Legend { /** * 默认图例目录 */ private static File defaultBase = new File("legend"); /** * 图例缓存 */ public static Cache fileCache = CacheUtil.newLFUCache(999); /** * 图例目录 */ private File base; /** * 图例文件 */ private File file; /** * 图例名称 */ private String name; /** * 图例坐标 */ private ViewPoint location; public static void setDefaultBase(File base) { defaultBase = base; } public static File getDefaultBase() { return defaultBase; } private Legend() { } /** * 获取图例 * * @param name 图例名称 * @return 图例 */ public static Legend inflate(String name) { Legend legend = fileCache.get(name); if (legend != null) { return legend; } List files = FileUtil.loopFiles(defaultBase, pathname -> pathname.getName().startsWith(name)); if (files.size() == 0) { Logger.error(defaultBase.getAbsolutePath() + " not found the legend of " + name); throw new RuntimeException(defaultBase.getAbsolutePath() + " not found the legend of " + name); } File file = files.get(0); Legend newLegend = new Legend(); newLegend.base = defaultBase; newLegend.file = file; newLegend.name = name; if (!file.getName().matches("[\\S\\s]+#L[0-9]+,[0-9]+\\.png")) { newLegend.location = null; } else { int x = Convert.toInt(ReUtil.get("[\\S\\s]+#L([0-9]+),[0-9]+\\.png", file.getName(), 1), 0); int y = Convert.toInt(ReUtil.get("[\\S\\s]+#L[0-9]+,([0-9]+)\\.png", file.getName(), 1), 0); newLegend.location = new ViewPoint(x, y); } fileCache.put(name, newLegend); return newLegend; } public File getBase() { return base; } public File getFile() { return file; } public BufferedImage getImage() { return ImageUtil.load(this.file); } public String getName() { return name; } public ViewPoint getLocation() { return location; } }