📄 mxutils.java
字号:
Rectangle rect = new Rectangle((int) Math.round(cx - w / 2), (int) Math.round(cy - h / 2), w, h); return rect.contains(x, y); } return true; } /** * Returns true if the dictionary contains true for the given key or * false if no value is defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @return Returns the boolean value for key in dict. */ public static boolean isTrue(Hashtable dict, String key) { return isTrue(dict, key, false); } /** * Returns true if the dictionary contains true for the given key or the * given default value if no value is defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @param defaultValue Default value to return if the key is undefined. * @return Returns the boolean value for key in dict. */ public static boolean isTrue(Hashtable dict, String key, boolean defaultValue) { Object value = dict.get(key); if (value == null) { return defaultValue; } else { if (value.equals("0")) { return false; } return value.equals("1") || Boolean.parseBoolean(value.toString()); } } /** * Returns the value for key in dictionary as an int or 0 if no value is * defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @return Returns the integer value for key in dict. */ public static int getInt(Hashtable dict, String key) { return getInt(dict, key, 0); } /** * Returns the value for key in dictionary as an int or the given default * value if no value is defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @param defaultValue Default value to return if the key is undefined. * @return Returns the integer value for key in dict. */ public static int getInt(Hashtable dict, String key, int defaultValue) { Object value = dict.get(key); if (value == null) { return defaultValue; } else { // Handles commas by casting them to an int return (int) Float.parseFloat(value.toString()); } } /** * Returns the value for key in dictionary as a float or 0 if no value is * defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @return Returns the float value for key in dict. */ public static float getFloat(Hashtable dict, String key) { return getFloat(dict, key, 0); } /** * Returns the value for key in dictionary as a float or the given default * value if no value is defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @param defaultValue Default value to return if the key is undefined. * @return Returns the float value for key in dict. */ public static float getFloat(Hashtable dict, String key, float defaultValue) { Object value = dict.get(key); if (value == null) { return defaultValue; } else { return Float.parseFloat(value.toString()); } } /** * Returns the value for key in dictionary as a double or 0 if no value is * defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @return Returns the double value for key in dict. */ public static double getDouble(Hashtable dict, String key) { return getDouble(dict, key, 0); } /** * Returns the value for key in dictionary as a double or the given default * value if no value is defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @param defaultValue Default value to return if the key is undefined. * @return Returns the double value for key in dict. */ public static double getDouble(Hashtable dict, String key, double defaultValue) { Object value = dict.get(key); if (value == null) { return defaultValue; } else { return Double.parseDouble(value.toString()); } } /** * Returns the value for key in dictionary as a string or null if no value * is defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @return Returns the string value for key in dict. */ public static String getString(Hashtable dict, String key) { return getString(dict, key, null); } /** * Returns the value for key in dictionary as a string or the given default * value if no value is defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @param defaultValue Default value to return if the key is undefined. * @return Returns the string value for key in dict. */ public static String getString(Hashtable dict, String key, String defaultValue) { Object value = dict.get(key); if (value == null) { return defaultValue; } else { return value.toString(); } } /** * Returns the value for key in dictionary as a color or null if no value * is defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @return Returns the color value for key in dict. */ public static Color getColor(Hashtable dict, String key) { return getColor(dict, key, null); } /** * Returns the value for key in dictionary as a color or the given default * value if no value is defined for the key. * * @param dict Dictionary that contains the key, value pairs. * @param key Key whose value should be returned. * @param defaultValue Default value to return if the key is undefined. * @return Returns the color value for key in dict. */ public static Color getColor(Hashtable dict, String key, Color defaultValue) { Object value = dict.get(key); if (value == null) { return defaultValue; } else { return parseColor(value.toString()); } } /** * */ public static Font getFont(Hashtable style) { return getFont(style, 1); } /** * */ public static Font getFont(Hashtable style, double scale) { String fontFamily = getString(style, mxConstants.STYLE_FONTFAMILY, mxConstants.DEFAULT_FONTFAMILY); int fontSize = getInt(style, mxConstants.STYLE_FONTSIZE, mxConstants.DEFAULT_FONTSIZE); int fontStyle = getInt(style, mxConstants.STYLE_FONTSTYLE); int swingFontStyle = ((fontStyle & mxConstants.FONT_BOLD) == mxConstants.FONT_BOLD) ? Font.BOLD : Font.PLAIN; swingFontStyle += ((fontStyle & mxConstants.FONT_ITALIC) == mxConstants.FONT_ITALIC) ? Font.ITALIC : Font.PLAIN; return new Font(fontFamily, swingFontStyle, (int) Math.round(fontSize * scale)); } /** * */ public static String hexString(Color color) { int r = color.getRed(); int g = color.getGreen(); int b = color.getBlue(); return String.format("#%02X%02X%02X", r, g, b); } /** * Convert a string representing a 24/32bit hex color value into a Color * value. * * @param colorString * the 24/32bit hex string value (ARGB) * @return java.awt.Color (24bit RGB on JDK 1.1, 24/32bit ARGB on JDK1.2) * @exception NumberFormatException * if the specified string cannot be interpreted as a * hexidecimal integer */ public static Color parseColor(String colorString) throws NumberFormatException { if (colorString.equalsIgnoreCase("white")) { return Color.white; } else if (colorString.equalsIgnoreCase("black")) { return Color.black; } else if (colorString.equalsIgnoreCase("red")) { return Color.red; } else if (colorString.equalsIgnoreCase("green")) { return Color.green; } else if (colorString.equalsIgnoreCase("blue")) { return Color.blue; } else if (colorString.equalsIgnoreCase("orange")) { return Color.orange; } else if (colorString.equalsIgnoreCase("yellow")) { return Color.yellow; } else if (colorString.equalsIgnoreCase("pink")) { return Color.pink; } else if (colorString.equalsIgnoreCase("turqoise")) { return new Color(0, 255, 255); } else if (colorString.equalsIgnoreCase("gray")) { return Color.gray; } else if (colorString.equalsIgnoreCase("none")) { return null; } int value; try { value = (int) Long.parseLong(colorString, 16); } catch (NumberFormatException nfe) { value = Long.decode(colorString).intValue(); } return new Color(value); } /** * Returns a hex representation for the given color. * * @param color Color to return the hex string for. * @return Returns a hex string for the given color. */ public static String getHexColorString(Color color) { return Integer.toHexString((color.getRGB() & 0x00FFFFFF) | (color.getAlpha() << 24)); } /** * Reads the given filename into a string. * * @param filename Name of the file to be read. * @return Returns a string representing the file contents. * @throws IOException */ public static String readFile(String filename) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(filename))); StringBuffer result = new StringBuffer(); String tmp = reader.readLine(); while (tmp != null) { result.append(tmp + "\n"); tmp = reader.readLine(); } reader.close(); return result.toString(); } /** * Writes the given string into the given file. * * @param contents String representing the file contents. * @param filename Name of the file to be written. * @throws IOException */ public static void writeFile(String contents, String filename) throws IOException { FileWriter fw = new FileWriter(filename); fw.write(contents); fw.flush(); fw.close(); } /** * Returns the Md5 hash for the given text. * * @param text String whose Md5 hash should be returned. * @return Returns the Md5 hash for the given text. */ public static String getMd5Hash(String text) { StringBuffer result = new StringBuffer(32); try { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(text.getBytes()); Formatter f = new Formatter(result); byte[] digest = md5.digest(); for (int i = 0; i < digest.length; i++) { f.format("%02x", new Object[] { new Byte(digest[i]) }); } } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); } return result.toString(); } /** * Returns true if the user object is an XML node with the specified type * and and the optional attribute has the specified value or is not * specified. * * @param value Object that should be examined as a node. * @param nodeName String that specifies the node name. * @return Returns true if the node name of the user object is equal to the * given type. */ public static boolean isNode(Object value, String nodeName) { return isNode(value, nodeName, null, null); } /** * Returns true if the given value is an XML node with the node name * and if the optional attribute has the specified value. * * @param value Object that should be examined as a node. * @param nodeName String that specifies the node name. * @param attributeName Optional attribute name to check. * @param attributeValue Optional attribute value to check. * @return Returns true if the value matches the given conditions. */ public static boolean isNode(Object value, String nodeName, String attributeName, String attributeValue) { if (value instanceof Element) { Element element = (Element) value; if (nodeName == null || element.getNodeName().equalsIgnoreCase(nodeName)) { String tmp = (attributeName != null) ? element .getAttribute(attributeName) : null; return attributeName == null || (tmp != null && tmp.equals(attributeValue)); } } return false; } /** * * @param g * @param antiAlias * @param textAntiAlias */ public static void setAntiAlias(Graphics2D g, boolean antiAlias, boolean textAntiAlias) { g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, (antiAlias) ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, (textAntiAlias) ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); } /** * Clears the given area of the specified graphics object with the given * color or makes the region transparent. */ public static void clearRect(Graphics2D g, Rectangle rect, Color background) { if (background != null) { g.setColor(background); g.fillRect(rect.x, rect.y, rect.width, rect.height); } else { Composite composite = g.getComposite(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f)); g.fillRect(rect.x, rect.y, rect.width, rect.height); g.setComposite(AlphaComposite.SrcOver); } } /** * Creates a buffered image for the given parameters. */ public static BufferedImage createBufferedImage(int w, int h, Color background) throws OutOfMemoryError
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -