2

I've been reading about sub-pixel accuracy but could not find a way to do this.

Q: How can I move a BufferedImage across a Canvas at non-integer speeds without stuttering?

I can logically understand how the process happens - slight resizing with Anti-Aliasing, but I cannot recreate this in Java. (I've had experience with Flash where such an effect is created by default on most visual objects.)

Note: I've submitted a question about the subject before, but after searching for further information I still couldn't implement what I learned.

If it is not too much trouble, a simple example would be very appreciated.

Thanks in advance.

Edit: This is how I understood the effect can be achieved, though it doesn't work for me:

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(new Rectangle2D.Double(x, y, width, height));

There is no visible change between double and similar int values at the x & y fields, and when I try to create very slow movement (system time based, for example) I see the rectangle doing nothing but moving a single pixel every now and then.

Community
  • 1
  • 1
Acidic
  • 6,154
  • 12
  • 46
  • 80
  • Why `Canvas` ? and nothing from `javax.swing`? - that may be the reason. Did you see [this](http://today.java.net/today/2006/02/23/SmoothAnimationDemo/com/sun/animation/SmoothAnimation.java) example? – ring bearer Sep 16 '11 at 02:51

2 Answers2

2

ButtonUITest offers a side-by-side comparison of the effect.

image

On many platforms, it's possible to zoom the display to see more. On Mac OS X, --8 works, as does /Developer/Applications/Graphics Tools/Pixie.app. On other platforms supporting Java, Zoom is a handy tool for examining anti-aliased pixels.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you for your answer, but unfortunately I cannot see how this is related to my question. I can create AA effects on shapes, but I cannot create smooth movement when an object is moved a non-whole amount of pixels every frame. – Acidic Sep 16 '11 at 02:00
  • Ah, you were looking for an animated demo. You'd have to move the `Rectangle2D` coordinates by a fraction of a pixel in each frame to see the effect. – trashgod Sep 16 '11 at 02:09
  • Which is exactly what I have done, yet there is no visible effect! – Acidic Sep 16 '11 at 02:22
  • I hope this is what you asked. Here is my render method: http://notepad.cc/xukoscu57. WIDTH and HEIGHT are the size of my content pane. (JPanel in a JFrame) This code basically moves a rectangle back and forth **very** slowly and it is easily visible that every few moments it simply jumps 1 pixel forward without any visible difference in between. – Acidic Sep 16 '11 at 03:09
  • No, this fragment is not an [sscce](http://sscce.org/), but it reveals that you are drawing with integer coordinates. Use [`draw(Shape s)`](http://download.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#draw%28java.awt.Shape%29). – trashgod Sep 16 '11 at 04:51
  • I'm not sure if pictures this nature should be distibuted to the public forums +1 – mKorbel Sep 16 '11 at 06:16
1

Try also setting the following rendering hint (as well as anti-aliasing):

g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
cgull
  • 1,407
  • 1
  • 11
  • 18
  • This seems to make quite an impact, but I can't tell whether it's for better or for worse. I can definitely still detect stuttering though. – Acidic Sep 16 '11 at 02:35