Custom shaped JDialog needs smooth edges
The problem that I'm facing is that when I use the setWindowShape method from the com.sun.awt.AWTUtilities, is that the edges of my JDialog aren't smooth and really ugly.
I'm referring to his url where my journey started: Here. I did everything as they adviced in the article. The photo's at the beginning of the article show the non-smooth edges of the oval shaped frame.
So since there was no solution mentioned in the article about the ugly edges of non rectangular shapes, I started to google to look for a solution. This is what I come up with:
1) Anti-aliasing doesn't seems to work when I use the setWindowShape method. Since the method is used in the componentResized method, I tried to add a anti-aliasing renderinghint in the paint method of the JDialog, but that is not working.
Code:
fd.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent evt) {
raphics2D g2 = (Graphics2D) g.create();
Double width = 500.0;
Double height = width/2;
Double arc = width/10;
roundRectangle = new RoundRectangle2D.Double(0.0, 0.0, width, height, arc, arc);
rectangleArea = new Area(roundRectangle);
int x2Points[] = {(width.intValue() * 2) / 5, width.intValue() / 4, width.intValue() / 2};
int y2Points[] = {height.intValue(), (height.intValue() * 7) /5, height.intValue()};
GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, x2Points.length);
polyline.moveTo(x2Points[0], y2Points[0]);
for (int index = 1; index < x2Points.length; index++) {
polyline.lineTo(x2Points[index], y2Points[index]);
}
triangleArea = new Area(polyline);
rectangleArea.add(triangleArea);
AWTUtilities.setWindowShape(rectangleArea, shape);
}
});
public void paint(Graphics g){
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
}
2) So I tried a different approach, namely drawing the shape in the paint method and use the setWindowOpaque too false. This gives me a good result, smooth edges, but this gives me other problems:
- If I want to add a panel to the JDialog, the panel and it components aren't visible, or only when i click on them.
- When I click and the JDialog appears, it seems it repaints itselfs a couple of times and it flickkers which is also not good.
Code:
@Override
public void paint(Graphics g){
AWTUtilities.setWindowOpaque(this, false);
Graphics2D g2 = (Graphics2D) g.create();
Double width = 500.0;
Double height = width/2;
Double arc = width/10;
roundRectangle = new RoundRectangle2D.Double(0.0, 0.0, width, height, arc, arc);
rectangleArea = new Area(roundRectangle);
int x2Points[] = {(width.intValue() * 2) / 5, width.intValue() / 4, width.intValue() / 2};
int y2Points[] = {height.intValue(), (height.intValue() * 7) /5, height.intValue()};
GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, x2Points.length);
polyline.moveTo(x2Points[0], y2Points[0]);
for (int index = 1; index < x2Points.length; index++) {
polyline.lineTo(x2Points[index], y2Points[index]);
}
triangleArea = new Area(polyline);
rectangleArea.add(triangleArea);
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
g2.setPaint(Color.MAGENTA);
g2.fill(rectangleArea);
g2.draw(rectangleArea);
}
3) I found an article on the web, which suggests about soft clipping. The thing is that I don't know if it can be a solution, because I don't understand anything of the setComposite function of Graphics2D. Here is a link to the article: Here.
I hope anyone can help me out with this non-smooth edged JDialog. The shape is a textballoon form. I've been searching for a couple of days now, and haven't found a clear solution on the web.
Grtz