Apply/decorate graphicImage with rounded corners
------------------------------------------------
Key: JBSEAM-4633
URL:
https://jira.jboss.org/jira/browse/JBSEAM-4633
Project: Seam
Issue Type: Feature Request
Components: JSF Controls
Reporter: Kasper Sørensen
Priority: Minor
Fix For: 2.2.1.CR2, The future
It would be great with a graphic image transformation that adds rounded corners to an
image. Most JavaScript libraries can add rounded corners to DIV-elements and more, but not
IMG-elements. This is why it would be great to perform such rounding of edges on the
server side.
I've implemented this feature using this code:
{code}
@Override
public void applyTransform(Image image) throws IOException {
Integer height = image.getHeight();
Integer width = image.getWidth();
BufferedImage sourceImage = image.getBufferedImage();
BufferedImage targetImage;
Graphics2D graphics;
if (image.getContentType() == Type.IMAGE_JPEG) {
targetImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
graphics = targetImage.createGraphics();
graphics.setBackground(Color.WHITE);
} else {
// transparent background if supported
targetImage = sourceImage.createGraphics().getDeviceConfiguration()
.createCompatibleImage(width, height, Transparency.BITMASK);
graphics = targetImage.createGraphics();
graphics.setBackground(new Color(0, 0, 0, 0));
}
graphics.clearRect(0, 0, width, height);
RoundRectangle2D.Float roundedRectangle = new RoundRectangle2D.Float(0,
0, width, height, radius, radius);
graphics.setClip(roundedRectangle);
graphics.drawImage(sourceImage, 0, 0, null);
graphics.dispose();
image.setBufferedImage(targetImage);
}
private Integer radius = 20;
public void setRadius(Integer radius) {
this.radius = radius;
}
public Integer getRadius() {
return radius;
}
@Override
public Object saveState(FacesContext context) {
Object[] state = new Object[2];
state[0] = super.saveState(context);
state[1] = radius;
return state;
}
@Override
public void restoreState(FacesContext context, Object state) {
Object[] states = (Object[]) state;
super.restoreState(context, states[0]);
radius = (Integer) states[1];
}
{code}
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
https://jira.jboss.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira