Image Color

I have an image that is, for example, a plain white circle. Is there a way for me to modify the color of the image? Thanks ahead of time.
[144 byte] By [Bobert.a] at [2007-10-3 5:22:08]
# 1
Not really. Either you have to use a PixelGrabber and change each pixel, or you have to repaint the image. It's pretty much like a canvas, once something's there, all you can do would be to paint over it or to start again. You can't remove or change.
CeciNEstPasUnProgrammeura at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for the response. I was hoping to use the image as a sort of 'model.' I would be drawing probably 200 of these modified images (the ones with different colors) on the screen.
Bobert.a at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 3

> Thanks for the response. I was hoping to use the

> image as a sort of 'model.' I would be drawing

> probably 200 of these modified images (the ones with

> different colors) on the screen.

That's exactly the wrong approach. Use the model as a model for the image. :) The image is just the View, a representation of something.

(MVC pattern is an interesting thing to look at, I highly recommend it.)

CeciNEstPasUnProgrammeura at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 4
The image I am using is actually a shaded circle. I wrote a method that draws a shaded circle pixel by pixel (according to the distance from the center), but I was afraid it would be too slow. Ill post if I can find it.
Bobert.a at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 5

Its probably very difficult to follow, but here it is:

public void fillShadedOval(Graphics g, int x, int y, int w, int h, Color c)

{

int centerX = x + (w/2);

int centerY = y + (h/2);

//calculates distance from the center to a side

double maxDistance = (w > h) ? w/2 : h/2;

double distance;

double percentage;

int alpha;

for(int i=x; i<=x+w; i++)

{

for(int k=y; k<=y+h; k++)

{

distance = Math.sqrt( Math.pow((centerX - i), 2) + Math.pow((centerY - k), 2) );

percentage = distance / maxDistance;

if(percentage > 1) percentage = 1;

alpha = (int)(percentage * c.getAlpha());

alpha = c.getAlpha() - alpha;

g.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha));

g.drawLine(i, k, i, k);

}

}

}

Bobert.a at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 6

> I would be drawing probably 200 of these modified images (the ones with different colors) on the screen.

You can create your own image so you aren't redrawing the image every time:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics2D g2d = image.createGraphics();

// using g2d you do your painting directly onto the image

g2d.dispose();

Now that you have an image you can use it in your program:

JLabel oval = new JLabel( new ImageIcon(image) );

camickra at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 7

Thanks for your reply. The thing is, the colors are constantly changing/fading from one color to another. There arent any 'set' colors that Im using. Im achieving the effect I want with the method I wrote, but it takes 15ish seconds to draw 255 of them on the screen. :( Does anyone have a solution to this?

Bobert.a at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 8
Caching the images of assorted colors and drawing them as needed might help. Kind of depends on how many different colors there are.
kablaira at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 9

You could also create a version with the "color" part transparent. For each circle, draw the color in the same shape as the circle then draw the image on top of it. The transparent part of the image will still show through to the color, the rest will produce the fade. Of course, that's assuming the fade isn't changing colors too and I don't know if that's even any faster than what you're doing.

kablaira at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 10
There are over 1000 colors that Im using.
Bobert.a at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 11
Thanks for the replies, by the way. This is going to be part of a game that Im trying to make, so I would need this to be drawn very quickly. Im open for any suggestions.
Bobert.a at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 12

> I would need this to be drawn very quickly

Which is why I suggested you pre draw the images and cache them.

You have two options;

a) draw the images on the fly (less memory, slower painting)

b) predraw the images and caches them (more memory, faster painting)

Given the generic question, we can't give any more specific detail.

camickra at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...
# 13

> > I would need this to be drawn very quickly

>

> Which is why I suggested you pre draw the images and

> cache them.

Well, I have around 1000 colors, so that would mean I would have around 1000 images. I was just wondering if there was an easier way. Thanks.

Bobert.a at 2007-7-14 23:29:09 > top of Java-index,Java Essentials,Java Programming...