GUI - How to change the background Color of the Panel
So in this program, let's try to change the background color of a panel.
Now in this program, we utilize the AWT. It is possible to use the Swing but let's do that on anot
her problem.
Ok so now, we use a Scrollbar for this in order to freely adjust the values of our color using the RGB format(0-255).
Since the there is no ActionListener of Scrollbar, unlike Button, but the Scrollbar is a AdjustmetListener found at java.awt.event package with only one method signature and that is adjustmentValueChanged(AdjustmentEvent e).
We are to use the Scrollbar objects to accommodate the three colors(Red, Green, and Blue. The event for each color sin
ce each change would just read the current value for each Scrollbar and use it as basis for the new color of the Panel.
The Scrollbars are namely, sbRed, sbGreen, and sbBlue. We set the minimum value of the scrollbar to 0 and maximim to 255 since the RGB only accepts 0-255. To set the Scrollbar with the values given, we use its constructor Scrollbar(int orientation, int value, int visible, int minimum, int maximum).
For the orientation, 0 is horizontal and 1 is vertical.
For the value, it is for the initial value the scrollbar.
For the visible, it is for how should the scroller for the Scrollbar should appear on the Scrollbar. If set to 0, it would adjust automatically. It is actually based on the range of the value for minimum to maximum. If it is 255, then it would match the maximum and would cover the all of the Scrollbar. Just be careful since if the value is greater than the maximum range, then it would generate an Error.
For minimum, it is the minimum value and the maximum is for the maximum value.
To get the value of each Scrollbar, which is an integer value, is the getValue() method. Example, sbRed.getValue().
I created one class for the AdjustmentListener to make the event listeners uniform for each scrollbar since named AdjL. But in this implementation, I need to make it an inner class so that I could access the objects. Though it is possible to use an outside class, at least I'll be able to only use only one file.
The layout for the Scrollbars, the top is for the Red, the middle is for the Green, then the bottom bar is for the Blue.
Sample output.
0 comments:
Post a Comment