?
Solved

Changing the RGB vals of an awt.Color Object.

Posted on 2003-03-31
8
Medium Priority
?
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

771 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