Hi, I just wanted to ask for some help considering images.

What I would like to do is take an image and then split it up into blocks of a certain dimension (say, 20x20). Then I would like to put these blocks into a 2D array. I tried searching for how to split images up, but got nowhere.

Any more details please ask

Recommended Answers

All 3 Replies

If nobody is replying because I haven't tried, i have. I just don't have any idea how to do it

hi

I had also this question before one month ago. but now it solved.
use Panel to stratch the image first is use BufferedImage to get the image then create one class which extends one JPanel and override one method named paintComponent to drow image on JPanel

the code is as follows

class NewClass extends JPanel implements Serializable {
    /**
     * This Class is a Inner Class which Extends JPanel here
     * paintComponent() method drows the Image on JPanel
     * using drawImage() method we drow BufferedImage image on JPanel causes the
     * Image Straching
     */
        Image image = null;
    public NewClass() {    
        
    }
    public NewClass(Image image) {
        this.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        this.image = image;
        repaint();
      
    }
    public void setImage(Image image){
        this.image = image;
         repaint();
       
    }
    public Image getImage(Image image){
        return image;
   }
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); //paint background
        if (image != null) { //there is a picture: draw it
            int height = this.getSize().height;
            int width = this.getSize().width;         
            g.drawImage(image,0,0, width, height, this);
               //g.drawImage(image, 0, 0, this); //original image size 
         }  //end if
    } //end paint
} //end class

in another JFreame Class call the methods of this JPanel class as follows

public class MyImage extends javax.swing.JInternalFrame
{

 public MyImage()
{
  imageLoad("D:\1.jpg");
//other code
}
   private void imageLoad(String tmprs1)
    {
        //This code is for showing image to JPanel dynemically by passing arguments 
        BufferedImage bimg = null;
        String path = tmprs1;
        
        try
        {
            // Track the image to completion.
                        bimg = ImageIO.read(new File(path));
            JPanel cont = (JPanel) getContentPane();
            imgobj = new NewClass(bimg);
            imgobj.setBounds(420,30, 150, 150);
            cont = new NewClass(bimg);
            imgobj.setImage(bimg);
            Image img = imgobj.getImage(bimg);
           // System.out.println(path);
            jPanel2.add(imgobj);//Tab Panel
            repaint();
            
        }
        catch(IIOException ex)
        {
            JOptionPane.showMessageDialog((Component)null, "Image Can't Read From System Please Check the Path in Database", "Error Loading Image",JOptionPane.ERROR_MESSAGE);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog((Component)null, "Unexpected Error Generated to Loading Image...", "Error Loading Image",JOptionPane.ERROR_MESSAGE);
        }
    }
    //Bellow code call paintComponent method every time when form contenet change
    @Override
    public void update(Graphics g)
    {
        paintComponent(g);
        paint(g);
    }
}

remember Make the NewClass inner to the MyImage Class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.