Re: [JAVA2D] Is there a bug with clipping and affine transforms?

2009-01-20 Thread java2d
First, note what the API doc says about setClip(Shape clip):

[b]Not all objects that implement the Shape interface can be used to set the 
clip. The only Shape objects that are guaranteed to be supported are Shape 
objects that are obtained via the getClip method and via Rectangle objects.[/b] 

Keeping that in mind, your render method might look as follows:

[code]
public void render(final Graphics2D g2d) {
g2d.setColor(Color.RED);
final Rectangle r = new Rectangle(0, 0, 80, 80);
Stroke stroke = g2d.getStroke();
Shape clip = stroke.createStrokedShape(r);
clip = clip.getBounds(); // Not getBounds2D, because we need a
 // plain Rectangle
g2d.setClip(clip);
g2d.draw(r);
}
[/code]

However, I guess that, even if there is no guarantee, you might want to set a 
clip as a circle, or any other shape that you can think of.

Maybe you can transform the clip shape and make it slightly wider so that in 
the end it occupies complete pixels, instead of parts of pixels. Of course you 
should take into account the transform of the Graphics object. I think this is 
not that easy.

As an alternative, you might set a stroke witdh of 2. Then the chances that 
half of the stroke will be within the clip will be somewhat greater.

Piet
[Message sent by forum member 'pietblok' (pietblok)]

http://forums.java.net/jive/thread.jspa?messageID=327009

===
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
lists...@java.sun.com and include in the body of the message help.


Re: [JAVA2D] Is there a bug with clipping and affine transforms?

2009-01-20 Thread Jim Graham

jav...@javadesktop.org wrote:

First, note what the API doc says about setClip(Shape clip):

[b]Not all objects that implement the Shape interface can be used to set the clip. The only Shape objects that are guaranteed to be supported are Shape objects that are obtained via the getClip method and via Rectangle objects.[/b] 


That's a red herring.  It was in there for the 1.1 days when we 
introduced Shape, but before we could handle arbitrary geometry in the 
rendering routines.  It should have been removed in Java 2, but we 
didn't notice it until recently.  It lives on in one of those the next 
time someone is in that file, please remember to delete this line limbo 
phases, but we have little reason to edit that file any more...


...jim

===
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
lists...@java.sun.com and include in the body of the message help.


Re: [JAVA2D] Is there a bug with clipping and affine transforms?

2009-01-20 Thread Jim Graham
Note that, in Java 2D, a shape cannot know its outline design because 
that depends on another graphics attribute - the Stroke object.  The 
default implementation of that interface is a BasicStroke object with a 
line width of 1.0f, but that is just the default for the Graphics 
objects you get from images and components.  A different implementation 
of Stroke may behave entirely differently and a graphics retrieved from 
a different context may have a different default setting.


Thus, to get a result that includes the Stroke you have to consult the 
Stroke object that will be used to render the object.  It implements a 
createStrokedShape() method, part of the Stroke interface, to return 
the shape that defines the area to be painted when a given Shape is 
stroked by that object.


If you just want the bounds with the stroke included (as opposed to the 
actual shape that will be stroked), and if you know that you only ever 
plan to use a BasicStroke object with a simple line width, then it might 
be simpler to just take the bounds of the shape, pad it by linewidth / 
2 on every side, and use that.  Unfortunately this approximation does 
have its limitations - the default JOIN attribute on BasicStroke is a 
MITER stroke which can extend more than lw/2 units away from the shape, 
depending on how sharp any of the angles in the shape are.  To predict 
if this comes into play you should read up on the subject of MITER joins 
and the miterlimit attribute.  Or, if you are using ROUND or BUTT joins 
then the lw/2 padding is sufficient for their operation.


Another potential caveat is that the default setting for the 
STROKE_CONTROL rendering hint, which controls tweaking an outline for 
aesthetic purposes, is STROKE_NORMALIZE which allows the rendering 
algorithms to tweak the outline by up to a pixel to increase the chance 
of consistent line thicknesses.  Setting that hint to STROKE_PURE 
requests that no such normalization occur during rendering...


...jim

jav...@javadesktop.org wrote:

Hi Piet,

Thank you for your input and suggestions.

I also discovered that the problem can be avoided by expanding the clip region 
but this example with the rectangle is only the very simplest case of the 
overall problem I am facing.  I have found that this problem applies with 
shapes of all kinds and complexity and expanding the clip region is not so easy 
given that the shape is not a simple rectangle.

How can I solve this problem in a more general sense with more complex shapes?
[Message sent by forum member 'qu0ll' (qu0ll)]

http://forums.java.net/jive/thread.jspa?messageID=326980

===
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
lists...@java.sun.com and include in the body of the message help.


