PNG supports a transparent alpha channel, but JPG does not. So, for JPG you would have to also pick a color to paint the "invisible" part of the rectangle for the rounded corners.
There is a class java.awt.geom.RoundRectangle2D
available to do this:
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
RoundRectangle2D rr = new RoundRectangle2D.Float(50, 50, 200, 100, 10, 10);
g2d.draw(rr);
}
The Float() method of the class RoundRectangle2D takes six arguments:
- The first two represent the location of the upper left corner.
- Arguments 3 and 4 represent the width and height of the rounded rectangle.
- The last two arguments represent the width and height of the arc drawn in the
corners.
So, draw a rounded rectangle that will just contain the image you want to have rounded corners and then either overlay or use a mask to get the desired effect.