Draw An Arc in Graphics

In this section, you will learn how to draw an arc in Graphics.
An arc of a circle is a segment of the circumference of the circle. To draw
an arc, class Arc2D is used. To give the stylistic and impressive way to the outline of arc, we have used BasicStroke class. The float value
passed into the constructor of Class BasicStroke shows the thickness of
the outline.
The method setPaint() paints the shape with specified color. The
method setStroke() sets the stroke settings. The method frame.setSize()
sets the size of frame and frame.show() shows the frame.
Here is the code of DrawAnArc.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
public class DrawAnArc extends JApplet {
BasicStroke basicStroke = new BasicStroke(5.0f);
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.red);
g2d.setStroke(basicStroke);
g2d.draw(new Arc2D.Double(5, 10, 150, 150, 60, 150,
Arc2D.OPEN));
}
public static void main(String s[]) {
JFrame frame = new JFrame("Draw Arc");
JApplet applet = new DrawAnArc();
frame.getContentPane().add("Center", applet);
frame.setSize(300, 250);
frame.show();
}
}
|
Download Source Code

Download Source code

|