0
public static BufferedImage split(BufferedImage img) {
   BufferedImage pic = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
   Graphics g = pic.getGraphics();

    int width = 2000/2;
    int height = 2000/2;
    int imageW = pic.getWidth();
    int imageH = pic.getHeight();
                // Tile the image to fill our area.
    for (int x = 0; x < width; x += imageW) {
        for (int y = 0; y < height; y += imageH) {
            g.drawImage(pic, x, y, null);
        }
    }  
    return pic ;  
}  

the point of the code is to create a tile of 2x2 of the image (same image reproduce at a smaller size in a 2x2 grid). i want to updated pic so i can print it onto a jpanel. all i get is black image. can someone tell me whats wrong with the code. or tell me how to create a better piece of code.

john
  • 3
  • 4
  • `g.drawImage(pic, x, y, null);`? You're painting `pic` to itself? – MadProgrammer Nov 26 '15 at 23:11
  • I'm not sure I understand what you're trying to do. You take an image, you create another of image of the same size, you (want to) paint the first image to the second image and given intervals (of 1:1)...?? Shouldn't the second image be bigger/smaller or the original image be scaled? – MadProgrammer Nov 26 '15 at 23:13
  • I want to make four smaller images of the original and place it in a grid of 2x2 that is the same size as the original image. – john Nov 27 '15 at 09:49

1 Answers1

0

I want to make four smaller images of the original and place it in a grid of 2x2 that is the same size as the original image

Something like...

public static BufferedImage split(BufferedImage img) {
    BufferedImage pic = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics g = pic.getGraphics();

    int width = pic.getWidth() / 4;
    int height = pic.getHeight() / 4;

    Image scaled = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);

    // Tile the image to fill our area.
    for (int x = 0; x < pic.getWidth(); x += width) {
        for (int y = 0; y < pic.getHeight(); y += height) {
            g.drawImage(scaled, x, y, null);
        }
    }
    g.dispose();
    return pic;
}

You may also like to have a look at Java: maintaining aspect ratio of JPanel background image and Quality of Image after resize very low -- Java for more details about how you can improve the scaling algorithm

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • yes exactly. can you please explain what happens in this code. please. i really want to know. – john Nov 27 '15 at 10:23
  • It's really very basic. It creates a new `BufferedImage` at the same size of the `img`. It then creates a scaled instance of the original, reducing it by 0.25%. It then paints the scaled instance onto the `pic` – MadProgrammer Nov 27 '15 at 10:28
  • Image scaled = img.getScaledInstance(width, height, Image.SCALE_SMOOTH); – john Nov 27 '15 at 10:33
  • okay. Thank you very much for your help. it was very appreciated. – john Nov 27 '15 at 10:40