-1

So I'm trying to take an BufferedImage and convert it into a type File, but ImageIO.write will only let me write the image to a file... I've looked at the source code and can't seem to find a way to convert an Image to a File, that isn't writing it to a file.

        // array of puzzle piece images
        BufferedImage puzzle_pieces[] = new BufferedImage[num_pieces];

        // make the pieces to the puzzle
        for (int current_row = 0; current_row < rows; current_row++)
        {
            for (int current_column = 0; current_column < columns; current_column++)
            {
                // initializes image array with pieces
                puzzle_pieces[count] = new BufferedImage(pieceWidth, pieceHeight, puzzle_image.getType());

                // draws the image for each piece  
                Graphics2D piece = puzzle_pieces[count++].createGraphics();  
                piece.drawImage(puzzle_image, 0, 0, pieceWidth, pieceHeight, pieceWidth * current_column, pieceHeight * current_row, 
                            pieceWidth * current_column + pieceWidth, pieceHeight * current_row + pieceHeight, null);  
                piece.dispose(); 
            }
        }

        // put pieces into array 
        for (int i = 0; i < num_pieces; i++) 
        {  
            //Put pieces into array of type File 
        } 
visc
  • 4,794
  • 6
  • 32
  • 58
  • What exactly are you trying to accomplish by converting the Image to a File? – kviiri Oct 27 '13 at 17:38
  • The type File only represents a path to a file. Converting an image to a File doesn't make much sense. If you want a File, then create it from scratch: `new File("image.png")` – JB Nizet Oct 27 '13 at 17:40
  • I split an image into pieces... but I want to load those pieces into my program not save them as a .jpeg. I have a function that saves the pieces to file. But, I need the pieces to remain files in my program, not "Image"s. – visc Oct 27 '13 at 17:45
  • That doesn't make any sense. A File is only a file path with added convenience features. – kviiri Oct 27 '13 at 17:55

1 Answers1

1

No File is needed here. Simply cut up the BufferedImage into separate images using getSubimage(x,y,w,h).

That technique is used in this answer to divide the image (loaded as a BufferedImage) into sub images for the various parts of the control. The mouse is hovering over the 'to right' control, showing the variant image used to show focus.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433