===
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
lists...@java.sun.com and include in the body of the message help.


Re: [JAVA2D] Is there a bug with clipping and affine transforms?

2009-01-19 Thread java2d
Not sure I'm clear on what's going on, you saying that you get lines in the 
middle of the rectangle that are no longer part of the clip? 

Can you put a minimal demonstration case?
[Message sent by forum member 'fred34' (fred34)]

http://forums.java.net/jive/thread.jspa?messageID=326918

===
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
lists...@java.sun.com and include in the body of the message help.


Re: [JAVA2D] Is there a bug with clipping and affine transforms?

2009-01-19 Thread java2d
Could this be responsible:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4265778
[Message sent by forum member 'mthornton' (mthornton)]

http://forums.java.net/jive/thread.jspa?messageID=326920

===
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
lists...@java.sun.com and include in the body of the message help.


Re: [JAVA2D] Is there a bug with clipping and affine transforms?

2009-01-19 Thread java2d
What I'm saying is that one or more of the lines that make up the rectangle 
will disappear at certain scale levels.  Try the following and you'll see that 
at smaller sizes some lines of the rectangle disappear.

[code]
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ClippingBug extends JFrame {

private class MyPanel extends JPanel {

@Override
public void paintComponent(final Graphics g) {
final Graphics2D g2d = (Graphics2D)g;
final double scaleFactorX = this.getWidth() / 100.0;
final double scaleFactorY = this.getHeight() / 100.0;
final AffineTransform at =
new 
AffineTransform(AffineTransform.getScaleInstance(scaleFactorX, scaleFactorY));
final int width = this.getWidth();
final int height = this.getHeight();
final int tx = 10 + (int)((width - 100.0 * 
scaleFactorX) / 2.0 / scaleFactorX);
final int ty = 10 + (int)((height - 100.0 * 
scaleFactorY) / 2.0 / scaleFactorY);
g2d.transform(at);
g2d.translate(tx, ty);
this.render(g2d);
}

public void render(final Graphics2D g2d) {
g2d.setColor(Color.RED);
final Rectangle r = new Rectangle(0, 0, 80, 80);
g2d.setClip(r);
g2d.draw(r);
}
}

public ClippingBug() {
this.setLayout(new BorderLayout());
final MyPanel panel = new MyPanel();
this.add(panel, BorderLayout.CENTER);
panel.setBackground(Color.WHITE);
this.pack();
this.setSize(500, 520);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
}

public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {

public void run() {
new ClippingBug().setVisible(true);
}
});
}
}
[/code]
[Message sent by forum member 'qu0ll' (qu0ll)]

http://forums.java.net/jive/thread.jspa?messageID=326921

===
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
lists...@java.sun.com and include in the body of the message help.


Re: [JAVA2D] Is there a bug with clipping and affine transforms?

2009-01-19 Thread java2d
No, it doesn't look like it.  I am not using particularly large dimensions but 
thanks for the suggestion.
[Message sent by forum member 'qu0ll' (qu0ll)]

http://forums.java.net/jive/thread.jspa?messageID=326922

===
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
lists...@java.sun.com and include in the body of the message help.


Re: [JAVA2D] Is there a bug with clipping and affine transforms?

2009-01-19 Thread java2d
I think the problem is that you, ever so little, are trying to paint outside 
the clipping area. Depending on the stroke width, lines are drawn on both sides 
of the specified line. If you alter your render method as follows:

[code]
public void render(final Graphics2D g2d) {
g2d.setColor(Color.RED);
final Rectangle r = new Rectangle(0, 0, 80, 80);
Rectangle clip = new Rectangle(-1, -1, 82, 82);
g2d.setClip(clip);
g2d.draw(r);
}
[/code]

you will see that the lines are always drawn correctly. In your version, only 
half of the width of the line can be drawn. When scaling very small and due to 
rounding problems, the drawing may disappear entirely.

Piet
[Message sent by forum member 'pietblok' (pietblok)]

http://forums.java.net/jive/thread.jspa?messageID=326978

===
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
lists...@java.sun.com and include in the body of the message help.


Re: [JAVA2D] Is there a bug with clipping and affine transforms?

2009-01-19 Thread java2d
Hi Piet,

Thank you for your input and suggestions.

I also discovered that the problem can be avoided by expanding the clip region 
but this example with the rectangle is only the very simplest case of the 
overall problem I am facing.  I have found that this problem applies with 
shapes of all kinds and complexity and expanding the clip region is not so easy 
given that the shape is not a simple rectangle.

How can I solve this problem in a more general sense with more complex shapes?
[Message sent by forum member 'qu0ll' (qu0ll)]

http://forums.java.net/jive/thread.jspa?messageID=326980

===
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
lists...@java.sun.com and include in the body of the message help.