1

我发现绘制部分图像的唯一方法是使用此drawImage功能:

公共抽象布尔drawImage(图像img,int dx1,int dy1,int dx2,int dy2,int sx1,int sy1,int sx2,int sy2,ImageObserver观察者)

有没有一种方法可以绘制只需要目标坐标而不是矩形的图像部分?我想指定我的图像剪切,然后在一个坐标处绘制它,而不必担心目标矩形与我的剪切大小匹配。我发现这有更多的错误空间,就好像你弄错了它会挤压/拉伸你的图像以适应等等。另外,当你在屏幕上移动图像时,这意味着移动两组点而不是一个简单的左上角坐标点。

(我确定我看过有人这样做的视频,我只是不记得功能是什么)

4

1 回答 1

2

您可以从您的图像创建一个子图像,然后绘制它。

请查看BufferedImage APIgetSubImage()方法。这会将大图像中的剪切图像作为 BufferedImage 对象返回。然后你可以用一个更简单的drawImage(...)重载来绘制它,它只是占据这个位置。

例如:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class SubImageEg extends JPanel {
   // images from Wikimedia Commons
   // http://commons.wikimedia.org/wiki/Main_Page
   public static final String MAIN_IMG_PATH = "http://upload.wikimedia.org/wikipedia/commons/" +
        "thumb/4/43/Sarcophilus_harrisii_taranna.jpg/800px-Sarcophilus_harrisii_taranna.jpg";
   public static final String SECOND_IMG_PATH = "http://upload.wikimedia.org/wikipedia/commons/" +
        "thumb/f/f8/Soldering_a_0805.jpg/800px-Soldering_a_0805.jpg";
   public static final String[] IMAGE_PATHS = {MAIN_IMG_PATH, SECOND_IMG_PATH};
   private static final int SUB_X = 520;
   private static final int SUB_Y = 340;
   private static final int SUB_W = 150;
   private static final int SUB_H = 150;
   private static final int SEC_SUB_X = 400;
   private static final int SEC_SUB_Y = 200;

   private BufferedImage[] images = new BufferedImage[IMAGE_PATHS.length];
   private BufferedImage secondImgSubImg;

   public SubImageEg() {
      try {
         for (int i = 0; i < IMAGE_PATHS.length; i++) {
            images[i] = ImageIO.read(new URL(IMAGE_PATHS[i]));
         }

         secondImgSubImg = images[1].getSubimage(SUB_X, SUB_Y, SUB_W, SUB_H);
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (images[0] != null) {
         g.drawImage(images[0], 0, 0, this);
      }
      if (secondImgSubImg != null) {
         g.drawImage(secondImgSubImg, SEC_SUB_X, SEC_SUB_Y, this);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (images[0] != null) {
         return new Dimension(images[0].getWidth(), images[0].getHeight());
      } else {
         return super.getPreferredSize();
      }
   }

   private static void createAndShowGui() {
      SubImageEg mainPanel = new SubImageEg();

      JFrame frame = new JFrame("SubImageEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
于 2012-08-08T03:19:08.537 回答