OK, I've been reading a lot online and I think I can't do this, but I'm really hoping someone has a clever workaround. What I have is a bunch of classes with Color attributes. I'm also storing the red, green, blue, alpha values as integer attributes in those classes because I am using them a lot. What I am doing now is this:
class someclass
{
Color color;
int red;
int green;
int blue;
int alpha;
public void setcolor (Color newcolor)
{
red = newcolor.getRed ();
green = newcolor.getGreen ();
blue = newcolor.getBlue ();
alpha = newcolor.getAlpha ();
color = new Color (red, green, blue, alpha);
}
}
I have a whole bunch of classes and a whole bunch of colors and what I'd like to do is have a single static function in some class that any of the other classes could call and pass the relevant variables, like you can in C++, like this:
public class aclass
{
public static Color CopyAttributes (Color newcolor, int red, int green, int blue, int alpha)
{
red = newcolor.getRed ();
green = newcolor.getGreen ();
blue = newcolor.getBlue ();
alpha = newcolor.getAlpha ();
return new Color (red, green, blue, alpha);
}
}
A function call from someclass could be:
color = aclass.CopyAttributes (newcolor, red, green, blue, alpha);
This won't work because you can't change red, green, blue, alpha inside the CopyAttributes function and have them change the calling class's attributes since int is a primitive type. However, that is what I WANT to do, so I'm looking for a workaround. Again, there are a whole bunch of classes and a whole bunch of Color variables and some of these color variables and their int counterparts aren't class variables, so I am constantly typing four lines of code like:
redval = acolor.getRed ();
greenval = acolor.getGreen ();
blueval = acolor.getBlue ();
alphaval = acolor.getAlpha ();
for a variety of variables. Am I simply stuck typing this all over the place or doing a big redesign or is there some way? And does the fact that I even WANT to do this mean that I am missing the whole point of Java and need to redesign? Thanks for any input.