Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Changing the RGB vals of an awt.Color Object.

Posted on 2003-03-31
8
Medium Priority
?
341 Views
Last Modified: 2010-03-31
Anybody know how to change the RGB Values in an java.awt.Color Object without having to make a new Instance? This has been driving me crazy! Anybody know why the designers chose not to allow anybody to change the vals in the first place? Something to do with native access to the Colorspace, perhaps?
0
Comment
Question by:LouisP
8 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 8240196
No. Though I have never wanted to either. Perhaps if you explain *why* you want to do this, someone could suggest a reasonable way dealing with whatever issue is causing trouble for you.
0
 
LVL 4

Expert Comment

by:kylar
ID: 8241381
if you just want a trick to kludge it together, you can do something like this:

package java.awt;

import java.awt.*;

/**
 * User: tbyrne
 * Date: Mar 31, 2003
 * Time: 1:12:14 PM
 */
public class MutableColor extends Color{
   public MutableColor(int r, int g, int b, int a) {
      super(r,g,b,a);
   }
  public void setColor(int r, int g, int b, int a) {
        value = ((a & 0xFF) << 24) |
                ((r & 0xFF) << 16) |
                ((g & 0xFF) << 8)  |
                ((b & 0xFF) << 0);
  }
}

Note that it's in the java.awt package (necessary because of the way Color is created. A better way is to do this:

public class MutableColor extends Color{
 private Color m_myColor = null;
//then override all the methods of color like this:
public int getRed(){
  return m_myColor.getRed();
}
//the rest left out for brevity.

//then add in a change.

public void setRGBValues(int red, int green, int blue){
  m_myColor = new Color(red, green, blue);
}

}


Hope that helps,
Kylar
0
 

Author Comment

by:LouisP
ID: 8243633
I'll tell you *why* I want to do this imladris...
Say I've got 800 points I wish to display as a line with each point a different colour, and say that I have to do this at >100fps. That would entail 800*100 "new Color(...)" ops per second, and as all good Java Programmers know; unnecessary new's mean excessive memory allocation and THAT means that as soon as Mr. GC decides to do a Major Collection everything halts while it does this...and suppose that these colour values are being passed via a TCP network and the machine on the other side is pumping these values at a constant rate; whenever the GC decides to halt all other VM processes when it does a major collection the TCP queue might just decide to overflow and throw away information and we don't want that, now do we?

Thanks Kylar, I thought of overriding the Color class, but I think it does something funky in that static construct when it loads up native libs - I tried something similar and (in my class extension) the colour change wasn't affected when I changed the values of the member variables
"value", "frgbvalue", "fvalue" and "falpha".
Maybe I did something wrong - I'll try it with your code, k? :D
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:LouisP
ID: 8244269
You see, Kylar, I'm trying to avoid the new op, that last example there is just wrapping the new Color(...) operation which is what I don't want to do. I want to change the Color object without having to create a new instance (and therefore also the overhead of assigning a namespace to it) of it to do so.

The problem with overriding the Color class is that the member vars are all either private or protected, those that are protected I can probably override if I let the extension class reside within the java.awt package...however the classloader won't like that and will probably decide to throw a SecurityException.

What I did do was to try and hack the source itself and then recompile the sdk...but that in itself presented problems of its own.

I guess the real question here is why Sun decided against allowing developers to change the Color Values inside a Color Object and whether or not they will add such a feature in the future.
0
 

Author Comment

by:LouisP
ID: 8244336
Well, I guess there's no real answer to this question. I decided to keep a table of predefined Color Objects, that way the references stay pertinent :D
0
 
LVL 16

Expert Comment

by:imladris
ID: 8246493
I am certainly not aware of exactly why the Color internals are unchangeable. One assumes that pieces of the AWT package were easier to implement if that assumption could be made. And yes, I doubt that at this point, after 4 versions, such a feature would be added any time soon, or at all.

The tactic of having predefined Color objects is certainly a reasonable way out in the scenario you have described. I bet it can even be made faster than changing the Color object, which is all to the good, given the requirements you describe.

0
 

Expert Comment

by:CleanupPing
ID: 9058368
LouisP:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 9065514
User resolved; points refunded and question closed.

Netminder
EE Admin
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question