I want to set my JButton background color with half red and half blue. Can I do that in java swing without using graphics 2D? Please help me!
Asked
Active
Viewed 104 times
-2
-
1Please explain more -- half red text? background-color? something else? What have you researched? What have you tried? Please go through the [ask]. – Hovercraft Full Of Eels Oct 10 '19 at 16:39
-
2“Can I do that in java swing without using graphics 2D?” In a word: no. – VGR Oct 10 '19 at 19:01
1 Answers
0
You can do it by making it gradient.
buttonGraphics.setPaint(new GradientPaint(
new Point(0, 0),
Color.RED,
new Point(0, getWidth()),
Color.BLUE()));
buttonGraphics.fillRect(0, 0, getWidth(), getHeight());
buttonGraphics.dispose()

O. Collins
- 38
- 3
-
If you want it vertically gradient, use getHeight() instead of getWidth(). – O. Collins Oct 10 '19 at 17:09
-
Thanks Collins . Am sorry that my question was incomplete. Actually i want to change the background color of the button . Can i do it without using Graphics 2D. – Alonepmk Oct 10 '19 at 17:41
-