The RoundRectangle2D
class defines a rectangle with rounded corners defined by a location (x,y)
, dimension (w x h)
, and the width and height of an arc with which to round the corners.
The RoundRectangle2D.Double
class constructs a RoundRectangle2D
from the specified values in double, including the location, width and the arch of the round rectangle.
package org.kodejava.awt.geom;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
public class DrawRoundRectangle extends JComponent {
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);
}
@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));
}
}
Here is the result you’ll get when you run the snippet:
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024