Changing the Icon

Hi All,

I had gone thru the Tutorial examples, and that all working fine.

Since im developing my own games by using jME, i need to change the icon in title bar.

Can anyone tell me the solution??

I voted for X.  :stuck_out_tongue:

Pretty tight voting, right Darkfrog?  :stuck_out_tongue:



On the other hand, you can certainly change the icon yourself if you don't use the default implementations of a game (i.e. StandardGame/SimpleGame). Maybe there is even a way to do it in those instances, but I don't know.  :wink:

I voted Y, because I didn't want to vote the same like darkfrog :roll:



The method you need for changing the icon is DisplaySystem.setIcon(Icon[]). It can be invoked in any game app, in a class derived from SimpleGame you could call

display.setIcon(new Icon[]{myIcon});


to set a single icon.

Thank u irrisor,

But it is also not working, I tried with jpg,bmp,and gif files, but the exception is raising ,



com.jme.system.JmeException: Your icon is in a format that could not be converted to RGBA8888

at com.jme.system.lwjgl.LWJGLDisplaySystem.setIcon(LWJGLDisplaySystem.java:529)




can u put more light on this?

The reason why, is because currently the setIcon method does dummy conversion (i.e. it only attempts to NOT convert the image).



(From ImageUtil)

   public static Image convert(Image source, int convertTo) throws JmeException {

      switch (convertTo) {
      case Image.RGBA8888:
         if (source.getType() == Image.RGB888)
            return _RGB888_to_RGBA8888(source);
         break;
      }
      throw new JmeException("Can not convert to this image format yet ("
            + source.getType() + " to " + convertTo   + ")");
   }



To change the format of your image, in a non extremely nice way, you can do:


Image img; //Your icon image, obtained from a jpg, or png, or something.
BufferedImage newimg = new BufferedImage( img.getWidth(null), img.getHeight(null), BufferedImage.INT_ARGB );
Graphics2D draw = mewimage.createGraphics();
draw.drawImage( img, 0, 0, null );
draw.dispose();



Now you can use this new image as your icon.

Hope it helps

i'm still confused. can someone show how to start from an image filename and end with setIcon()? i'm stuck with this broken code:


URL url = this.getClass().getResource("path/to/image.png");
java.awt.Image img = Toolkit.getDefaultToolkit().getImage(url);
BufferedImage newimg = new BufferedImage( img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D draw = newimg.createGraphics();
draw.drawImage( img, 0, 0, null );
draw.dispose();
DisplaySystem.getDisplaySystem().setIcon(new Image[]{newimg});


Could you explain in which way is it broken? I have not tried it myself…  :expressionless:

well part of my problem is that there's an awt "Image" class and a jme "Image" class, so i'm not sure which is used in which sample code, and how i can go from one to the other.



so to answer your question. i currently have an error on the last line. i guess i need to convert a BufferedImage into a jme.Image (which then can be put into a jme.Image[])?

Oh, that is correct, I didn't see that it takes a jME Image and not an awt Image…



To change that, you can use the ImageGraphics class instead of a Graphics2D… and thus, the code would be as follows:


RL url = this.getClass().getResource("path/to/image.png");
java.awt.Image img = Toolkit.getDefaultToolkit().getImage(url);
LWJGLImageGraphics draw = new LWJGLImageGraphics( img.getWidth(null), img.getHeight(null), 1 );
draw.drawImage( img, 0, 0, null );
DisplaySystem.getDisplaySystem().setIcon(new Image[]{draw.getImage()});

it says com.jmex.awt.swingui.LWJGLImageGraphics is not visible. do i have to subclass it or something?

That is what happens whenever I give code without actually trying to compile it…  :smiley:



Use this to obtain a ImageGraphics:


ImageGraphics.createInstance(img.getWidth(null), img.getHeight(null), 1);

now i get the following exception. ideas?


url:file:./path/to/myimage.png
img:apple.awt.OSXImage@a49182
Exception in thread "Thread-8" java.lang.IllegalArgumentException: Width (-1) and height (-1) must be > 0
   at java.awt.image.SampleModel.<init>(SampleModel.java:108)
   at java.awt.image.ComponentSampleModel.<init>(ComponentSampleModel.java:128)
   at java.awt.image.PixelInterleavedSampleModel.<init>(PixelInterleavedSampleModel.java:69)
   at java.awt.image.Raster.createInterleavedRaster(Raster.java:638)
   at java.awt.image.Raster.createInterleavedRaster(Raster.java:265)
   at java.awt.image.BufferedImage.<init>(BufferedImage.java:385)
   at com.jmex.awt.swingui.LWJGLImageGraphics.<init>(Unknown Source)
   at com.jmex.awt.swingui.LWJGLImageGraphics.<init>(Unknown Source)
   at com.jmex.awt.swingui.ImageGraphics.createInstance(Unknown Source)



my code is below. the Toolkit is returning an apple.awt.OSXImage even if i cast it to java.awt.Image. is this the source of the problem perhaps?

    public void setIcon(String filename) {        
       URL url = KSTexture.getURL(filename);
       System.out.println("url:"+url);
       java.awt.Image img = (java.awt.Image) Toolkit.getDefaultToolkit().getImage(url);
       System.out.println("img:"+img.toString());
       ImageGraphics draw = ImageGraphics.createInstance(img.getWidth(null), img.getHeight(null), 1);
       draw.drawImage(img, 0, 0, null);
       DisplaySystem.getDisplaySystem().setIcon(new Image[]{draw.getImage()});
    }

Well, that is a problem with your image, not with the code. As for the Mac specific image, I bet that is just their implementation of the java.awt.Image, and therefore, should work fine.



The main problem comes from the fact that perhaps your image is not fully loaded… try using a MediaTracker for that.

duenez said:

try using a MediaTracker for that.

Or use ImageIO - even easier and blocks automatically until the image is fully loaded.

hmm tried those suggestions and that doesn't seem to be the problem.



anyway, i appreciate the suggestions. let me know if any one else knows how to get setIcon to work.

I know this thread is old, but anyway :slight_smile:



I have followed suggestion from this thread and everything runs well except that there is no icon showing. The standard icon is gone and replaced by nothing.



When printing out the information about the image I get this:



Image information: BufferedImage@9f2a0b: type = 0 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@1813fac transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 16 height = 16 #numDataElements 3 dataOff[0] = 0



So I guess the image has been loaded ok, but still blank icon :confused:



Suggestions?



Here is my code:

   public static void changeIcon(String imagePathName) {
         URL iconPath = B3dMain.class.getClassLoader().getResource("data/images/" + imagePathName + ".png");
         System.out.println(iconPath);
         try {
            BufferedImage img = ImageIO.read(iconPath);
             System.out.println("Image information: " + img.toString());
             ImageGraphics draw = ImageGraphics.createInstance(img.getWidth(null), img.getHeight(null), 1);
             System.out.println(draw.drawImage(img, 0, 0, null));
             DisplaySystem.getDisplaySystem().setIcon(new Image[]{draw.getImage()});
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
   }



Its called from a Standardgame main:

public class B3dMain {
   public static void main(String[] args) {
      // Create our game
      StandardGame game = new StandardGame("Unpublic alpha 2008");
      // Set new icon
      changeIcon("icon16");
      // Start the game thread
      game.start();
      // Create our game state
      MainGameState gameState = new MainGameState();
      // Attach it to the GameStateManager
      GameStateManager.getInstance().attachChild(gameState);
      // Activate it
      gameState.setActive(true);
                        //More code goes here...

Has anybody yet solved this problem? I have the same problem like kbotnen. The image just isn't shown, it's just blank…  :frowning: