Question

I there a way to get the color that is behind the slider portion of a JSlider? For Example, I made a program (see Image Below) that creates a gradient and requires the user to pick a color with a JSlider. Is there a way to get the color that the JSlider stops at?

enter image description here

Was it helpful?

Solution

If you can get a point on the slider you could use.

Color color = robot.getPixelColor((int)coord.getX(), (int)coord.getX())

The pseudo code would look like

 Robot robot = new Robot();
 int xValue = slider.getLocationOnScreen().x + (int)(slider.getValue()/(double)(slider.getMaximum()-slider.getMinimum()) * slider.getWidth() +.5);//Calculates distance of the sliders' position).
 int yValue = slider.getLocationOnScreen().y + slider.getHeight()/2;//Calculates a y-value to extract the pixel from. 
 Color color = robot.getPixelColor(xValue,yValue);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top