Arc2D
is the abstract superclass for all objects that store a 2D arc defined by a framing rectangle, start angle, angular extent (length of the arc), and a closure type (Arc2D.OPEN
, Arc2D.CHORD
, or Arc2D.PIE
).
To constructs a new arc in double values, such as defining the specified location, size, angular extents, and closure type we can use the Arc2D.Double
static-inner class.
package org.kodejava.awt.geom;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Arc2D;
public class DrawArc extends JComponent {
public static void main(String[] args) {
JFrame frame = new JFrame("Draw Arc Demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new DrawArc());
frame.pack();
frame.setSize(new Dimension(420, 450));
frame.setVisible(true);
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(2.0f));
g2.setPaint(Color.RED);
g2.draw(new Arc2D.Double(50, 50, 300, 300, 0, 90, Arc2D.PIE));
g2.setPaint(Color.GREEN);
g2.draw(new Arc2D.Double(50, 50, 300, 300, 90, 90, Arc2D.PIE));
g2.setPaint(Color.BLUE);
g2.fill(new Arc2D.Double(50, 50, 300, 300, 180, 90, Arc2D.PIE));
}
}
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