Hi,
I have a small Applet, where I draw some stuff using g.drawLine(…) (g is a java.awt.Graphic object). I have to draw huge amounts of line, and depending of their position, a Color is chosen -> Everytime I draw a Line, a g.setColor(…) is called, with the desired Color.
First problem : As I have about 40 000+ lines, about 40 000 (!) Colors are created at every repaint.
What I want to do, is to create a single Color Object, and then change it’s RGB values, avoiding a Constructor call. Example :
c.red = something;
c.green = something;
c.blue = something;
g.setColor(c);
or
c.setRGB(something);
c.setColor(c);
This would save me a lot of memory space, and will be faster (Object creation is too slow in this case, compared to simple field assignation).
Second problem : the java.awt.Color class does not have methods like setRed(int), setGreen, setBlue, there are no r, g, and b public fields.
I then wanted to subclass Color, in order to implement some methods to change the color value.
Third problem : The color value is stored in a private int field, which means that I can’t change it outside of Color… I can’t subclass Color, in order to implement my own setRGB method. The only way to change the color Value in a Color object is to recreat a Color object.
My question : Am I right ? Is there no way to change a color value without creating a new Color object ? Is there a way to do what I want ?
Thanks !