Re: Rounded shapes
On 01/12/2011 04:19 AM, Roedy Green wrote:
I have been playing around with the Graphics2D drawing tools. I
discovered they behave very much like PostScript. I also discovered
OpenGL. It too seems to work much the same way.
I set myself the task of drawing a VGA connector. It has a rounded D
shaped shell. I wonder if there is something I have missed that would
make it easy to round the corners.
I discovered that BasicShape control of caps and joins just rounds to
half the radius of a line. I want a much bigger radius.
I discovered curveto but the docs don't in any way hint how to get the
two control points to get the effect you want.
By brute force I can compute the intersection points of circles and
lines. I can hardly imagine your average programmer willing to go to
all that work just to round off some shapes. Is there a tool in there
to form a broad smooth curve where two lines join at an angle?
I imagined some day a drawing package might exist where lines were
like physical pipes that, depending on the "material" they were made
of, would bend in various ways when pinned down at various locations
so that people without a math background could create without having
to compute all the magic connecting points.
In the process of writing this, I discovered some odd things about
Unicode. The circled, and black circled characters are split into
three regions. The characters are not contiguous. 0 is not even next
to 1. The other odd thing is the Dialog font renders different
circled numbers in radically different sizes. They are unusable. I
had to construct the glyphs programmatically.
Here is a 90 degree curved corner. The D connector corners are more
like 110/70 degrees but I think the same idea could work. You might
play with the quadTo() method as well.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class test2 extends JPanel {
final Path2D.Double path;
public test2() {
setPreferredSize(new Dimension(320,240));
path = new Path2D.Double();
path.moveTo(40.0,40.0);
path.lineTo(100.0,40.0);
path.curveTo(100.0,40.0,120.0,40.0,120.0,60.0);
path.lineTo(120.0,80.0);
}
public void paintComponent(Graphics g2d) {
Graphics2D g = (Graphics2D)g2d;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.BLACK);
g.draw(path);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new test2(),BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}
--
Knute Johnson
s/nospam/knute2011/