Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Loading a BufferedImage  (Read 2575 times)
0 Members and 1 Guest are viewing this topic.
Offline PlaneGuy

Senior Newbie




Java games rock!


« Posted 2003-03-10 18:03:11 »

Im trying to load a image as a buffered image since I am going to rotate it around and other fun stuff like that.

The problem is I'm not loading it into the buffered image properly. When I try and draw it to my frame, all i get is a suitably sized black box. Tongue

Anyway, if you're willing to take a look at my loading code....

[CODE]
   private BufferedImage loadBI(String filename)
   {
       Image img=Toolkit.getDefaultToolkit().getImage(filename); //works
       int w = 512; // img.getWidth(this);
       int h = 384; // img.getHeight(this);
       BufferedImage newbi = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);  //variable name gives too much away Wink
       newbi.createGraphics().drawImage(img,0,0,this);
       return newbi;
   }
[/CODE]
Offline role

Junior Member




Java manmoth is coming!


« Reply #1 - Posted 2003-03-12 10:32:28 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      private BufferedImage loadBI(String filename) {
            Image img = Toolkit.getDefaultToolkit().getImage(filename); //works
           int w = 512; // img.getWidth(this);
           int h = 384; // img.getHeight(this);
           MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(img,0);
            try { tracker.waitForID(0); } catch(Exception e) { }
            BufferedImage newbi = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);  
            newbi.createGraphics().drawImage(img,0,0,this);
            return newbi;
      }


try this, i don't know is this the right answer or not but i think you create the image from unfinished read image
so wait the image loaded first using mediatracker

Golden T Game Engine - Java2D/OpenGL Game Engine
Golden T Website | Golden T Forum
Offline PlaneGuy

Senior Newbie




Java games rock!


« Reply #2 - Posted 2003-03-12 17:44:03 »

Quote


try this, i don't know is this the right answer or not but i think you create the image from unfinished read image
so wait the image loaded first using mediatracker



Yep, that was it. Thanks a lot!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PlaneGuy

Senior Newbie




Java games rock!


« Reply #3 - Posted 2003-03-13 01:04:28 »

Okay... maybe not.

I modified my code the get the width and height from thr Just loaded image, but now it gives ma a runtime error that says width and height are -1.

Now, width and height are -1 if they are "not yet known", or so the API spec says. Is my image loaded or not? I kind of need those values Tongue
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #4 - Posted 2003-03-13 05:52:42 »

What does your code look like now?
Maybe the tracker.waitForID(0) throws an exception for whatever reason (you could output the exception in the catch block to be sure).

Offline role

Junior Member




Java manmoth is coming!


« Reply #5 - Posted 2003-03-13 12:45:30 »

humm  Shocked
i don't get it
where do you put the get width and get height method?
you should put it after mediatracker, so the image fully loaded before asking its width
so the code must be like this
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
 private BufferedImage loadBI(String filename) { 
  Image img = Toolkit.getDefaultToolkit().getImage(filename); //works
 MediaTracker tracker = new MediaTracker(this);
  tracker.addImage(img,0);
  try { tracker.waitForID(0); } catch(Exception e) { }
  int w = img.getWidth(null);
  int h = img.getHeight(null);
  BufferedImage newbi = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);  
  newbi.createGraphics().drawImage(img,0,0,this);
  return newbi;
 }

Golden T Game Engine - Java2D/OpenGL Game Engine
Golden T Website | Golden T Forum
Offline bmyers

Junior Member





« Reply #6 - Posted 2003-03-13 15:21:13 »

There's a much simpler way, if you're not trying to load images over the network and are just loading them off of a local disk: use javax.imageio.ImageIO.read( File f )

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private BufferedImage loadBI(String filename)
{
    BufferedImage img = null;
    try {
                     File f = new File( filename );
                     img = javax.imageio.ImageIO.read( f );
    } catch { IOException ioe) { /* ... */ }

   return img;
}


--Brad

Offline bedelf

Junior Member




Are you suggesting coconuts migrate?


« Reply #7 - Posted 2003-03-13 15:21:43 »

try this code to load an image and make sure it's accelerated:

// get the graphicsConfig when you create the frame

GraphicsEnvironment graphicsEnvironment =
                       GraphicsEnvironment.getLocalGraphicsEnvironment();
           graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
           graphicsConfiguration = graphicsDevice.getDefaultConfiguration();
           
           frame = new Frame( "blah", graphicsConfiguration );

