1

I am trying to write a NXT Robot simulator with lejOS where the robot can avoid obstacles, but I want the obstacles to be generated in the code. The code below allows for the creation of the Circle.jpg, but the code crashes if it tries to use the image it creates. The code works in Intellij IDEA, but not eclipse with a previously generated image. I have tried the following with no results:

  • Used .png instead of .jpg
  • Used and image generated by a previous run, meaning it already existed.
  • Changed type from opaque to translucent, etc.

I am wondering what I am doing that makes the image crash my code when generating the image on the fly?

Update: Added command used to invoke new jpg, and a picture of the error.

public static void obstacleFactory() 
{
    int width = 30;
    int height = 30;

    GraphicsEnvironment environment = 
            GraphicsEnvironment.getLocalGraphicsEnvironment();

    GraphicsDevice device = 
            environment.getDefaultScreenDevice();

    GraphicsConfiguration config = device.getDefaultConfiguration();

    BufferedImage bufferedImage = config.createCompatibleImage(width, height, 
            Transparency.TRANSLUCENT);

    Graphics2D g2d = bufferedImage.createGraphics();

    g2d.setColor(Color.yellow);
    g2d.fillOval(0, 0, width, height);

    g2d.dispose();

    RenderedImage rendImage = bufferedImage;

    try {
        File file = new File("src/sprites/Circle.jpg");
        ImageIO.write(rendImage, "jpg", file);
    } catch (IOException e) {}
}

And the command use to invoke the jpg is

NxtContext.useObstacle("sprites/Circle.jpg", 250, 475);

This is what eclipse says: enter image description here

FossilizedCarlos
  • 197
  • 2
  • 10

1 Answers1

1

I have no problems with your code in eclipse, except for the ellipse colors. What do you mean saying "the code crashes"? Do you gent an exception? What's the informaion in it? Could it be that you just don't have a "src/sprites" path?

Mikhail
  • 1,223
  • 14
  • 25
  • I'm not acquaint with the lejOS/NXT, but the exception seems to be mostly related to loading, not to saving. Are you sure that you have your file path specified correctly? In the creating code you use `src/sprites/` but in the load you use only `sprites/`, is it OK? You may want to check the documentation on specifying path for the Actor (e.g. here: `http://www.aplu.ch/classdoc/jgamegrid/ch/aplu/jgamegrid/Actor.html#Actor(boolean, java.lang.String)`) – Mikhail Mar 30 '12 at 10:23
  • Yep. The images are taken from /sprites, assuming the lejOS library takes care of the rest. The images I generate come from regular java code, so I have to tell it the full path. – FossilizedCarlos Mar 31 '12 at 00:33
  • Are you totally sure on the directories setup? The docs says `From the given filename the image file is searched in the following order: - if application is packed into a jar archive, relative to the root of the jar archive - relative to the directory /gamegrid/ - relative or absolute to current application directory` Which case is yours? Why do you put the image into subfolder of `src`? Also you may ask Dr. Aegidius Plüss for the sources of the library and debug it yourself. [The library home page says](http://www.aplu.ch/home/apluhomex.jsp?site=45) `Source code available soon (please ask)` – Mikhail Apr 01 '12 at 04:48
  • Found the problem. Eclipse does not refresh the navigator fast enough, and it is not automatic by default. – FossilizedCarlos Apr 03 '12 at 18:25