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

2014/2/18

Help With Image

JetLennit JetLennit

2014/2/18

#
Hi there, I am using something to split up my sprite-sheet into individual java.awt.images but i have run into a problem. Not all the images are the same size, does anyone know how to remove the excess transparency in said images automatically? Thanks in advance
bourne bourne

2014/2/18

#
So like to trim as much transparency from all sides of an image? I wrote a method before to do this, I can dig it up if you want
JetLennit JetLennit

2014/2/18

#
I would appreciate it if you could
bourne bourne

2014/2/18

#
private GreenfootImage trim(GreenfootImage t)
    {
        int firstRow;
 outer: for (firstRow = 0; firstRow < t.getHeight(); firstRow++)
            for (int c = 0; c < t.getWidth(); c++)
                if (t.getColorAt(c, firstRow).getAlpha() != 0)
                    break outer;
        int lastRow;
 outer: for (lastRow = t.getHeight() - 1; lastRow > 0; lastRow--)
            for (int c = 0; c < t.getWidth(); c++)
                if (t.getColorAt(c, lastRow).getAlpha() != 0)
                    break outer;
        int firstCol;
 outer: for (firstCol = 0; firstCol < t.getWidth(); firstCol++)
            for (int r = 0; r < t.getHeight(); r++)
                if (t.getColorAt(firstCol, r).getAlpha() != 0)
                    break outer;
        int lastCol;
 outer: for (lastCol = t.getWidth() - 1; lastCol > 0; lastCol--)
            for (int r = 0; r < t.getHeight(); r++)
                if (t.getColorAt(lastCol, r).getAlpha() != 0)
                    break outer;
        GreenfootImage newT = new GreenfootImage(lastCol - firstCol + 1, lastRow - firstRow + 1);
        newT.drawImage(t, -firstCol, -firstRow);
        return newT;
    }
JetLennit JetLennit

2014/2/18

#
Thanks! But do you know how this could be translated into a regular java.awt.Image?
bourne bourne

2014/2/18

#
A GreenfootImage has method getAwtImage which gives you a BufferedImage, which has a method getScaledInstance to return a java.awt.Image. But if you just working with Image, Image has a getGraphics method that you can work with
JetLennit JetLennit

2014/2/18

#
The problem is I am not using greenfoot,
bourne bourne

2014/2/18

#
Could you use BufferedImage instead of Image?
JetLennit JetLennit

2014/2/18

#
Yeah
bourne bourne

2014/2/18

#
private static BufferedImage trim(BufferedImage t)
    {
        int firstRow;
 outer: for (firstRow = 0; firstRow < t.getHeight(); firstRow++)
            for (int c = 0; c < t.getWidth(); c++)
                if (t.getRGB(c, firstRow) != 0)
                    break outer;
        int lastRow;
 outer: for (lastRow = t.getHeight() - 1; lastRow > 0; lastRow--)
            for (int c = 0; c < t.getWidth(); c++)
                if (t.getRGB(c, lastRow) != 0)
                    break outer;
        int firstCol;
 outer: for (firstCol = 0; firstCol < t.getWidth(); firstCol++)
            for (int r = 0; r < t.getHeight(); r++)
                if (t.getRGB(firstCol, r) != 0)
                    break outer;
        int lastCol;
 outer: for (lastCol = t.getWidth() - 1; lastCol > 0; lastCol--)
            for (int r = 0; r < t.getHeight(); r++)
                if (t.getRGB(lastCol, r) != 0)
                    break outer;
        BufferedImage newT = new BufferedImage(lastCol - firstCol + 1, lastRow - firstRow + 1, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = newT.createGraphics();
        g.drawImage(t, null, -firstCol, -firstRow);
        g.dispose();
        return newT;
    }
JetLennit JetLennit

2014/2/18

#
Thank you so much!!!
You need to login to post a reply.