Drawing an image using sub-pixel level accuracy using Graphics2D

前端 未结 4 1227
北荒
北荒 2021-02-15 14:48

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 fram

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-15 15:28

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

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

提交回复
热议问题