Hark, An Arc!

By David Wall, ITworld |  News

When you instanciate a Graphics2D object, you enable yourself to refer
to a bunch of interesting methods having to do with portions of
circles. A portion of a circle is called an arc, and the Graphics2D
object enables to you use arcs in three interesting ways:

* As open arcs, with no connections between their endpoints.
* As chorded arcs, with a chord (a straight line through what would
be the inside of the circle, were it complete) connecting the
arc's endpoints.
* As the basis for a pie (really a pie-slice, most of the time),
which is an arc with line segments connecting its endpoints to
the center of the circle that defines the arc.

Here's a little program illustrating the variations on the arc theme:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
class Arcs extends Frame {
public static void main(String arg[]) {
new Arcs();
}
Arcs() {
super("Arcs");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); } } );
setSize(350,330);
show();
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Arc2D chord = getArcChord();
Arc2D open = getArcOpen();
Arc2D pie = getArcPie();
AffineTransform at = new AffineTransform();

// First row
at.setToTranslation(10.0,30.0);
g2.setTransform(at);
g2.draw(pie);
at.setToTranslation(120.0,30.0);
g2.setTransform(at);
g2.draw(open);
at.setToTranslation(230.0,30.0);
g2.setTransform(at);
g2.draw(chord);

// Second row
at.setToTranslation(10.0,60.0);
g2.setTransform(at);
pie.setAngleExtent(-135.0);
g2.draw(pie);
at.setToTranslation(120.0,60.0);
g2.setTransform(at);
open.setAngleExtent(-135.0);
g2.draw(open);
at.setToTranslation(230.0,60.0);
g2.setTransform(at);
chord.setAngleExtent(-135.0);
g2.draw(chord);

// Third row
at.setToTranslation(10.0,190.0);
g2.setTransform(at);
pie.setAngleStart(45.0);
pie.setAngleExtent(270.0);
g2.draw(pie);
at.setToTranslation(120.0,190.0);
g2.setTransform(at);
open.setAngleStart(45.0);
open.setAngleExtent(270.0);
g2.draw(open);
at.setToTranslation(230.0,190.0);
g2.setTransform(at);
chord.setAngleStart(45.0);
chord.setAngleExtent(270.0);
g2.draw(chord);
}
private Arc2D getArcPie() {
Arc2D.Float arc = new Arc2D.Float(
0.0f,0.0f,
100.0f,100.0f,
0.0f,45.0f,
Arc2D.PIE);
return(arc);
}
private Arc2D getArcChord() {
Arc2D.Float arc = new Arc2D.Float(
0.0f,0.0f,
100.0f,100.0f,
0.0f,45.0f,
Arc2D.CHORD);
return(arc);
}
private Arc2D getArcOpen() {
Arc2D.Float arc = new Arc2D.Float(
0.0f,0.0f,
100.0f,100.0f,
0.0f,45.0f,
Arc2D.OPEN);
return(arc);
}
}

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question