使用 Graphics2D 使用亚像素级精度绘制图像 [英] Drawing an image using sub-pixel level accuracy using Graphics2D

查看:39
本文介绍了使用 Graphics2D 使用亚像素级精度绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试像在电子游戏中一样以正常速度在屏幕上绘制图像.

I am currently attempting to draw images on the screen at a regular rate like in a video game.

不幸的是,由于图像移动的速度,有些帧是相同的,因为图像还没有移动一个完整的像素.

Unfortunately, because of the rate at which the image is moving, some frames are identical because the image has not yet moved a full pixel.

有没有办法为 Graphics2D 提供 float 值以在屏幕上绘制图像,而不是 int 值?

Is there a way to provide float values to Graphics2D for on-screen position to draw the image, rather than int values?

最初这是我所做的:

BufferedImage srcImage = sprite.getImage ( );
Position imagePosition = ... ; //Defined elsewhere
g.drawImage ( srcImage, (int) imagePosition.getX(), (int) imagePosition.getY() );

这当然是阈值,所以图片不会在像素之间移动,而是从一个跳到下一个.

This of course thresholds, so the picture doesn't move between pixels, but skips from one to the next.

下一个方法是将油漆颜色设置为纹理并在指定位置绘制.不幸的是,这产生了不正确的结果,显示平铺而不是正确的抗锯齿.

The next method was to set the paint color to a texture instead and draw at a specified position. Unfortunately, this produced incorrect results that showed tiling rather than correct antialiasing.

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());

我应该怎么做才能达到Java中图像的亚像素渲染的预期效果?

What should I do to achieve the desired effect of subpixel rendering of an Image in Java?

推荐答案

你可以使用一个BufferedImageAffineTransform,绘制到缓冲图像,然后绘制缓冲图像到绘制事件中的组件.

You can use a BufferedImage and AffineTransform, draw to the buffered image, then draw the buffered image to the component in the paint event.

    /* overrides the paint method */
    @Override
    public void paint(Graphics g) {
        /* clear scene buffer */
        g2d.clearRect(0, 0, (int)width, (int)height);

        /* draw ball image to the memory image with transformed x/y double values */
        AffineTransform t = new AffineTransform();
        t.translate(ball.x, ball.y); // x/y set here, ball.x/y = double, ie: 10.33
        t.scale(1, 1); // scale = 1 
        g2d.drawImage(image, t, null);

        // draw the scene (double percision image) to the ui component
        g.drawImage(scene, 0, 0, this);
    }

在此处查看我的完整示例:http://pastebin.com/hSAkYWqM

Check my full example here: http://pastebin.com/hSAkYWqM

这篇关于使用 Graphics2D 使用亚像素级精度绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