To do this, you must create your own component.
Set the paintComponent method to JPanel, and inside the paintComponent method draw (i.e. fill) the rounded rectangle 2D in gray:
RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(x, y, w, h, 10, 10); g.fill(roundedRectangle);
(The last two values determine the curvature. Play around until you get what you want)
Now move x, y and reduce the width and height so that when drawing the next rectangle it is inside the gray rectangle, Set the graphic color to blue, then do the following:
RoundRectangle2D roundedRectangle2 = new RoundRectangle2D.Float(x + 5, y + 5, w - 10, h - 10, 10, 10); g.fill(roundedRectangle2);
You will also need to add text. To add text, x and y are required. The exact x and y positions can be difficult to compute, so you may need FontMetrics to get some more information about the rectangular shape of the string. Fontmetrics has methods like stringWidth () and getHeight () to help you determine what your x and y should be.
g.drawString("Click Me", x, y);
Finally, you need to have a mouse movement listener on the panel. The listener needs to find when the mouse is over the button, and then redraw the component.
Your rectangle can be transferred to the shape object, and a calculation can be made whether the mouse is in shape, For example:
shape.contains(x,y)
If it contains, change the color and then call repaint () or updateUI () on the panel.
Note. Your color object must be stored as a class level field in the class so that it can be changed with the mouse.
Hope this helps!