// then make some method like this one

public BufferedImage loadImage( String file ) {

// use the Toolkit and MediaTracker to load the image

           Toolkit tk = Toolkit.getDefaultToolkit();
           Image temp = tk.createImage( file );
           MediaTracker mt = new MediaTracker( frame );
           mt.addImage( temp, 0 );
           
           try {
                 mt.waitForAll();
           } catch ( Exception e ) {
               System.out.println(e.toString());
           }
           
           BufferedImage image = graphicsConfiguration.createCompatibleImage( temp.getWidth( null ),
                       temp.getHeight( null ), Transparency.BITMASK );

           Graphics g = image.getGraphics();
           g.drawImage( temp, 0, 0, null );
           g.dispose();
           
           return image;
     }
Offline bedelf

Junior Member




Are you suggesting coconuts migrate?


« Reply #8 - Posted 2003-03-13 15:26:22 »

are images loaded with javax.imageio accelerated?
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #9 - Posted 2003-03-13 17:37:00 »

Quote
are images loaded with javax.imageio accelerated?


don't think so, no.

but, its not hard to create an accelerated image, and copy it over.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline darcone

Junior Member




Size matters


« Reply #10 - Posted 2003-03-14 10:06:04 »

This is the code I use, I wrote it using some java tutorial...

public BufferedImage getBufferedImage(Image img) {
       BufferedImage bImg = new BufferedImage(img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_INT_ARGB);
       Graphics2D biContext = bImg.createGraphics();
       biContext.drawImage(img, 0, 0, null);    
       return bImg;
}
Offline PlaneGuy

Senior Newbie




Java games rock!


« Reply #11 - Posted 2003-03-14 17:20:16 »

Ok, I think I fixed it. Dumbass me wasn't passing a proper component. (I told you that variable name was revealing :P)

Is there any way I can do a similar loading without actually having some component to wait on? Or do I at least have to pass through a dummy component to the media tracker?

Thanks for all your help.
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #12 - Posted 2003-03-14 18:24:30 »

the best solution....  don't use MediaTracker!

simply use toolkit.createImage (or getImage)

then, to begin the loading,
toolkit.checkImage (passing in an image observer)
followed by a synchronized wait,
which gets woken by the image observer when the image is loaded completely.

Alternatively, forget about all that crap, and just use ImageIO.
(followed by copying the unaccelerated image into a blank acceleratable image)

