gpt4 book ai didi

java - 边框带有圆角

转载 作者:行者123 更新时间:2023-12-01 11:28:51 27 4
gpt4 key购买 nike

这里我有一小段代码,用于使用 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();

最佳答案

除了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);

关于java - 边框带有圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30601869/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com