Sub types in Java

  • Thread starter Michael Kragh Pedersen
  • Start date
M

Michael Kragh Pedersen

Is it all possible for me to have a subtype in my variables? For
example, like this.

Color Colour
byte Red
byte Green
byte Blue

So that you can access Colour.Blue (the blue value of this colour),and
Colour to get the color itself (for use with fx JPanel and paint), and
so by calling Colour(255,255,255), Red, Blue and Green are set to the
values, and Colour is set to the color value itself. I know how to do
this in Visual Basic, but is it in any way possible for Java?

Michael K. P.
 
B

Bjorn Abelli

...
Is it all possible for me to have a subtype in my
variables? For example, like this.

Color Colour
byte Red
byte Green
byte Blue

I wouldn't call that "subtypes", as they are *attributes* of a Color...
So that you can access Colour.Blue (the blue value of this colour),and
Colour to get the color itself (for use with fx JPanel and paint), and so
by calling Colour(255,255,255), Red, Blue and Green are set to the values,
and Colour is set to the color value itself. I know how to do this in
Visual Basic, but is it in any way possible for Java?

I'm not exactly sure what you want to accomplish, but I'll try to elaborate
some on it.

Color is a class in the library, which have several methods implemented, of
which some can do what you want, e.g.:

class MyPanel extends JPanel
{
private Color color = new Color(0,0,0); // Default black

public Color getColor() { return color; }
public void setColor(Color newColor) { color = newColor; }

...

// ...and here you can use your color any way you want.
}

If you want the "red" of the color, you now just can call it in the
following manner:

...

MyPanel mp = new MyPanel(); // etc...
...

int red = mp.getColor().getRed();

...

....and the same goes for the others...

To set a new color, based on new rgb-values, just...

mp.setColor( new Color(255,255,255) );

....if you want those values...

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Color.html

You could possibly consider comp.lang.java.help for this type of basic
questions.


// Bjorn A
 
B

Boudewijn Dijkstra

Michael Kragh Pedersen said:
Is it all possible for me to have a subtype in my variables? For example,
like this.

Color Colour
byte Red
byte Green
byte Blue

So that you can access Colour.Blue (the blue value of this colour),and
Colour to get the color itself (for use with fx JPanel and paint), and so by
calling Colour(255,255,255), Red, Blue and Green are set to the values, and
Colour is set to the color value itself. I know how to do this in Visual
Basic, but is it in any way possible for Java?

If by "the colour value itself", you mean an int, then no. The class
java.awt.Color has methods for getting 8-bit colour values as wel as a
getRGB() method for getting them all in an int.
 
T

Tim Slattery

Michael Kragh Pedersen said:
Is it all possible for me to have a subtype in my variables? For
example, like this.

Color Colour
byte Red
byte Green
byte Blue

So that you can access Colour.Blue (the blue value of this colour),and
Colour to get the color itself (for use with fx JPanel and paint), and
so by calling Colour(255,255,255), Red, Blue and Green are set to the
values, and Colour is set to the color value itself. I know how to do
this in Visual Basic, but is it in any way possible for Java?

It looks to me like you're talking about properties of a class.
"Color" would be the class, and it would contain properties named Red,
Green and Blue:

class Color
{
int Red;
int Green;
int Blue;

....
}

You might want to have accessor functions for the properties (which
might restrict their values to something in the range 0-255), maybe a
method that combines the three values into a single integer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top