-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.4.0
-
generic
-
generic
Name: gm110360 Date: 11/29/2001
java version "1.4.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)
It seems intuitive and natural to expect that for any Shape s, the fragment:
g2.fill(s);
should be equivalent to:
g2.setClip(s);
g2.fillRectangle(...); // a rectangle larger than clipping shape.
this almost works, but the latter version is not properly anti-aliased.
The following is a demo program that will reproduce the problem. Run the
program with no arguments to see the smooth (anti-aliased) image, and with the
argument "-bug" to see the jagged image produced by setting the user clip.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.font.*;
/**
* Minimal test program to demonstrate a bug in the interaction of
* anti-aliasing and clipping in Java2D.
*/
public class FillShape extends JFrame {
/** If true then demonstrate the bug, otherwise do smooth rendering
*/
private boolean bugFlag = false;
private ShapePanel panel = new ShapePanel();
public FillShape(boolean bugFlag) {
super("Shape Test");
this.bugFlag = bugFlag;
setBounds(200,200,500,500);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp=getContentPane();
cp.add(panel, BorderLayout.CENTER);
}
public class ShapePanel extends JPanel {
public void paintComponent(Graphics g) {
// render background:
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// request anti-aliasing and high quality rendering:
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
Font f = new Font("SansSerif",
Font.BOLD | Font.ITALIC,
64);
GlyphVector gv = f.createGlyphVector(g2.getFontRenderContext(),
"Hello!");
Shape s = gv.getOutline();
AffineTransform tf = AffineTransform.getTranslateInstance(200,200);
s = tf.createTransformedShape(s);
g2.setPaint(Color.black);
if (bugFlag) {
// demonstrate the bug:
// setting the user clip to s here should have no effect
// on the result image, since the shape to be filled lies
// entirely within the user clip by virtue of being the
// SAME shape.
g2.setClip(s);
}
g2.fill(s);
}
}
public static void main(String args[]) {
boolean bug = (args.length > 0) && (args[0].equals("-bug"));
FillShape fs = new FillShape(bug);
fs.setVisible(true);
}
}
(Review ID: 136322)
======================================================================
- duplicates
-
JDK-4212743 Provide antialiased (i.e. soft edged) clipping
-
- Open
-