Results 1 to 4 of 4
  1. #1
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default

    i got the code from the internet
    Any code that uses
    Java Code:
    image = new ImageIcon(image).getImage();
    is cr@p. I strongly recommend that you avoid the site where you got that code.

    im not that into java
    You can find all you need here: The Java™ Tutorials

    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

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default

    Oh, and use ImageIO to load your image.

    db

  4. #4
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    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

  1. Split a string into array every 11th char
    By isaac.flaum in forum New To Java
    Replies: 1
    Last Post: 02-17-2011, 06:01 AM
  2. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  3. bufferedImage
    By ranadav in forum New To Java
    Replies: 3
    Last Post: 06-05-2010, 03:22 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •