Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wrldwzrd89

macrumors G5
Original poster
Jun 6, 2003
12,110
77
Solon, OH
What I'm trying to do is fetch a PNG image from a JAR file that my NetBeans 6.5 project depends on. The weird thing is that it was working until I changed some code in an attempt to fix another issue. The problem seems to be the compositeImage() function. When I rename the function to OLDcompositeImage and write a temporary compositeImage function that simply returns null, Class.getResource() calls work. With that code there, all calls to Class.getResource() fail, and return null.

Here's the complete source of the file in question, if it helps any:
I had to take some stuff out of the source due to post character limits.
Code:
// GraphicsManager.java
// GNU license stuff goes here
package net.worldwizard.fantastle;

// imports go here

public class GraphicsManager {

    private static final Color TRANSPARENT = new Color(200, 200, 100);
    private static final Color OUTLINE = new Color(200, 100, 200);
    private static final Color INVERT = new Color(100, 200, 200);
    private static final Color CUSTOM_1 = new Color(200, 100, 100);
    private static final Color CUSTOM_2 = new Color(100, 200, 100);
    private static final Color CUSTOM_3 = new Color(100, 100, 200);
    public static final int MAX_WINDOW_SIZE = 720;
    public static final int WINDOW_SPACING = 10;

    public static ImageIcon getCompositeImage(BufferedImage bottom, BufferedImage top) {
        BufferedImage[] components = new BufferedImage[2];
        components[0] = bottom;
        components[1] = top;
        return new ImageIcon(GraphicsManager.compositeImage(components));
    }

