Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  reading images and accelerating them on windows  (Read 781 times)
0 Members and 1 Guest are viewing this topic.
Offline Kova

Senior Member





« Posted 2006-11-01 17:48:33 »

Hello.
So far I've been reading images through ImageIO.read(URL). Now when my game started to have more images I noticed low fps and that they aren't accelerated. I'm using Windows XP and I set sun.java2d.translaccel=true property but my image still dosen't get accelarated. I know that I can only accelarate images that aren't being modifyied (rotated, scaled...), only translated. To see if image got accelarated I use .getCapabilities(null).isAccelerated().
I've googled and read java2d faq but none was helpfull, I can use BufferImage constructor or .createCompatabileImage() to create empty accelarated image but when I put raster from loaded image into it, it dosen't get accelarated. I know solution is something trivial but I'm tired of constant reading and searching. So how do I load images and make them accelarated? Thank you.
Offline dishmoth
« Reply #1 - Posted 2006-11-01 18:17:31 »

[...] but when I put raster from loaded image into it, it dosen't get accelarated.

It may be that you need to drawImage() your original image onto a newly created, compatible BufferedImage.
Simon

Offline fletchergames

Senior Member





« Reply #2 - Posted 2006-11-01 18:47:57 »

Creating a "compatible" image is just something you have to do so that the JVM knows that the image should be a managed image.  Here's my method for doing it:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
/**Creates a blank BufferedImage.
 * @param width the width of the Image
 * @param height the height of the Image
 * @param shouldTransparencyBeAllowed whether to allow transparency in the BufferedImage
created.  In most implementations of Java, allowing transparency is slower.
 * @return the BufferedImage created
 */

public static BufferedImage createImage(final int width, final int height,
   final boolean shouldTransparencyBeAllowed)
{
   //get the GraphicsConfiguration
  GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.
      getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();

   //this enables/disables transparency and makes the Image more likely to be managed
  return graphicsConfiguration.createCompatibleImage(width, height,
      shouldTransparencyBeAllowed ? Transparency.BITMASK : Transparency.OPAQUE);
} //end createImage


Then you just need to draw the image you loaded to the image returned by the createImage method.

As you can see, it's not anything difficult, and it's something that doesn't seem like it should have to be done at all.  I don't know why images aren't "compatible" by default, but that's just the way it is.  It's a small price to pay for not having to write your own graphics library.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2006-11-01 18:57:48 »

Thank you both, this worked, I'll put it into my loadImage method so it's automatic

1  
2  
3  
4  
        temp_bfimg = loadImage("images/teren_gol.png");
        GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        teren_gol_bfimg = gc.createCompatibleImage(temp_bfimg.getWidth(), temp_bfimg.getHeight(), BufferedImage.TRANSLUCENT);
        teren_gol_bfimg.getGraphics().drawImage(temp_bfimg, 0, 0, null);


OK this is how it all looks like now (btw. I liked fletcher's javadoc so from now on I'll use it also, you can remove author part Smiley )
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
    /** load image from PATH and make it compatible so it can be accalarated
     * @author Kova (kova1337@gmail.com)
     * @param path - relative path to image, ex.: "images/some_image.png"
     * @param image_type - type of BufferedImage
     * @return compatible loaded BufferedImage or null if reading fails
     * @see Transparency.OPAQUE, .BITMASK, .TRANSLUCENT
     */

   public BufferedImage loadAndCreateCompatibleImage(String path, int image_type) {
       URL url = null;
       try {
          url = getClass().getClassLoader().getResource(path);
                GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
                BufferedImage temp1_bfimg = ImageIO.read(url);
                BufferedImage temp2_bfimg = gc.createCompatibleImage(temp1_bfimg.getWidth(), temp1_bfimg.getHeight(), image_type);
                temp2_bfimg.getGraphics().drawImage(temp1_bfimg, 0, 0, null);
           return temp2_bfimg;
       } catch (Exception e) {
          System.out.println("Error loading image: " + path + " " + url);
          System.exit(0);
          return null;
       }
   }
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!