Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default Cut Java image into different pieces with an ImageBuffer

    Hey,

    I want to cut an image in java into several pieces with an ImageBuffer. When I try to do this with this code it cut's the image in 32 pieces (wich I want) but it doesn't change position and gives me 32-times te same image.

    I want 32 different pictograms from one imagefile (size of imagefile 800 x 400).

    This is what I made of it? (The java applet was just for testing)

    Java Code:
    public class GetImage {
        
        private String filename;
    
        public GetImage() {
    
        }
    
        public GetImage(String filename) {
            this.filename = filename;
        }
    
        public BufferedImage getImageNow() throws IOException {
           File file = new File(this.filename); 
           FileInputStream fis = new FileInputStream(file);
           BufferedImage image = ImageIO.read(fis);
           return image;
        }
    }
    Java Code:
    public class SplitImage {
    
        private int rows;
        private int columns;
        private int chuncks;
        private int chunckheigth;
        private int chunckwidth;
        private BufferedImage image;
        private BufferedImage images[];
    
        public SplitImage() {
    
        }
    
        public SplitImage(int rows, int columns, BufferedImage image) {
            this.row = rows;
            this.columns = columns;
            this.chuncks = this.rows * this.columns;
            this.image = image;
            this.chunckwith = 102;
            this.chunckheitgh = 97;
        }
    
        public BufferedImage[] splitImageNow() throws IOException{
            int count = 0;
            images = new BufferedImage[chuncks];
              for (int x = 0; x < rows; x++) {
                for (int y = 0; y < columns; y++) {
                    //Initialize the image array with image chunks
                    images[count] = image.getSubimage(0, 0, this.chunckwidth, this.chunckheigth);
                    Graphics2D gr = images[count++].createGraphics();
                    //gr.drawImage(image, 0, 0,chunckwidth,chunckheigth,null);
                    gr.drawImage(image, 0, 0, chunckwidth, chunckheigth, chunckwidth * y, chunckheigth * x, chunckwidth * y + chunckwidth, chunckheigth * x + chunckwidth, null);
                    gr.dispose();
                }
        }
             return images;
        }
    
        public void saveImages (BufferedImage[] images) throws IOException {
             for (int i = 0; i < images.length; i++) {
                ImageIO.write(images[i], "png", new File("img" + i + ".png"));
        }
    
        }
    }
    Java Code:
    public class Application_GUI extends Applet {
    
        GetImage getImage;
        SplitImage splitImage;
    
        public void init() {
            setSize(500,500);
            try
            {
            getImage = new GetImage("Pictograms.png");
            splitImage = new SplitImage(4,8,getImage.getImageNow());
            splitImage.saveImages(splitImage.splitImageNow());
            }
            catch (Exception e)
            {
                System.out.println("something has gone wrong");
            }
        }
    
    }
    Attached Thumbnails Attached Thumbnails Cut Java image into different pieces with an ImageBuffer-pictogramex3bjava2012.png  

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Cut Java image into different pieces with an ImageBuffer

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Cut Java image into different pieces with an ImageBuffer

    1. Read the API for getSubImage(...) and you'll find that it shares the same data array as the original image. Any drawing you do to on the subimage is going to affect the original image.
    2. Read the API for the overload of Graphics#drawImage that you use (the one that takes 8 int arguments). The significance of each of those arguments is clearly defined.

    But, if you're dealing with an image that doesn't change, why not just use subimages obtained with relevant coordinates? There's really no point in getting a subimage from the top-left corner and then trying to draw something over it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 07-28-2011, 08:48 AM
  2. Battleship Game - Pieces lay on top of eachother.
    By lolerskates2695 in forum New To Java
    Replies: 7
    Last Post: 04-25-2011, 01:05 AM
  3. Replies: 12
    Last Post: 04-14-2011, 01:58 PM
  4. Triangle Areas - Please help before I smash my laptop into pieces!
    By Atia of the julii in forum New To Java
    Replies: 67
    Last Post: 11-10-2010, 05:40 PM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 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
  •