Slick Forums

Discuss the Slick 2D Library
It is currently Mon May 20, 2013 2:44 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Oct 10, 2011 7:26 am 
Offline

Joined: Mon Oct 10, 2011 7:07 am
Posts: 2
Hi all,
I was wondering how we could place an image (e.g. png) on a button. Of course, using a theme works but unfortunately this does not help as we do not have the images available upfront and therefore can not "hard-code" them in a theme.

So far I was trying to use the following code:

[code]
Button btn = new Button() {
@Override
protected void afterAddToGUI(final GUI gui) {
Texture texture = gui.getRenderer().loadTexture(pngFileLocation, "RGBA", "linear");
setBackground(texture.getImage(0,0, texture.getWidth(), texture.getHeight, null, false, Rotation.NONE);
}
}
[/code]

Background:
We are using TWL within an Eclipse rich client application. Other plugins may contribute extensions to our GUI that contain buttons with images. As we do not know what will be contributed before, we need to set the image of a button within the code when evaluating the UI contributions. The Button shall look like a regular button (designed by a theme :-) but instead of text, an image shall be shown.

Any help would be great.
Thanks,
Holger


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2011 3:51 pm 
Offline

Joined: Thu Jul 28, 2011 3:21 pm
Posts: 17
Location: Hungary
This is what I use.

in layout method:

I use a HashMap to store the images.

Code:
button.setBackground(ImageStore.getInstance().getTWLImage("wctitle"));


This method load a png image and convert it to TWL's DynamicImage

Code:
private DynamicImage loadTWLImage(String name) {
        try {
            BufferedImage img = ImageIO.read(new File("images/" + name + ".png"));
            ByteBuffer bb = ByteBuffer.allocateDirect(img.getWidth() * img.getHeight() * 4);

            for (int i = 0; i < img.getHeight(); i++) {
                for (int j = 0; j < img.getWidth(); j++) {
                    int argb = img.getRGB(j, i);
                    byte alpha = (byte) (argb >> 24);
                    byte red = (byte) (argb >> 16);
                    byte green = (byte) (argb >> 8);
                    byte blue = (byte) (argb);

                    bb.put(red);
                    bb.put(green);
                    bb.put(blue);
                    bb.put(alpha);
                }
            }
            bb.flip();

            DynamicImage dymage = Main.getTWLRenderer().createDynamicImage(img.getWidth(), img.getHeight());
            dymage.update(bb, DynamicImage.Format.RGBA);
            return dymage;
        } catch (IOException ex) {
            Logger.getLogger(ImageStore.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }


Hope it helps. Matthias have answers to your questations.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2011 5:39 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1171
If you call setBackground you should also override applyThemeBackground to be a nop. Otherwise applying a theme (or adding the widget to the GUI) will over write your background.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 11, 2011 7:21 am 
Offline

Joined: Mon Oct 10, 2011 7:07 am
Posts: 2
Thanks guys,
you helped me out.

Taking Matthias hint into account, I now use the following solution (I could not copy/paste the code so there might be some typos :roll: ):

Code:
URL imageUrl = ...; // the URL where the image is

Button btn = new Button(){
   @Override
   protected applyThemeBackground(final ThemeInfo themeInfo) {
      if (null != imageUrl) {
         try {
            final BufferedImage bi = ImageIO.read(imageUrl);
            final DynamicImage dynImage = getGUI().getRenderer().createDynamicImage(bi.getWidth(), bi.getHeight());
            dynImage.update(getDataBuffer(bi), Format.BGRA);
            setBackground(dynImage);
         }
         catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
};

public static ByteBuffer getDataBuffer(final BufferedImage bufImage) {
   final boolean hasAlpha = hasAlpha(bufImage);
   final BufferedImage bi = new BufferedImage(bugImage.getWidth(), bufImage.getHeight(), hasAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
   final Graphics2D g = bi.createGraphics();
   g.drawImage(bi, null, null);
   d.dispose();
   final int[] data = ((DataBufferInt) bi.getRaster().getDataBuffer()).getData();
   final ByteBuffer bb = ByteBuffer.allocateDirect(data.length * 4);
   bb.order(ByteOrder.LITTLE_ENDIAN);
   bb.asIntBuffer().put(data);
   return bb;
}

public static hasAlpha(final java.awt.Image img) {
   boolean hasAlpha = false;
   if (img instanceof BufferedImage) {
      hasAlpha = ((BufferedImage)img).getColorModel().hasAlpha();
   }
   else {
      final PixelGrabber pg = new PixelGrabber(img, 0, 0, 1, 1, false);
      try {
         pg.grabPixels();
         hasAlpha = pg.getColorModel().hasAlpha();
      }
      catch (final InterruptedException e) {
         e.printStackTrace();
      }
   }
}


Greetings,
Holger


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group