日期:2014-05-20 浏览次数:20656 次
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
*
* @date 26/10/2012
*/
public class TestRoundCorner extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("Test Rectangle Round Corner ");
f.setContentPane(new TestRoundCorner());
f.setSize(800, 600);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
int padding = 50;
int r = 50;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rec = getBounds();
rec.x += padding;
rec.y += padding;
rec.width -= padding * 2;
rec.height -= padding * 2;
RoundRectangle2D roundRec = new RoundRectangle2D.Double(
rec.x - r, rec.y - r,
rec.width + r, rec.height + r, r, r);
@SuppressWarnings("unchecked")
Graphics2D gg = (Graphics2D)g;
gg.setColor(Color.BLUE);
Area area = new Area(rec);
area.intersect(new Area(roundRec));
gg.fill(area);
}
}