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

2014/3/10

Change Color of an Image

1
2
Pointifix Pointifix

2014/3/10

#
first i have my editor, i can choose a color and an object and then by clicking there it should be the object with the right color, without making hundrets of pictures with each color for on object
Pointifix Pointifix

2014/3/10

#
later on the map from the editor can be loaden to play on it, but thats another thing
bourne bourne

2014/3/10

#
Sounds like you need to change the color of an image each time you insert an object, not every act cycle?
bourne bourne

2014/3/10

#
Here are my test results with the RGBImageFilter, swapping one color for another, on one image 500x500. Results: Time to convert using RGBImageFilter: 0.018seconds. Time to convert using standard loops and setRGB: 0.038seconds.
Pointifix Pointifix

2014/3/10

#
the background is moving, cause the map is bigger then the world, so a dragable world, what causes that i need to draw them each cycle
Pointifix Pointifix

2014/3/10

#
18 ms is a considerable time i think
Pointifix Pointifix

2014/3/10

#
ah sry 500*500 so 18 ms for my whole screen, ok that could be possible how to use the rbgimagefilter correctly on gray item?
bourne bourne

2014/3/10

#
You really can predefine these images (in code from your gray ones, like when your World is constructed), and access the right image via a color and shape. There may be a lot of them, but using loops, method for swapping colors on an image, and structured arrays shouldn't be a problem.
Pointifix Pointifix

2014/3/10

#
k ;) i understood, if i want these i have to use rgbimagefilter, thanks for that information, i hope i will bring this project to the end so i can show it to you guys ;)
bourne bourne

2014/3/10

#
If you are going to predefine images, you don't need RGBImageFilter, you could just use simple pixel checking since it happens at initiation and doesn't take all day doing it. But if you are interested in RGBImageFilter here is my test code:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import greenfoot.Actor;
import greenfoot.GreenfootImage;
import java.awt.Color;
import java.awt.image.RGBImageFilter;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Graphics2D;
 
/**
 * TestActor
 *
 * @author Taylor Born
 * @version March 2014
 */
public class TestActor extends Actor
{
    private static final int RGB_RED = -65536;//Color.RED.getRGB();
    private static final int RGB_BLUE = -16776961;//Color.BLUE.getRGB();
     
    public TestActor()
    {
        GreenfootImage image = new GreenfootImage(500, 500);
        image.setColor(Color.RED);
        image.fill();
        setImage(image);
    }
     
    @Override
    public void act()
    {
        long startTime = System.currentTimeMillis();
         
        BufferedImage image = getImage().getAwtImage();           
        RGBImageFilter colorfilter = new RedBlueSwapFilter();             
        FilteredImageSource filteredImageSource = new FilteredImageSource(image.getSource(), colorfilter);            
        Image filteredImage = Toolkit.getDefaultToolkit().createImage(filteredImageSource);
        Graphics2D g = image.createGraphics();
        g.drawImage(filteredImage, 0, 0, null);
        g.dispose();
         
        long midTime = System.currentTimeMillis();
        System.out.println("Time to convert using RGBImageFilter: " + (midTime - startTime) / 1000d + "seconds.");
         
        for (int y = 0; y < image.getHeight(); y++)
            for (int x = 0; x < image.getWidth(); x++)
                switch (image.getRGB(x, y)) {
                    case RGB_RED: image.setRGB(x, y, RGB_BLUE); break;
                    case RGB_BLUE: image.setRGB(x, y, RGB_RED);
                }
         
        long endTime = System.currentTimeMillis();
        System.out.println("Time to convert using standard loops and setRGB: " + (endTime - midTime) / 1000d + "seconds.");
    }
     
    private static class RedBlueSwapFilter extends RGBImageFilter
    {
        public RedBlueSwapFilter() {
            // The filter's operation does not depend on the
            // pixel's location, so IndexColorModels can be
            // filtered directly.
            canFilterIndexColorModel = true;
        }
 
        @Override
        public int filterRGB(int x, int y, int rgb) {
            switch (rgb) {
                case RGB_RED: return RGB_BLUE;
                case RGB_BLUE: return RGB_RED;
                default: return rgb;
            }
        }
    }
}
Pointifix Pointifix

2014/3/10

#
k thanks always good to see examples, way fast you have written that ;) wow
bourne bourne

2014/3/10

#
I just noticed, the time for the loops and setRGB may be a little off, since line 44 is included in the time frame. Quick fix would place this just after that print statement:
1
midTime = System.currentTimeMillis();
Pointifix Pointifix

2014/3/10

#
:DDD you pro :) ill study rgbimagefilter class tomorrow a bit to get this colored: mom it looks like that if you are interessted: http://www.fotos-hochladen.net/view/unbenanntlbqzruj6tw.jpg just random placed all different objects in different directions ;) good night now ;)
You need to login to post a reply.
1
2