Skip to main content

How to fill the area between two parallel arcs

2 replies [Last post]
raghavareddy
Offline
Joined: 2009-02-27
Points: 0

I am new to Java AWT.
Can any one help in how to fill the area between two parallel arcs ?

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
floogle
Offline
Joined: 2008-09-17
Points: 0

Thanks, kamre. I was looking for something similar, and this does the trick. Very succinct, perfect example. Kudos.

kamre
Offline
Joined: 2008-11-23
Points: 0

Something like this:

[code]
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Arc2D;
import java.awt.geom.Path2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class ParallelArcs extends JComponent {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// center of both arcs
double x = 100.0;
double y = 80.0;
// radii of arcs
double r1 = 50.0;
double r2 = 20.0;
// diameters of arcs
double d1 = 2 * r1;
double d2 = 2 * r2;
// start angle and extend
double start = -45;
double extent = 225;
// create arcs
Arc2D arc1 = new Arc2D.Double(x - r1, y - r1, d1, d1, start, extent,
Arc2D.OPEN);
Arc2D arc2 = new Arc2D.Double(x - r2, y - r2, d2, d2, start + extent,
-extent, Arc2D.OPEN);
// create path that consists of arcs
Path2D path2D = new Path2D.Double();
path2D.append(arc1, false);
path2D.append(arc2, true);
path2D.closePath();
// fill constructed path
g2d.fill(path2D);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Parallel Arcs");
frame.getContentPane().add(new ParallelArcs());
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
[/code]