Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, how to store multiple integers, separated by comma, into one variable

In Java, I want to setup a variable that can store multiple integers (RGB values) and integers are separated by comma. For example, my current code is like

background(255,255,0);  // This changes my GUI background color to yellow.

The code I want is

type yellow = (Here goes the yellow's RGB value 255,255,0)
background(yellow);

My question is how to setup the variable yellow so it can replace the actual RGB values. Thank you.

like image 861
zym Avatar asked Jun 23 '26 19:06

zym


1 Answers

All the answers you got so far somehow solve the problem you described. But the problem is: they are not really helpful.

Never put information into strings and rely on parsing them. If you really want to do that; then you really do not need all the overhead of a statically compiled language like java. Then you are much better of using languages like python, ruby ... which allow you to handle "stringified" information much easier.

What you want to do is: learn about object orientation. So, you want to represent colors. Then model a class that represents a color. That could look somehow like:

public class Color {
  private final int r, g, b;
  public Color(int r, ... {
    this.r = r

and so on. Then you can write down a color like

Color yellow = new Color(255,255,0)

Heck; you could even start and declare constants representing useful predefined colors.

You know, like the existing Java AWT Color class.

So: please take the time to learn and understand the concepts I am talking about; and keep in mind: using strings for such purposes is absolutely bad.

like image 170
GhostCat Avatar answered Jun 25 '26 09:06

GhostCat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!