如何在Java 2D中绘制圆形矩形?
在RoundRectangle2D类定义与由位置界定圆角的矩形(x,y),尺寸(w x h),和与舍入到角部的圆弧的宽度和高度。
的RoundRectangle2D.Double类构造一个RoundRectangle2D从在双指定的值,包括位置,宽度和圆角矩形的拱。
package org.nhooo.example.geom;import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
public class DrawRoundRectangle extends JComponent {
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.RED);
g2.setStroke(new BasicStroke(2.0f));
double x = 50;
double y = 50;
double w = x + 250;
double h = y + 100;
g2.draw(new RoundRectangle2D.Double(x, y, w, h, 50, 50));
}
public static void main(String[] args) {
JFrame frame = new JFrame("Rounded Rectangle Demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new DrawRoundRectangle(), BorderLayout.CENTER);
frame.pack();
frame.setSize(420, 300);
frame.setVisible(true);
}
}
运行代码段
以上是 如何在Java 2D中绘制圆形矩形? 的全部内容, 来源链接: utcz.com/z/347079.html