Results 1 to 6 of 6
  1. #1
    CollinD is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Question Problem running oustide of NetBeans

    Hi Everyone,

    I have just finished an application that inverts colors in images (long story, but yes it has a purpose) and it runs fine in NetBeans when I hit F6. However, when I do a clean+build, and try to run the JAR via double-click, it has some interesting issues. Occasionally the JFrame GUI will display blank, other times it will be a static image that does not change when I click on it, mouse over, etc. When the GUI does work, I am able to select images to invert via the button using a JFileChooser. But when I click 'invert', the SwingWorker class that handles inversion instantly finishes, but does not actually perform the inversion.
    In case it matters, the method for inverting the images is as follows:
    Pull image from file into BufferedImage, invert pixels, write to png file using imageIO.write.

    I have no idea why it is not working outside of NetBeans, any help would be much appreciated. Let me know if more information is needed.

    Thanks!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    Can you provide an SSCCE that demonstrates this behavior?

  3. #3
    CollinD is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Here's an example that does the exact same thing. First file is the main application, I removed the GUI and everything so now it just works from hardcoded paths
    Java Code:
    public class Main {
        public static void main(String[] args) {
            BufferedImage bi = ImageInverter.loadImageFromFile("c:\\HSG.tif");
            ImageInverter.invertBufferedImage(bi);
            java.io.File outfile = new java.io.File("c:\\temp\\HSG.png");
            try
            {
                javax.imageio.ImageIO.write(bi, "png", outfile);
            }
            catch (Exception ignore)
            {
    
            }
        }
    
    }
    my ImageInverter class:
    Java Code:
    import java.awt.image.*;
    import javax.media.jai.*;
    import java.util.*;
    public class ImageInverter {
    //hash table stores known values
    private static Hashtable ht = new Hashtable();
     //invert colors of a buffered image
        public static void invertBufferedImage(BufferedImage bi)
        {
            int width = bi.getWidth(null);
            int height = bi.getHeight(null);
            int percent;
            int clr, newClr;
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    clr = bi.getRGB(i, j);
                    newClr = invertColor(clr);
                    bi.setRGB(i, j, newClr);
                }
            }
        }
        //fetch image from file
        public static BufferedImage loadImageFromFile(String fname)
        {
            RenderedImage img = JAI.create("fileload", fname);
            BufferedImage bi = convertRenderedImage(img);
            img = null;
            return bi;
        }
        //fetch inverted color
        public static int invertColor(int clr)
        {
            //inverts color
            //format: 0xAARRGGBB
            long clor = 0;
            if (ht.containsKey(clr))
            {
                return (Integer)ht.get(clr);
            }
            int r, g, b, a;
    
            String val = Integer.toHexString(clr);
            //will not affect alpha values
            //fetch RGB
            a = Integer.parseInt(val.substring(0, 2), 16);
            r = Integer.parseInt(val.substring(2, 4), 16);
            g = Integer.parseInt(val.substring(4, 6), 16);
            b = Integer.parseInt(val.substring(6, 8), 16);
            //invert RGB
            r = 0xFF - r;
            g = 0xFF - g;
            b = 0xFF - b;
            //rebuild color int
            val = getColorString(a, r, g, b);
            clor = Long.parseLong(val, 16); //Workaround for a bug in Integer.parseInt
            //add color as known
            ImageInverter.ht.put(clr, (int)clor);
            return (int)clor;
        }
        //parse color int (0xAARRGGBB) into string
        public static String getColorString(int a, int r, int g, int b)
        {
                    String val = "";
                    String aString, bString, gString, rString;
                    aString = fixLen(a);
                    bString = fixLen(b);
                    rString = fixLen(r);
                    gString = fixLen(g);
                    val = aString + rString + gString + bString;
                    return val;
        }
        //helper
        public static String fixLen(int a)
        {
            String ret = Integer.toString(a, 16);
            if (ret.length() == 1)
                ret = "0" + ret;
            return ret;
        }
        //Found at http://www.jguru.com/faq/view.jsp?EID=114602
        public static BufferedImage convertRenderedImage(RenderedImage img) {
    		if (img instanceof BufferedImage) {
    			return (BufferedImage)img;
    		}
    		ColorModel cm = img.getColorModel();
    		int width = img.getWidth();
    		int height = img.getHeight();
    		WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    		boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    		Hashtable properties = new Hashtable();
    		String[] keys = img.getPropertyNames();
    		if (keys!=null) {
    			for (int i = 0; i < keys.length; i++) {
    				properties.put(keys[i], img.getProperty(keys[i]));
    			}
    		}
    		BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties);
    		img.copyData(raster);
    		return result;
    	}
    }
    It's a very quick and dirty solution, but it is working when run through NetBeans. Has the exact same issue once built.

  4. #4
    CollinD is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    I managed to get it to throw an error (still works in netbeans):
    . . .\dist>java -jar SSCCE.jar
    Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/j
    at sscce.ImageInverter.loadImageFromFile(ImageInverte r.java:37)
    at sscce.Main.main(Main.java:18)
    Caused by: java.lang.ClassNotFoundException: javax.media.jai.JAI
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more
    Some kind of problem finding JAI maybe?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

  6. #6
    CollinD is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the quick and helpful info, Kevin. Took me a while to figure out how to properly set the classpath in NetBeans, but that did it. Apparently 'build projects in classpath' was checked, and that was the issue.

Similar Threads

  1. Little problem I'm running with my applet
    By Lightcraft in forum Java Applets
    Replies: 1
    Last Post: 04-09-2010, 01:27 AM
  2. Replies: 0
    Last Post: 08-26-2009, 01:03 PM
  3. Replies: 2
    Last Post: 03-09-2009, 02:36 AM
  4. Problem in running ant
    By sireesha in forum New To Java
    Replies: 4
    Last Post: 05-30-2008, 06:10 PM
  5. jsp running problem
    By bharanikumariyerjava in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 10:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •