This site requires JavaScript, please enable it in your browser!
Greenfoot back
TheStrangeOne
TheStrangeOne wrote ...

2018/1/19

A little question about BufferedImages

TheStrangeOne TheStrangeOne

2018/1/19

#
Hi!I am working on a project ,a sliding puzzle and i am trying to figure out how to crop the pieces from an image using BufferedImages but i can't figure out how to use them properly if someone would be kind one to explain how those BufferedImages work and how to succesfully crop a certain image i would be grateful.
danpost danpost

2018/1/19

#
I do not believe that use of the BufferedImage class will allow conversion to HTML5 javascript (at least not currently because it is a java.awt class). However, you do not need to use that class at all. Use the 'drawImage' method of the GreenfootImage class to create the cropped images. The following sample code illustrates how to accomplish it:
GreenfootImage fullImage = new GreenfooImage("bg.jpg"); // the full image
int blockSize = 80; // for pieces of size 80x80 pixels
int size = 4; // for a 4x4 grid using 15 pieces
GreenfootImage[] images = new GreenfootImage[size*size-1];
for (int i=0; i<images.length; i++)
{
    images[i] = new GreenfootImage(blockSize, blockSize);
    images[i].drawImage(fullImage, -80*(i%size), -80*(i/size));
}
These images can then be assigned to the actors for the pieces.
CxVercility CxVercility

2018/1/20

#
Why is it i%size once and i/size the other time o.o
danpost danpost

2018/1/20

#
CxVercility wrote...
Why is it i%size once and i/size the other time o.o
Because one, i%size, goes from 0 to 3 four times as 'i' goes from 0 to 8 and after every 4 values of 'i' the other one, i/size, increments by one. The values for (i%size, i/size) as i goes from 0 to 14, in order, are (0, 0), (1, 0), (2, 0), (3, 0), (0, 1), (1, 1), (2, 1), (3, 1), (0, 2), (1, 2), (2, 2), (3, 2), (0, 3), (1, 3) and (2, 3).
CxVercility CxVercility

2018/1/20

#
danpost wrote...
CxVercility wrote...
Why is it i%size once and i/size the other time o.o
Because one, i%size, goes from 0 to 3 four times as 'i' goes from 0 to 8 and after every 4 values of 'i' the other one, i/size, increments by one. The values for (i%size, i/size) as i goes from 0 to 14, in order, are (0, 0), (1, 0), (2, 0), (3, 0), (0, 1), (1, 1), (2, 1), (3, 1), (0, 2), (1, 2), (2, 2), (3, 2), (0, 3), (1, 3) and (2, 3).
Damn, thats smart.
You need to login to post a reply.