I am using the visual design editor in Netbeans. I have two sliders with corresponding JLabels. What I want is a rectangle that changes color when the slider is moved. How do I incorporate this with the designer’s generated code?
How do I paint my rectangle near the slider?
Colors in Java are composed by
RGBvalues.Red,GreenandBlueall haveintegervalues between 0 and 255. Netbeans visual editor allows you to set bothmaximumandminimumvalues for your slider.To add transparency to a color use
Red,Green,BlueandAlphaall have float values between 0.0 and 1.0.To change color when the slider is moved, add an event for when the mouse is dragged (you can do this using the visual editor) and use
slider.getValue()to retrieve the value.To paint the rectangle near the sliders (there should be at least 3 sliders, one for each rbg component) retrieve the slider position and dimensions with
slider.getBounds()(bounds containsxandycoordinates pluswidthandheight)Example (draws a rectangle 30×30 20 pixels right the sliders)