绘制图像的某些部分偏离角点?

2023-12-04

我正在使用某种精灵表来加载宇宙飞船。 Graphics.drawImage() 的文档指出参数是

boolean Graphics.drawImage(Image img,
   int dstx1, int dsty1, int dstx2, int dsty2,
   int srcx1, int srcy1, int srcx2, int srcy2,
   ImageObserver observer);

但是,文档中说 dstx1 和 dsty2 是左上角的坐标,当您使用 dstx2 和 dsty2 指定绘制的区域时,尺寸为(dstx2- dstx1)和(dsty2 - dsty1)。除非我误解了该函数的工作原理,否则它似乎只会加载角落处的图像部分。如何绘制图像中未连接到左角的部分,以绘制精灵表的不同部分?


public abstract boolean drawImage(Image img,
                                  int dx1,
                                  int dy1,
                                  int dx2,
                                  int dy2,
                                  int sx1,
                                  int sy1,
                                  int sx2,
                                  int sy2,
                                  ImageObserver observer)

参数:

  • img- 要绘制的指定图像。如果 img 为 null,则此方法不执行任何操作。
  • dx1- 目标矩形第一个角的 x 坐标。
  • dy1- 目标矩形第一个角的 y 坐标。
  • dx2- 目标矩形第二个角的 x 坐标。
  • dy2- 目标矩形第二个角的 y 坐标。
  • sx1- 源矩形第一个角的 x 坐标。
  • sy1- 源矩形第一个角的 y 坐标。
  • sx2- 源矩形第二个角的 x 坐标。
  • sy2- 源矩形第二个角的 y 坐标。
  • observer- 当更多图像被缩放和转换时要通知的对象。

the ds 是目的地,表示您希望在表面上绘制图像的位置。这ss 是源图像的坐标。对于两者来说,第一个角是左上角,第二个角是右下角。

因此,您可以将源图像视为要绘制的图像的焦点部分。您可以使用源坐标来确定要提取的矩形区域。

目标坐标布局源区域实际绘制的方式/位置。因此,如果您只想沿 x 轴移动绘制的图像,那么您只需移动dx1dx2。实际上,您可以使用坐标来调整绘制区域的大小,只需指定大于或小于源矩形的坐标矩形

            Source Image                     Destination panel
 sx1, sy1      
    +---------------+---------+        +-----------------------------+
    |               |         |        |                             |
    | region to     |         |        | dx1, dy1                    |
    |        draw   |         |        |    +----------+             |    
    |               |         |        |    |          |             |
    +---------------+         |        |    |          |             | 
    |           sx2, sy2      |        |    +----------+             |     
    |                         |        |            dx2, dy2         |
    |                         |        |                             |
    +-------------------------+        +-----------------------------+

这是一个正在运行的示例。

Disclaimer: My calculations may be a little off. Not sure if I'm capturing each every section of the image. Just whipped this up really quick.

enter image description here

enter image description here

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class NerdGirl extends JPanel {
    private static final int SPRITE_ROWS = 5;
    private static final int SPRITE_COLUMNS = 2;
    private static final int DELAY = 150;

    private int DIM_W;
    private int DIM_H;

    private int x1Src;
    private int y1Src;
    private int x2Src;
    private int y2Src;

    private BufferedImage img;

    public NerdGirl() {
        try {
            img = ImageIO.read(getClass().getResource("/resources/nerd-girl.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(NerdGirl.class.getName()).log(Level.SEVERE, null, ex);
        }
        DIM_W = img.getWidth() / SPRITE_ROWS;
        DIM_H = img.getHeight() / SPRITE_COLUMNS;
        x1Src = 0;
        y1Src = 0;
        x2Src = x1Src + DIM_W;
        y2Src = y1Src + DIM_H;
        Timer timer = new Timer(DELAY, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (x1Src >= img.getWidth() - DIM_H - 5) {  // 5 to take care of precision loss
                    x1Src = 0;
                    x2Src = x1Src + DIM_W;
                    if (y1Src >= DIM_H - 5) { // 5 to take care of precision loss
                        y1Src = 0;
                        y2Src = y1Src + DIM_H;
                    } else {
                        y1Src += DIM_H;
                        y2Src = y1Src + DIM_H;
                    }

                } else {
                    x1Src += DIM_W;
                    x2Src = x1Src + DIM_W;
                }

                repaint();
            }
        });
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), x1Src, y1Src, x2Src, y2Src, this);
    }

    @Override
    public Dimension getPreferredSize() {
        return (img == null) ? new Dimension(300, 300) : new Dimension(DIM_W, DIM_H);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new NerdGirl());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

绘制图像的某些部分偏离角点? 的相关文章

随机推荐