本文介绍了绘画图像的某些部分从角偏移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了各种各样的精灵表在飞船加载。 Graphics.drawImage()的文档指出参数是

 布尔Graphics.drawImage(图片IMG,
   INT dstx1,诠释dsty1,诠释dstx2,INT dsty2,
   INT srcx1,诠释srcy1,诠释srcx2,INT srcy2,
   ImageObserver的观察者);

然而,文件说,dstx1和dsty2是左上角的坐标,当您使用dstx2和dsty2指定绘制的面积,尺寸为(dstx2- dstx1)和(dsty2 - dsty1)。除非我误解的功能是如何工作的,看来,这只是图像的加载部分形成的角落。一个人怎么能得出未连接到左侧角落,绘制一个精灵表的不同部分的图像的一部分?


解决方案

 公共抽象布尔的drawImage(Image img时,
                                  INT DX1,
                                  INT DY1,
                                  INT DX2,
                                  INT DY2,
                                  INT SX1,
                                  INT SY1,
                                  INT SX2,
                                  INT SY2,
                                  ImageObserver的观察者)

the ds are the destination, meaning where you want the image to be drawn on your surface. The ss are the coordinates of the source image. For both, the first corner being the upper left, and the second corner being the bottom right.

So you can think of the source image as like a focus section of the image to be drawn. You can use the source coordinates to determine the rectangle region to extract.

The destination coordinates layout how/where the the source region is actually drawn. So if you just want to move the drawn image along the x axis, then you just move the dx1 and the dx2. You can actually use the coordinates to resize the drawn region, by just specifying a coordinate rectangle larger or smaller than the source rectangle

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

Here's a running example.

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

这篇关于绘画图像的某些部分从角偏移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:03