其他方式的子图像?

时间:2012-08-08 03:15:11

标签: java image awt

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

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

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

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

1 个答案:

答案 0 :(得分:2)

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

请查看BufferedImage API getSubImage()方法。这将从大图像中返回剪切图像作为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();
         }
      });
   }
}