[英]Border with rounded corners
Here i have small piece of code for getting rectangular box using AWT.But i want that border should be rounded corner.Can you please suggest me? 在这里,我有一小段代码可以使用AWT获取矩形框。但是我希望边框应该是圆角的。你能建议我吗?
int width = 150;
int height = 50;
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
Font font = new Font("Georgia", Font.BOLD, 18);
g2d.setFont(font);
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
GradientPaint gp = new GradientPaint(0, 0,
Color.decode("#24777D"), 0, height / 2, Color.decode("#008080"), true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);
g2d.setColor(new Color(255, 255, 255));
g2d.dispose();
Use drawRoundRect() method of Graphics2D class to paint border in addition to fillRoundRect() method.Follow API documentation.Here is a sample code for a try. 除了fillRoundRect()方法之外,还可以使用Graphics2D类的drawRoundRect()方法绘制边框。请遵循API文档。
JFrame f = new JFrame();
f.setLayout(null);
f.setDefaultCloseOperation(3);
f.setSize(500, 500);
f.setLocationRelativeTo(null);
JPanel p = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
//Draws the rounded opaque panel with borders.
graphics.setColor(getBackground());
//paint background
graphics.fillRoundRect(0, 0, width, height, 17, 17);
graphics.setColor(getForeground());
//paint border
graphics.drawRoundRect(0, 0, width, height, 17, 17);
}
};
p.setBounds(20,20,150,50);
p.setOpaque(false);
f.getContentPane().setBackground(Color.BLUE);
f.add(p);
f.setVisible(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.