6

I need a JavaFX program to set text to a random color and opacity I'm not sure on how to do it.

Here is a sample of my code:

Text text1 = new Text();
text1.setText("Java");
text1.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 22));
text1.setRotate(90);
gridpane.add(text1, 3, 1);
Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58
Sunny Dhillon
  • 81
  • 1
  • 1
  • 8

2 Answers2

12

You can use Math.random() to generate a Double in the range [0,1), so you need to do:

text.setOpacity(Math.random());

Color took a little more digging through the docs, but can be accomplished with:

text.setFill(Color.color(Math.random(), Math.random(), Math.random());

setFill comes from Shape, which Text inherits from. setFill takes a Paint, which Color is the simplest implementation of. Color.color(double, double, double) takes the rgb value with doubles in the range [0,1].

Learn how to navigate through the docs and you'll be able to find these sorts of things on your own quickly in the future!

Note: opacity/rgb color all take doubles of the range [0,1] where Math.random() produces in the range [0,1). If you're unfamiliar with this notation, this means Math.random() will NEVER produce 1, only a number smaller than 1 by possible accuracy. This means that you will never have a 100% fully opaque/r/g/b with this method but in reality you probably can't tell the difference, so it's better to use the less complicated method.

Note 2: javafx.scene.paint.Color#color actually provides a four-argument constructor that includes opacity, but I would recommend setting the opacity of the Text node itself as above rather than the opacity of the Paint.

CAD97
  • 1,160
  • 12
  • 29
  • Since I usually use Label and not Text, here's a link discussing when to use one or the other: http://stackoverflow.com/questions/24374867/label-and-text-differences-in-javafx – CAD97 Mar 01 '16 at 06:11
  • thanks for the tip opacity worked but color is still giving me a not suitable consturctor error. i had tried math.random before but didnt know you needed it 3 times – Sunny Dhillon Mar 01 '16 at 14:44
  • That was my mistake in reading the docs. The constructor takes (double r, double g, double b, double alpha). I've changed the answer to use `Color.color` now, which has an option for rgb. You need the three randoms because the method takes three values. – CAD97 Mar 01 '16 at 14:56
  • Awesome thanks I'll try it out once im home should work now – Sunny Dhillon Mar 01 '16 at 19:13
  • still wont run with text.setFill(Color.color(Math.random(), Math.random(), Math.random()); – Sunny Dhillon Mar 02 '16 at 04:13
  • Make sure you've imported `javafx.scene.paint.Color` and not some other Color class such as the one in `java.awt`, as only the JavaFX one will work in this case. It works for me as is. – CAD97 Mar 02 '16 at 04:23
0

Like this:

Text randomColorText(String txt) {
    Text t = new Text(txt);
    Random rng = new Random();
    int c = rng.nextInt();
    int r = c & 255;
    int g = (c >>> 8) & 255;
    int b = (c >>> 16) & 255;
    double op = (c >>> 24) / 255.0;
    t.setFill(Color.rgb(r, g, b, op));
    // or use only r,g,b above and set opacity of the Text shape: t.setOpacity(op);
    return t;
}

Note that the other answer that mentions how Random will never return a double == 1.0, says you won't get the full range of colours is wrong. Color RGB values do not have the same range as double - typically they end up as 8-bit values in the range 0-255 at some point, on some high end applications you might use 16-bits per channel. You will get the full range of colours using the doubles from Random.

You will note that I avoided calling into the random number generator multiple times for what is usually represented by a 32-bit value. (Micro-optimization: Calling nextInt does half the work of nextDouble, and we only need to call it once. I would typically save the instance of Random as a static variable rather than create one every time that method is called. java.util.Random is threeadsafe.)

swpalmer
  • 3,890
  • 2
  • 23
  • 31