Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (488)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 Images from a file using a Grid  (Read 2280 times)
0 Members and 1 Guest are viewing this topic.
Offline GeissT

Junior Newbie





« Posted 2011-09-25 05:22:07 »

Hey all,

You will most likely have seen the graphics files for Minecraft by our own Markus Persson. If not then you should take a look at a texture pack for Minecraft. I really want to know how to load the individual images from those squares, I know how to load a single image but not how to take certain pixels from that image.

Thanks in advance,

GeissT
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #1 - Posted 2011-09-25 07:20:04 »

Are you referring to like a spritesheet?

or more like an image/pixel representation of a 2d environment/map




"Experience is what you get when you did not get what you wanted"
Offline GeissT

Junior Newbie





« Reply #2 - Posted 2011-09-25 08:43:55 »

Yeah,

Like a spritesheet I think

Sprite | Sprite | Sprite
----------------------
Sprite | Sprite | Sprite
----------------------
Sprite | Sprite | Sprite

Something like the above ^^^^^

EDIT: http://jobu1233.blog.com/files/2010/11/terrain.png

I want something like this to be loaded
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-09-25 09:11:55 »

Using java.awt.image.BufferedImage, you could use the Graphics's 10-param drawImage method:
1  
drawImage(Image i, int destinationX, int destinationY, int destinationX2, int destinationY2, int sourceX, int sourceY, int sourceX2, int sourceY2, ImageObserver io);


Example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
BufferedImage original = //get entire image

//create new image using a square's width and height
BufferedImage square = new BufferedImage(width,height);
Graphics g = square.getGraphics();

//draw that square into the new image
g.drawImage(original, 0, 0, width, height, x, y, x+width, y+height, null);

g.dispose();

Offline GeissT

Junior Newbie





« Reply #4 - Posted 2011-09-25 09:18:58 »

Wow!

Thanks, I am going to test this now :DDDDD

Thank you very much!
Offline GeissT

Junior Newbie





« Reply #5 - Posted 2011-09-25 09:36:35 »

I am new to Java Graphics and it seems I have some errors.

Would you please be able to give a full example with the example original file being sprites.png?

Thanks in advance,

GeissT
Offline Z-Man
« Reply #6 - Posted 2011-09-25 19:21:24 »

Using java.awt.image.BufferedImage, you could use the Graphics's 10-param drawImage method:
Or you could use getSubimage() to chop it up into little images. I just dislike the 10-param drawImage(), seems exsessive when you could just chop the image up and draw each one by itself with the nice 4-param drawImage(Image img, int x, int y, ImageObserver observer)  method.
Online ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-09-25 20:24:47 »

@GeissT
What errors do you have?

@Z-Man
And how are you supposed to chop a 3x3 image using that 4-param method?

EDIT: Wow! I never knew about that getSubimage method! @OP: instead of my method use:

1  
BufferedImage square = original.getSubimage(x,y,width,height);

Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #8 - Posted 2011-09-25 20:39:03 »

Worth noting that Images returned from getSubimage share the parent Image's DataBuffer.

So if you had a mostly blank sprite sheet, created subimages for all the sprite frames, and then disposed of the parent image expecting the memory it used to be recovered by the GC, you'd be mistaken.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Z-Man
« Reply #9 - Posted 2011-09-25 21:44:23 »

Worth noting that Images returned from getSubimage share the parent Image's DataBuffer.

So if you had a mostly blank sprite sheet, created subimages for all the sprite frames, and then disposed of the parent image expecting the memory it used to be recovered by the GC, you'd be mistaken.
Huh I've never had problems with this, but since the data can still be accessed through the subimage wouldn't the GC leave it alone? If it's a problem you could do something like this to make sure it's ok.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public static BufferedImage[][] parseImage(BufferedImage src, int subWidth, int subHeight)
{
   BufferedImage[][] imgs = new BufferedImage[src.getWidth() / subWidth][src.getHeight() / subHeight];
   GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
   
   for(int x = 0; x < src.getWidth() / subWidth; x++)
   {
      for(int y = 0; y < src.getHeight() / subHeight; y++)
      {
         BufferedImage subImage = gc.createCompatibleImage(subWidth, subHeight, src.getTransparency());
         BufferedImage subSrc = src.getSubimage(x * subWidth, y * subHeight, subWidth, subHeight);
         
         subImage.createGraphics().drawImage(subSrc, 0, 0, null);
         imgs[x][y] = subImage;
      }
   }
   
   return imgs;
}


EDIT: To simplify my example and expand upon ra4kings:
1  
2  
3  
4  
BufferedImage square = new BufferedImage(width, height, original.getType());
BufferedImage subImage = original.getSubimage(x, y, width, height);
Graphics2D g = square.createGraphics();
g.drawImage(subImage, 0, 0, null);
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #10 - Posted 2011-09-25 22:10:18 »

Worth noting that Images returned from getSubimage share the parent Image's DataBuffer.

So if you had a mostly blank sprite sheet, created subimages for all the sprite frames, and then disposed of the parent image expecting the memory it used to be recovered by the GC, you'd be mistaken.
Yeah, it's a nasty gotcha. The worst side-effect IMHO is that because it's just a reference into a much bigger slice of data the drawing speed may be *much* worse than if you create a new image of the right size and draw the portion into it.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Online ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2011-09-26 00:28:50 »

Yep either that or just use the 10-param method for splitting.

However, I remember a while back there was a thread that showed how drawImage somehow alters the image a bit. Someone showed that drawImage doesn't draw the image bit by bit but does some changes.

Offline GeissT

Junior Newbie





« Reply #12 - Posted 2011-09-26 14:33:51 »

Thanks everyone,

When I get around to it, I will check the getSubImage method out Smiley !!

GeissT
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.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (69 views)
2014-08-11 02:49:23

BurntPizza (46 views)
2014-08-09 21:09:32

BurntPizza (37 views)
2014-08-08 02:01:56
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!