If you still want to use toolkit.createImage, perhaps this image loader i wrote a while ago will be helpful.
(be warned, it isn't bulletproof - having said that, it includes a fix relating to Image.createScaledinstance())

It doesn't have any facility for handling images that ABORT or ERROR during loading.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
import java.awt.*;
import java.awt.image.*;
public class ImageLoader implements      ImageObserver
{
      //THIS CLASS IS NOT THREAD      SAFE
     final      Toolkit toolkit;
      final      GraphicsConfiguration gc;
      final      boolean debug;

      public ImageLoader(Toolkit      toolkit,      GraphicsConfiguration gc)
      {
            this(toolkit,gc,false);
      }

      public ImageLoader(Toolkit      toolkit,      GraphicsConfiguration gc, boolean debug)
      {
            this.toolkit =      toolkit;
            this.gc = gc;
            this.debug = debug;
      }

      public Image[]      loadImages(String[] s, int      transparency)
      {
            Image[] imagesToLoad      = new      Image[s.length];
            for(int i =      0;      i < imagesToLoad.length; i++)
            {
                  imagesToLoad[i] =      loadImage(s[i], transparency);
            }
            return imagesToLoad;
      }

      //      a duplicate      of      the image is made, because      some methods
     //      when called      using      an      Image      obtained      directly      from a file      (i.e.      the Images ImageProducer is a      file)
     //      are very      buggy      (i.e.      Image.getScaledInstance();)
     public Image loadImage(String      s,      int transparency)
      {
            if(debug) System.out.print("loadImage " +      s + "      ");

            //1st, fully load      the source image
           Image      sourceImage      = prepareImage(toolkit.createImage(getClass().getResource(s)));
            //then, create      a replica, that will      have its      ImageObserver as a memoryImage (rather      than a file)
           BufferedImage returnImage = gc.createCompatibleImage(sourceImage.getWidth(null),sourceImage.getHeight(null), transparency);

            //now, copy      the sourceImage into      the memoryImage
           Graphics2D g2d      = returnImage.createGraphics();
            g2d.setComposite(AlphaComposite.Src);
            g2d.drawImage(sourceImage,0,0,null);
            g2d.dispose();

            //and      return the memoryImage (the original image source is lost)
           return returnImage;
      }

      //prepares an [] of precreated Images
     //      blocks until all images      are loaded (or      ERRORed)
     //      each image is 'prepared' sequencially,      so      only 1
     //      image      loading thread      should be active at any      1 time
     //      im      not sure      if      this is better      or      worse      than loading all images      simultaneously
     //      though, looking at the way      MediaTracker does      it      - i think its better
     public Image[]      prepareImages(Image[] imagesToLoad)
      {
            for(int i =      0;      i < imagesToLoad.length; i++)
            {
                  prepareImage(imagesToLoad[i],-1,-1);
            }
            return imagesToLoad;
      }

      //prepares a single image (blocks until it is fully loaded
     public Image prepareImage(Image image)
      {
            return prepareImage(image,      -1,-1);
      }

      volatile      boolean loaded      = false;
      public synchronized Image prepareImage(Image      image, int width,      int height)
      {
            if(debug) System.out.print("prepareImage " +      width      + ","      + height      + " ");
            loaded =      false;
            //prepare image doesn't      always do the loading asynchronously!!
           if(!toolkit.prepareImage(image,width,height,this) && !loaded)
            {
                  //if(debug)      System.out.print("async      load");
                 try
                  {
                        this.wait();
                  }
                  catch(InterruptedException      e){}
            }
            if(!loaded)      System.out.println("ERROR:      Failed to load      image      " + image);
            return image;
      }

      public synchronized boolean imageUpdate(Image img,      int flags, int      x,      int y, int w, int      h)
      {
            if((flags &      ImageObserver.ALLBITS) == ImageObserver.ALLBITS)
            {
                  if(debug) System.out.println(generateDebugData("Complete",x,y,w,h,flags));
                  loaded =      true;
                  this.notifyAll();
                  return false;
            }
            if((flags &      ImageObserver.FRAMEBITS) == ImageObserver.FRAMEBITS)
            {
                  if(debug) System.out.println(generateDebugData("Framebits",x,y,w,h,flags));
                  loaded =      true;
                  this.notifyAll();
                  return false;
            }
            if((flags &      ImageObserver.ERROR)      ==      ImageObserver.ERROR)
            {
                  if(debug) System.out.println(generateDebugData("Error",x,y,w,h,flags));
                  this.notifyAll();
                  return false;
            }
            if((flags &      ImageObserver.ABORT)      ==      ImageObserver.ABORT)
            {
                  if(debug) System.out.println(generateDebugData("Abort",x,y,w,h,flags));
                  this.notifyAll();
                  return false;
            }
            if(debug) System.out.print(".");
            return true;
      }

      private String      generateDebugData(String flagString, int x, int      y,      int w, int h, int      flags)
      {
            StringBuffer sb =      new StringBuffer();
            sb.append(flagString).append(" x=").append(x)
                                                      .append(" y=").append(y)
                                                      .append(" width=").append(w)
                                                      .append(" height=").append(h)
                                                      .append(" flags=").append(flags);
            return sb.toString();
      }
}

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #13 - Posted 2003-03-14 18:38:49 »

and the ImageIO alternative (that avoids all the ImageObserver uglyness)...
(just knocked this together - it compiles but ive not run it Tongue)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
import java.awt.*;
import java.awt.image.*;
public class ImageLoader
{
   final GraphicsConfiguration gc;
   public ImageLoader(GraphicsConfiguration gc)
   {
      this.gc = gc;
   }

   public BufferedImage loadImage(String resource, int transparencyType) throws java.io.IOException
   {
      BufferedImage src = javax.imageio.ImageIO.read(getClass().getResource(resource));
      BufferedImage dst = gc.createCompatibleImage(src.getWidth(),src.getHeight(),transparencyType);
      Graphics2D g2d = dst.createGraphics();
      g2d.setComposite(AlphaComposite.Src);
      g2d.drawImage(src,0,0,null);
      g2d.dispose();
      return dst;
   }
}

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
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.

BurntPizza (10 views)
2014-09-19 03:14:18

Dwinin (29 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (79 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (60 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (30 views)
2014-09-07 01:12:14

Longarmx (36 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!