This question is a followup to one I asked some time ago: Drawing a bordered path with sharp corners in Java
After experimentation, I've discovered some behavior which may be intended, or may be a bug, but either way is not desired by me. SSCCE:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CornerTest {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CornerTest window = new CornerTest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public CornerTest() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
protected void paintComponent(Graphics g) {
GeneralPath path;
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(15.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.setColor(Color.BLACK);
path = new GeneralPath();
path.moveTo(100, 100);
path.lineTo(200, 100);
path.lineTo(100, 50);
g2d.draw(path);
}
};
frame.setBackground(Color.CYAN);
frame.add(panel);
}
}
By taking the line path.lineTo(100, 50);
and playing with the second argument, we can change the angle of the path drawn. See the image below for various examples.
Basically, I am drawing a GeneralPath
with the angle slightly decreasing in each image. Eventually (in the bottom image) the angle reaches zero. In this case, the rounded "join" no longer is painted. This sort of makes sense, but it sort of doesn't. I'm not sure if it is intended. Interestingly, if you change the angle to slightly greater than 0 (i.e. by changing the line referenced above to path.lineTo(100, 99.999);
the rounded corner is drawn again.
In my application it is possible for paths to double back on themselves (i.e. create zero degree angles), and in this case it is much more appealing aesthetically to have the join round drawn in such a case. Is there any way I can hack the Java source to make this work?