I'm currently trying to draw images on the screen at normal speed, such as in a video game.
Unfortunately, due to the speed with which the image moves, some frames are identical because the image has not yet moved the full pixel.
Is there a way to provide float values for Graphics2D to display positions on the screen, not int values?
Initially, this is what I did:
BufferedImage srcImage = sprite.getImage ( ); Position imagePosition = ... ; //Defined elsewhere g.drawImage ( srcImage, (int) imagePosition.getX(), (int) imagePosition.getY() );
These, of course, are threshold values, so the image does not move between pixels, but is skipped from one to another.
The next method was to set the color of the paint on the texture and draw in the specified position. Unfortunately, this led to incorrect results, which showed that smoothing is not alternating, but correct smoothing.
g.setRenderingHint ( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); BufferedImage srcImage = sprite.getImage ( ); g.setPaint ( new TexturePaint ( srcImage, new Rectangle2D.Float ( 0, 0, srcImage.getWidth ( ), srcImage.getHeight ( ) ) ) ); AffineTransform xform = new AffineTransform ( ); xform.setToIdentity ( ); xform.translate ( onScreenPos.getX ( ), onScreenPos.getY ( ) ); g.transform ( xform ); g.fillRect(0, 0, srcImage.getWidth(), srcImage.getHeight());
What should I do to achieve the desired effect of sub-pixel image rendering in Java?