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

2017/4/27

Image Manipulation New

danpost danpost

2017/4/28

#
I am not certain that the 'turn90' static method can actually change the dimensions of a BufferedImage. In other words, the dimensions of 'bi' cannot be changed as far as I can tell. As a GreenfootImage object, you can change the BufferedImage that backs it, allowing the size to change; but, once a BufferedImage object is created, I do not believe it is possible to alter its dimensions. So, for that method to work as you intended, you must return a new BuffferedImage object with the turned image and the calling statement must replace the reference to point to the new object. In other words, instead of calling the method like this:
1
Processor.turn90(bi); // where 'bi  points to a BufferedImage object
you need to do something like this:
1
bi = Processor.turn90(bi);
where the declaration line of 'turn90' is:
1
public static BufferedImage turn90(BufferedImage bi)
ironphoenix20 ironphoenix20

2017/4/28

#
ok but how do i set the buffered image to the method in the world class? there is no bi variable in the world class. and im unable to do image.getBufferedImage() = Processor.turn90(image.getBufferedImage());
danpost danpost

2017/4/28

#
I need to make a correction on the last line I gave above:
1
public static GreenfootImage turn90(BuffferedImage bi)
Then you can use:
1
image.setImage(Processor.turn90(image.getBufferedImage()));
Within the 'turn90' method, you need to create a new GreenfootImage object, modify its BufferedImage object and return the GreenfootImage object so that the 'image' actor can take on that new image.
ironphoenix20 ironphoenix20

2017/4/28

#
ok. ill try that.
ironphoenix20 ironphoenix20

2017/4/28

#
To clarify, I modify the greenfoot image of the bufferedimage taken is the parameter?
ironphoenix20 ironphoenix20

2017/4/30

#
Actually, the turn90 method is now flipping the image 180 degrees instead of 90 degrees. Can you find any errors in the method that would cause this?
danpost danpost

2017/4/30

#
ironphoenix20 wrote...
To clarify, I modify the greenfoot image of the bufferedimage taken is the parameter?
You are not modifying the GreenfootImage at all. You are creating a new Greenfootimage. The source BufferedImage backing the original GreenfootImage can acquired at any time (before the calling and passing it or after the call, passing the GreenfootImage itself).
ironphoenix20 wrote...
Actually, the turn90 method is now flipping the image 180 degrees instead of 90 degrees. Can you find any errors in the method that would cause this?
Actually, you have a lot of code that is not necessary. For example, your ImageHolder class can simply be this:
1
2
3
4
5
6
7
8
9
import greenfoot.*;
  
public class ImageHolder extends Actor
{
    public ImageHolder (String fileName)
    {
        setImage(fileName);
    }
}
The filename is not variable, so the file should always be present -- and if a FileNotFoundException is thrown, it just means you forgot to add the file to the 'images' folder or there is a naming conflict (both of which can be fixed). As a secondary inconsequence, any value returned from the 'openFile' method is ignored. The other thing is this: the image of the actor, or the buffered image backing it can still be acquired without the use of any fields or extra methods in the ImageHolder class. Now, as far as turning 180 instead of 90, please show the new 'turn90' method code as well as the code block in your Background class 'checkMouse' method under this 'if' condition:
1
if (Greenfoot.mouseClicked(turn90))
ironphoenix20 ironphoenix20

2017/4/30

#
actually, its turning 90 degrees now but how do I change the size of the world so the rotated image will fit inside it properly? Heres my code: turn90() method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public static GreenfootImage turn90(BufferedImage bi)
    {
        int xSize = bi.getWidth();
        int ySize = bi.getHeight();
 
        // Temp image, to store pixels as we reverse everything
        BufferedImage newBi = new BufferedImage (ySize, xSize, 3);
        int newX = newBi.getWidth();
        int newY = newBi.getHeight();
 
        for (int i = 0; i<newX; i++)
        {
            for (int j = 0; j<newY; j++)
            {
                int p = bi.getRGB(j, newX - 1 - i);
                newBi.setRGB(i,j, p);
            }
        }
 
        return createGreenfootImageFromBI(newBi);
 
        /*for (int i = 0; i<newX; i++)
        {
            for (int j = 0; j<newY; j++)
            {
                int b = newBi.getRGB(i,j);
                bi.setRGB(j,i,b);
            }
        }
        return createGreenfootImageFromBI(bi);*/
        //drawImage(turned,100,200);
    }
checkMouse for turn90():
1
2
3
4
5
6
7
8
9
10
11
else if (Greenfoot.mouseClicked(turn90)){
                if (redoCount>0)
                {
                    redoCount = 0;
                }
                image.setImage(Processor.turn90(image.getBufferedImage()));
                setBackground(Processor.turn90(image.getBufferedImage()));
                //removeObject(image);
                //addObject(turned, 330, 310);
                //undoCount++;
            }
danpost danpost

2017/5/1

#
ironphoenix20 wrote...
actually, its turning 90 degrees now but how do I change the size of the world so the rotated image will fit inside it properly? Heres my code: < Code Omitted >
If it is turning 90 degrees now, then the codes you just gave are not needed for fixing that. The image is already set to an actor -- why are you turning it again and setting the doubly turned image to the background of the world (I do not get it). Now, what is 'redoCount'? that was not in your Background class previously.
ironphoenix20 ironphoenix20

2017/5/1

#
redoCount is a separate thing that I'm trying to do, unrelated to turn90(). Ok, if I dont need to set image to background, then how do I change the size of the world so it matches image size? Or I else my rotated image looks funny and does not fit properly in the world as it has diff height and width from original image.
danpost danpost

2017/5/1

#
The size of a world is fixed when you create it. If you really want the size of world to change, then you need to create a new world with the new dimensions and set it active.
danpost danpost

2017/5/1

#
If the size of the image will be the size of the new world, then you could add another constructor to the world:
1
2
3
4
5
6
7
public Background(GreenfootImage bg)
{
    super(bg.getWidth(), bg.getHeight(), 1);
    image = bg;
    setBackground(bg);
    prepare();
}
Add a prepare method and cut/paste all codes in the original constructor that will be done regardless of first or re-sized world into it and call it from the original constructor as well.
ironphoenix20 ironphoenix20

2017/5/1

#
I can do this to create a new world each time I need to use the turn90() method?
danpost danpost

2017/5/1

#
ironphoenix20 wrote...
I can do this to create a new world each time I need to use the turn90() method?
Experiment with it.
You need to login to post a reply.