    private static BufferedImage compositeImage(BufferedImage[] components) {
        Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
        CompositeContext cc = c.createContext(ColorModel.getRGBdefault(), ColorModel.getRGBdefault(), new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT));
        BufferedImage finalImage = components[0];
        for (int z = 1; z < components.length; z++) {
            WritableRaster wr0 = finalImage.getRaster();
            WritableRaster wr1 = components[z].getRaster();
            cc.compose(wr0, wr1, wr0);
            finalImage.setData(wr0);
        }
        return finalImage;
    }

    public static BufferedImage getCachedImage(final int size, final String category, final String name, final Color targetColor) {
        BufferedImage cachedTemplate = GraphicsManager.getImageTemplate(category, name, size);
        Tags cachedTags = GraphicsManager.getImageTags(category, name);
        BufferedImage recoloredTemplate = GraphicsManager.recolorImage(cachedTemplate, cachedTags, targetColor);
        return recoloredTemplate;
    }

    public static BufferedImage getCachedImageWithAttributes(final int size, final String category, final String name, final Color targetColor, final String[] attributeNames, final Color[] targetAttributeColors) {
        String normalCategory = GraphicsManager.normalizeName(category);
        String normalName = GraphicsManager.normalizeName(name);
        BufferedImage cachedTemplate = GraphicsManager.getImageTemplate(normalCategory, normalName, size);
        Tags cachedTags = GraphicsManager.getImageTags(normalCategory, normalName);
        BufferedImage recoloredTemplate = GraphicsManager.recolorImage(cachedTemplate, cachedTags, targetColor);
        BufferedImage[] cachedAttributeTemplate = new BufferedImage[attributeNames.length];
        Tags[] cachedAttributeTags = new Tags[attributeNames.length];
        BufferedImage[] recoloredAttributeTemplate = new BufferedImage[attributeNames.length];
        for (int z = 0; z < attributeNames.length; z++) {
            attributeNames[z] = GraphicsManager.normalizeName(attributeNames[z]);
            cachedAttributeTemplate[z] = GraphicsManager.getAttributeImageTemplate(normalName, attributeNames[z], size);
            cachedAttributeTags[z] = GraphicsManager.getAttributeImageTags(normalName, attributeNames[z]);
            recoloredAttributeTemplate[z] = GraphicsManager.recolorImage(cachedAttributeTemplate[z], cachedAttributeTags[z], targetAttributeColors[z]);
        }
        BufferedImage[] preComposite = new BufferedImage[recoloredAttributeTemplate.length + 1];
        for (int z = 0; z < preComposite.length; z++) {
            if (z == 0) {
                preComposite[z] = recoloredTemplate;
            } else {
                preComposite[z] = recoloredAttributeTemplate[z - 1];
            }
        }
        BufferedImage postComposite = GraphicsManager.compositeImage(preComposite);
        return postComposite;
    }

    public static ImageIcon getLogo() {
        try {
            final URL url = GraphicsManager.class.getResource("/net/worldwizard/fantastle/resources/graphics/logo/logo.png");
            final Image image = Toolkit.getDefaultToolkit().createImage((ImageProducer) url.getContent());
            final ImageIcon icon = new ImageIcon(image);
            if (icon != null) {
                return icon;
            } else {
                return null;
            }
        } catch (final IOException ie) {
            return null;
        } catch (final NullPointerException np) {
            return null;
        }
    }

    public static int getDefaultSize() {
        return 48;
    }

    private static BufferedImage recolorImage(final BufferedImage template, final Tags tags, final Color morph) {
        if (template != null) {
            BufferedImage converted = GraphicsManager.convertImage(template);
            if (tags.shouldRecolor() || tags.shouldParseSpecialColors()) {
                int w = converted.getWidth();
                int h = converted.getHeight();
                int pixel;
                for (int x = 0; x < w; x++) {
                    for (int y = 0; y < h; y++) {
                        pixel = converted.getRGB(x, y);
                        Color old = new Color(pixel);
                        Color transformed = old;
                        // Parse special colors
                        if (tags.shouldParseSpecialColors()) {
                            if (old.equals(GraphicsManager.TRANSPARENT)) {
                                transformed = new Color(old.getRed(), old.getGreen(), old.getBlue(), 0);
                            } else if (old.equals(GraphicsManager.OUTLINE)) {
                                if (old.getRed() > 127 || old.getGreen() > 127 || old.getBlue() > 127) {
                                    transformed = Color.WHITE;
                                } else {
                                    transformed = Color.BLACK;
                                }
                            } else if (old.equals(GraphicsManager.INVERT)) {
                                int tRed = 255 - morph.getRed();
                                int tGreen = 255 - morph.getGreen();
                                int tBlue = 255 - morph.getBlue();
                                transformed = new Color(tRed, tGreen, tBlue);
                            } else if (old.equals(GraphicsManager.CUSTOM_1)) {
                                int tRed = tags.getCustomColor(0, 0, old.getRed());
                                int tGreen = tags.getCustomColor(0, 1, old.getGreen());
                                int tBlue = tags.getCustomColor(0, 2, old.getBlue());
                                transformed = new Color(tRed, tGreen, tBlue);
                            } else if (old.equals(GraphicsManager.CUSTOM_2)) {
                                int tRed = tags.getCustomColor(1, 0, old.getRed());
                                int tGreen = tags.getCustomColor(1, 1, old.getGreen());
                                int tBlue = tags.getCustomColor(1, 2, old.getBlue());
                                transformed = new Color(tRed, tGreen, tBlue);
                            } else if (old.equals(GraphicsManager.CUSTOM_3)) {
                                int tRed = tags.getCustomColor(2, 0, old.getRed());
                                int tGreen = tags.getCustomColor(2, 1, old.getGreen());
                                int tBlue = tags.getCustomColor(2, 2, old.getBlue());
                                transformed = new Color(tRed, tGreen, tBlue);
                            }
                            pixel = transformed.getRGB();
                            converted.setRGB(x, y, pixel);
                        }
                        // Do general recoloring
                        if (tags.shouldRecolor()) {
                            int oRed = old.getRed();
                            int oGreen = old.getGreen();
                            int oBlue = old.getBlue();
                            if (oRed == oGreen && oRed == oBlue && oGreen == oBlue) {
                                float intensity = (float) ((float) oRed / 255.0);
                                int tRed = (int) (morph.getRed() * intensity);
                                int tGreen = (int) (morph.getGreen() * intensity);
                                int tBlue = (int) (morph.getBlue() * intensity);
                                transformed = new Color(tRed, tGreen, tBlue);
                            }
                        }
                        pixel = transformed.getRGB();
                        converted.setRGB(x, y, pixel);
                    }
                }
            }
            return converted;
        } else {
            return null;
        }
    }

    private static BufferedImage convertImage(final BufferedImage template) {
        if (template.getType() != BufferedImage.TYPE_INT_ARGB) {
            int w = template.getWidth();
            int h = template.getHeight();
            BufferedImage templateOut = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            int pixel;
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    pixel = template.getRGB(x, y);
                    templateOut.setRGB(x, y, pixel);
                }
            }
            return templateOut;
        } else {
            return template;
        }
    }

    private static BufferedImage getImageTemplate(final String category, final String name, final int size) {
        try {
            String normalCategory = GraphicsManager.normalizeName(category);
            String normalName = GraphicsManager.normalizeName(name);
            final URL url = GraphicsManager.class.getResource("/net/worldwizard/fantastle/resources/graphics/" + normalCategory + "/" + normalName + "/" + String.valueOf(size) + ".png");
            final BufferedImage image = ImageIO.read(url);
            if (image != null) {
                return image;
            } else {
                return null;
            }
        } catch (final IOException ie) {
            return null;
        } catch (final NullPointerException np) {
            return null;
        } catch (final IllegalArgumentException ia) {
            return null;
        }
    }

    private static BufferedImage getAttributeImageTemplate(final String object, final String name, final int size) {
        try {
            String normalObject = GraphicsManager.normalizeName(object);
            String normalName = GraphicsManager.normalizeName(name);
            final URL url = GraphicsManager.class.getResource("/net/worldwizard/fantastle/resources/graphics/attributes/" + normalObject + "/" + normalName + "/" + String.valueOf(size) + ".png");
            final BufferedImage image = ImageIO.read(url);
            if (image != null) {
                return image;
            } else {
                return null;
            }
        } catch (final IOException ie) {
            return null;
        } catch (final NullPointerException np) {
            return null;
        } catch (final IllegalArgumentException ia) {
            return null;
        }
    }

    private static Tags getImageTags(final String category, final String name) {
        try {
            String normalCategory = GraphicsManager.normalizeName(category);
            String normalName = GraphicsManager.normalizeName(name);
            final URL url = GraphicsManager.class.getResource("/net/worldwizard/fantastle/resources/graphics/" + normalCategory + "/" + normalName + "/tags.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            return GraphicsManager.tagFileParser(br);
        } catch (final IOException ie) {
            return null;
        } catch (final NullPointerException np) {
            return null;
        } catch (final IllegalArgumentException ia) {
            return null;
        }
    }

    private static Tags getAttributeImageTags(final String object, final String name) {
        try {
            String normalObject = GraphicsManager.normalizeName(object);
            String normalName = GraphicsManager.normalizeName(name);
            final URL url = GraphicsManager.class.getResource("/net/worldwizard/fantastle/resources/graphics/attributes/" + normalObject + "/" + normalName + "/tags.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            return GraphicsManager.tagFileParser(br);
        } catch (final IOException ie) {
            return null;
        } catch (final NullPointerException np) {
            return null;
        } catch (final IllegalArgumentException ia) {
            return null;
        }
    }

    private static Tags tagFileParser(BufferedReader br) throws IOException {
        String recolor = br.readLine();
        String special = br.readLine();
        String custom1red, custom1green, custom1blue, custom2red, custom2green, custom2blue, custom3red, custom3green, custom3blue;
        custom1red = custom1green = custom1blue = custom2red = custom2green = custom2blue = custom3red = custom3green = custom3blue = null;
        try {
            custom1red = br.readLine();
            custom1green = br.readLine();
            custom1blue = br.readLine();
            custom2red = br.readLine();
            custom2green = br.readLine();
            custom2blue = br.readLine();
            custom3red = br.readLine();
            custom3green = br.readLine();
            custom3blue = br.readLine();
        } catch (final IOException ie) {
            // Ignore
        }
        br.close();
        // Parse the tags file
        String[] recolorArray = recolor.split("=");
        String[] specialArray = special.split("=");
        boolean recolorValue = false;
        boolean specialValue = false;
        if (recolorArray[0].equalsIgnoreCase("recolor")) {
            recolorValue = Boolean.parseBoolean(recolorArray[1]);
        }
        if (specialArray[0].equalsIgnoreCase("special")) {
            specialValue = Boolean.parseBoolean(specialArray[1]);
        }
        int[][] custom = null;
        String[] custom1RedArray = null;
        String[] custom1GreenArray = null;
        String[] custom1BlueArray = null;
        String[] custom2RedArray = null;
        String[] custom2GreenArray = null;
        String[] custom2BlueArray = null;
        String[] custom3RedArray = null;
        String[] custom3GreenArray = null;
        String[] custom3BlueArray = null;
        if (custom1red != null) {
            custom = new int[3][3];
            custom1RedArray = custom1red.split("=");
            custom1GreenArray = custom1green.split("=");
            custom1BlueArray = custom1blue.split("=");
            if (custom1RedArray[0].equalsIgnoreCase("custom1red")) {
                try {
                    custom[0][0] = Integer.parseInt(custom1RedArray[1]);
                } catch (NumberFormatException nf) {
                    custom[0][0] = 0;
                }
            }
            if (custom1GreenArray[0].equalsIgnoreCase("custom1green")) {
                try {
                    custom[0][1] = Integer.parseInt(custom1GreenArray[1]);
                } catch (NumberFormatException nf) {
                    custom[0][1] = 0;
                }
            }
            if (custom1BlueArray[0].equalsIgnoreCase("custom1blue")) {
                try {
                    custom[0][2] = Integer.parseInt(custom1BlueArray[1]);
                } catch (NumberFormatException nf) {
                    custom[0][2] = 0;
                }
            }
            if (custom2red != null) {
                custom2RedArray = custom2red.split("=");
                custom2GreenArray = custom2green.split("=");
                custom2BlueArray = custom2blue.split("=");
                if (custom2RedArray[0].equalsIgnoreCase("custom2red")) {
                    try {
                        custom[1][0] = Integer.parseInt(custom2RedArray[1]);
                    } catch (NumberFormatException nf) {
                        custom[1][0] = 0;
                    }
                }
                if (custom2GreenArray[0].equalsIgnoreCase("custom2green")) {
                    try {
                        custom[1][1] = Integer.parseInt(custom2GreenArray[1]);
                    } catch (NumberFormatException nf) {
                        custom[1][1] = 0;
                    }
                }
                if (custom2BlueArray[0].equalsIgnoreCase("custom2blue")) {
                    try {
                        custom[1][2] = Integer.parseInt(custom2BlueArray[1]);
                    } catch (NumberFormatException nf) {
                        custom[1][2] = 0;
                    }
                }
                if (custom3red != null) {
                    custom3RedArray = custom3red.split("=");
                    custom3GreenArray = custom3green.split("=");
                    custom3BlueArray = custom3blue.split("=");
                    if (custom3RedArray[0].equalsIgnoreCase("custom3red")) {
                        try {
                            custom[2][0] = Integer.parseInt(custom3RedArray[1]);
                        } catch (NumberFormatException nf) {
                            custom[2][0] = 0;
                        }
                    }
                    if (custom3GreenArray[0].equalsIgnoreCase("custom3green")) {
                        try {
                            custom[2][1] = Integer.parseInt(custom3GreenArray[1]);
                        } catch (NumberFormatException nf) {
                            custom[2][1] = 0;
                        }
                    }
                    if (custom3BlueArray[0].equalsIgnoreCase("custom3blue")) {
                        try {
                            custom[2][2] = Integer.parseInt(custom3BlueArray[1]);
                        } catch (NumberFormatException nf) {
                            custom[2][2] = 0;
                        }
                    }
                }
            }
        }
        Tags t = new Tags(recolorValue, specialValue, custom);
        return t;
    }

    private static String normalizeName(String name) {
        StringBuilder sb = new StringBuilder();
        for (int x = 0; x < name.length(); x++) {
            char curr = name.charAt(x);
            if (!Character.isWhitespace(curr)) {
                sb.append(curr);
            } else {

                sb.append("_");
            }
        }
        return sb.toString().toLowerCase();
    }
}
 

wrldwzrd89

macrumors G5
Original poster
Jun 6, 2003
12,110
77
Solon, OH
After experimenting a bit more, it seems that the root cause is the attempt to get the WritableRaster for the BufferedImage object.
private static BufferedImage compositeImage(BufferedImage[] components) {
Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
CompositeContext cc = c.createContext(ColorModel.getRGBdefault(), ColorModel.getRGBdefault(), new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT));
BufferedImage finalImage = components[0];
for (int z = 1; z < components.length; z++) {
WritableRaster wr0 = finalImage.getRaster();
WritableRaster wr1 = components[z].getRaster();

cc.compose(wr0, wr1, wr0);
finalImage.setData(wr0);
}
return finalImage;
}
The red lines are the ones that seem to be causing the problem.
 

wrldwzrd89

macrumors G5
Original poster
Jun 6, 2003
12,110
77
Solon, OH
Finally got this one fixed. After a lot of head-scratching, I figured out that if I went back to the old code, but changed one little function call, it works.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.