Results 1 to 4 of 4
Thread: Split BufferedImage into Array
- 08-10-2011, 08:57 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 71
- Rep Power
- 0
Split BufferedImage into Array
hi guys, i tried to split a buffered image into an Array.. but it fails
Java Code:private BufferedImage imageTileset; private BufferedImage[] images; imageTileset = ImageToBufferedImage("http://www.java-forums.org/images/0"); images = splitImage(imageTileset, 16, 16); public BufferedImage[] splitImage(BufferedImage img, int cols, int rows) { int w = img.getWidth()/cols; int h = img.getHeight()/rows; int num = 0; BufferedImage imgs[] = new BufferedImage[w*h]; for(int y = 0; y < rows; y++) { for(int x = 0; x < cols; x++) { imgs[num] = new BufferedImage(w, h, img.getType()); // => ArrayIndex 4 doesnt work; // Tell the graphics to draw only one block of the image Graphics2D g = imgs[num].createGraphics(); g.drawImage(img, 0, 0, w, h, w*x, h*y, w*x+w, h*y+h, null); g.dispose(); num++; } } return imgs; } public BufferedImage ImageToBufferedImage(String imagepath) { BufferedImage img = toBufferedImage(new ImageIcon(imagepath + ".png").getImage()); return img; } public BufferedImage toBufferedImage(Image image) { image = new ImageIcon(image).getImage(); BufferedImage bimage = new BufferedImage(image.getWidth(null), image .getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics g = bimage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return bimage; }
at the line which i wrote the mistake in, it says:
ArrayIndex Bounds out of exception: 4....
i dont know why.. i got the code from the internet and im not that into java , that i can find it :/
help me please
if you need to know: my image is 1024x1024 and a .png...Last edited by Coold0wn; 08-10-2011 at 09:00 AM.
- 08-10-2011, 09:11 AM #2
Any code that usesi got the code from the internetis cr@p. I strongly recommend that you avoid the site where you got that code.Java Code:image = new ImageIcon(image).getImage();
You can find all you need here: The Java™ Tutorialsim not that into java
Do you know how to declare and use arrays in Java? There's a section in the tutorial about that. Refer to it if you need to.
BufferedImage has a method getSubImage(...) which could be useful. Moreover, since subImages share the same data array with the image they are split from, a tiling program using these images would be less memory intensive than one which constructs new images for each tile. That could improve performance when tiling a large image.
I recommend you try to do this yourself with reference to the tutorials and the API. Feel free to post a question here any time you get stuck, preferably with a SSCCE, which will help you get better help sooner.
db
- 08-10-2011, 09:40 AM #3
Oh, and use ImageIO to load your image.
db
- 08-10-2011, 11:10 AM #4
Member
- Join Date
- Aug 2011
- Posts
- 71
- Rep Power
- 0
oh thx for these informations :)
well i took this one, because its like the only website in the internet which manages to give a whole code, and not a summery of methods and attributes..
most sites said: you can also split images.
and thats a big problem to me , cause i just cant invent sth like this :P
well.. im nearly finished with my program already.. but i think ill start over to learn everything exactly and right with the website you gave me..
are you sure that everything is right with this tutorial? cause i do like always get an error if i try to do it like them... :/
edit: arrays are the last problem... theyre pretty easy, but these images in java... stealing my nerves^^
its like some chosen ones understand how they are working , and all the others ... can copy paste..^^
edit2: Wow... i work with BlueJ - i changed nothing, closed the program, reopened the project, started it ... and it worked... Oo
but i have another question, if you would be so kind and answert it:
How can i make Buttons, that only are a picture.. because my buttons are not 64x64 px squares, but they have a border which is about 100x80.. and that looks stupid..
i heard , you can do it with images, and with a mouse listener so it somehow knows at which picture you are at and then does different things if you click...
do you know anything about that?Last edited by Coold0wn; 08-10-2011 at 11:33 AM.
Similar Threads
-
Split a string into array every 11th char
By isaac.flaum in forum New To JavaReplies: 1Last Post: 02-17-2011, 06:01 AM -
Split a String with split()--Help
By danilson in forum New To JavaReplies: 7Last Post: 11-19-2010, 04:08 PM -
bufferedImage
By ranadav in forum New To JavaReplies: 3Last Post: 06-05-2010, 03:22 PM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks