📄 resources.java
字号:
public static Resources open(InputStream resource) throws IOException { return new Resources(resource); } /** * Returns the image resource from the file * * @param id name of the image resource * @return cached image instance */ public Image getImage(String id) { return (Image)resources.get(id); } /** * Returns the animation resource from the file * * @param id name of the animation resource * @return cached image instance */ public StaticAnimation getAnimation(String id) { return (StaticAnimation)resources.get(id); } /** * Returns the data resource from the file * * @param id name of the data resource * @return newly created input stream that allows reading the data of the resource */ public InputStream getData(String id) { return new ByteArrayInputStream((byte[])resources.get(id)); } /** * Returns a hashmap containing localized String key/value pairs for the given locale name * * @param id name of the data resource * @param locale name of the locale resource * @return Hashtable containing key value pairs for localized data */ public Hashtable getL10N(String id, String locale) { return (Hashtable)((Hashtable)resources.get(id)).get(locale); } /** * Returns the font resource from the file * * @param id name of the font resource * @return cached font instance */ public Font getFont(String id) { return (Font)resources.get(id); } /** * Returns the theme resource from the file * * @param id name of the theme resource * @return cached theme instance */ public Hashtable getTheme(String id) { Hashtable h = (Hashtable)resources.get(id); // theme can be null in valid use cases such as the resource editor if(h != null && h.containsKey("uninitialized")) { Enumeration e = h.keys(); while(e.hasMoreElements()) { String key = (String)e.nextElement(); if(key.endsWith("font") || (key.endsWith("Image") && !key.endsWith("scaledImage"))) { Object value = h.get(key); if(value == null) { throw new IllegalArgumentException("Couldn't find resource: " + key); } // the resource was not already loaded when we loaded the theme // it must be loaded now so we can resolve the temporary name if(value instanceof String) { Object o = resources.get(value); if(o == null) { throw new IllegalArgumentException("Theme entry for " + key + " could not be found: " + value); } h.put(key, o); } } if(key.endsWith("border")) { Border b = confirmBorder(h, key); b.setPressedInstance(confirmBorder(h, key + "Pressed")); b.setFocusedInstance(confirmBorder(h, key + "Focused")); h.remove(key + "Pressed"); h.remove(key + "Focused"); h.put(key, b); } } h.remove("uninitialized"); } return h; } private Border confirmBorder(Hashtable h, String key) { Object val = h.get(key); if(val == null) { return null; } if(!(val instanceof Border)) { String[] value = (String[])val; if(value == null) { throw new IllegalArgumentException("Couldn't find resource: " + key); } // the resource was not already loaded when we loaded the theme // it must be loaded now so we can resolve the temporary name Border imageBorder = createImageBorder(value); return imageBorder; } return (Border)val; } private Border createImageBorder(String[] value) { Image[] images = new Image[value.length]; for(int iter = 0 ; iter < value.length ; iter++) { images[iter] = (Image)resources.get(value[iter]); } switch(images.length) { case 2: return Border.createImageBorder(images[0], images[1], null); case 3: return Border.createImageBorder(images[0], images[1], images[2]); case 8: return Border.createImageBorder(images[0], images[1], images[2], images[3], images[4], images[5], images[6], images[7], null); default: return Border.createImageBorder(images[0], images[1], images[2], images[3], images[4], images[5], images[6], images[7], images[8]); } } Object getResourceObject(String res) { return resources.get(res); } private Image createImage() throws IOException { byte[] data = new byte[input.readInt()]; input.readFully(data); return Image.createImage(data, 0, data.length); } private byte[] createData() throws IOException { byte[] data = new byte[input.readInt()]; input.readFully(data); return data; } private Font loadFont(String id, boolean packed) throws IOException { Image bitmap; if(packed) { bitmap = createPackedImage8(); } else { bitmap = createImage(); } int charCount = input.readShort(); int[] cutOffsets = new int[charCount]; int[] charWidth = new int[charCount]; for(int iter = 0 ; iter < charCount ; iter++) { cutOffsets[iter] = input.readShort(); charWidth[iter] = input.readByte(); } String charset = input.readUTF(); Font old = Font.getBitmapFont(id); if(old != null) { return old; } return Font.createBitmapFont(id, bitmap, cutOffsets, charWidth, charset); } private Hashtable loadTheme(String id, boolean newerVersion) throws IOException { Hashtable theme = new Hashtable(); theme.put("name", id); // marks the theme as uninitialized so we can finish "wiring" cached resources theme.put("uninitialized", Boolean.TRUE); int size = input.readShort(); for(int iter = 0 ; iter < size ; iter++) { String key = input.readUTF(); // if this is a simple numeric value if(key.endsWith("Color")) { theme.put(key, Integer.toHexString(input.readInt())); continue; } // if this is a short numeric value if(key.endsWith("transparency")) { theme.put(key, "" + (input.readByte() & 0xff)); continue; } // if this is a padding or margin then we will have the 4 values as bytes if(key.endsWith("padding") || key.endsWith("margin")) { int p1 = input.readByte() & 0xff; int p2 = input.readByte() & 0xff; int p3 = input.readByte() & 0xff; int p4 = input.readByte() & 0xff; theme.put(key, "" + p1 + "," + p2 + "," + p3 + "," + p4); continue; } if(key.endsWith("border")) { theme.put(key, createBorder(input, newerVersion)); if(newerVersion) { if(input.readBoolean()) { theme.put(key + "Pressed", createBorder(input, true)); } if(input.readBoolean()) { theme.put(key + "Focused", createBorder(input, true)); } } continue; } // if this is a font if(key.endsWith("font")) { Font f; // is this a bitmap font? if(input.readBoolean()) { String fontId = input.readUTF(); f = (Font)resources.get(fontId); // if the font is not yet loaded if(f == null) { theme.put(key, fontId); continue; } } else { f = Font.createSystemFont(input.readByte(), input.readByte(), input.readByte()); } theme.put(key, f); continue; } // if this is a background image if(key.endsWith("bgImage")) { String imageId = input.readUTF(); Image i = (Image)resources.get(imageId); // if the font is not yet loaded if(i == null) { theme.put(key, imageId); continue; } theme.put(key, i); continue; } if(key.endsWith("scaledImage")) { if(input.readBoolean()) { theme.put(key, "true"); } else { theme.put(key, "false"); } continue; } // thow an exception no idea what this is throw new IOException("Error while trying to read theme property: " + key); } return theme; } private Object createBorder(DataInputStream input, boolean newerVersion) throws IOException { int type = input.readByte(); switch(type) { case BORDER_TYPE_EMPTY: return Border.getEmpty(); case BORDER_TYPE_LINE: // use theme colors? if(input.readBoolean()) { return Border.createLineBorder(input.readByte()); } else { return Border.createLineBorder(input.readByte(), input.readInt()); } case BORDER_TYPE_ROUNDED: // use theme colors? if(input.readBoolean()) { return Border.createRoundBorder(input.readByte(), input.readByte()); } else { return Border.createRoundBorder(input.readByte(), input.readByte(), input.readInt()); } case BORDER_TYPE_ETCHED_LOWERED: // use theme colors? if(input.readBoolean()) { return Border.createEtchedLowered(); } else { return Border.createEtchedLowered(input.readInt(), input.readInt()); } case BORDER_TYPE_ETCHED_RAISED: // use theme colors? if(input.readBoolean()) { return Border.createEtchedRaised(); } else { return Border.createEtchedRaised(input.readInt(), input.readInt()); } case BORDER_TYPE_BEVEL_RAISED: // use theme colors? if(input.readBoolean()) { return Border.createBevelRaised(); } else { return Border.createBevelRaised(input.readInt(), input.readInt(), input.readInt(), input.readInt()); } case BORDER_TYPE_BEVEL_LOWERED: // use theme colors? if(input.readBoolean()) { return Border.createBevelLowered(); } else { return Border.createBevelLowered(input.readInt(), input.readInt(), input.readInt(), input.readInt()); } case BORDER_TYPE_IMAGE: Object[] imageBorder = readImageBorder(input); if(!newerVersion) { // legacy issue... input.readBoolean(); } return imageBorder; } return null; } private String[] readImageBorder(DataInputStream input) throws IOException { // Read number of images can be 2, 3, 8 or 9 int size = input.readByte(); String[] imageBorder = new String[size]; for(int iter = 0 ; iter < size ; iter++) { imageBorder[iter] = input.readUTF(); } return imageBorder; } private Hashtable loadL10N() throws IOException { Hashtable l10n = new Hashtable(); int keys = input.readShort(); int languages = input.readShort(); String[] keyArray = new String[keys]; for(int iter = 0 ; iter < keys ; iter++) { String key = input.readUTF(); keyArray[iter] = key; } for(int iter = 0 ; iter < languages ; iter++) { Hashtable currentLanguage = new Hashtable(); String lang = input.readUTF(); l10n.put(lang, currentLanguage); for(int valueIter = 0 ; valueIter < keys ; valueIter++) { currentLanguage.put(keyArray[valueIter], input.readUTF()); } } return l10n; } /** * Creates a packed image from the input stream for an 8 bit packed image */ private IndexedImage createPackedImage8() throws IOException { // read the length of the palette; int[] palette = new int[input.readByte() & 0xff]; for(int iter = 0 ; iter < palette.length ; iter++) { palette[iter] = input.readInt(); } int width = input.readShort(); int height = input.readShort(); byte[] data = new byte[width * height]; input.readFully(data); return new IndexedImage(width, height, palette, data); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -