2012-09-14 78 views
4

有沒有辦法將圖片拆分爲區域(現在是JLabel,但如果需要,我可以更改它)?
我在我的程序中使用swing,我有一個圖像(本例爲正方形),裏面有一些三角形,星星和梯形(可以是JPG,PNG等)。
這個想法是,用戶將點擊其中一個形狀,然後我將另一個小圖標放在用戶點擊的區域頂部。用戶可以點擊多個區域,但在一天結束時,我需要知道點擊了哪些形狀。將圖片拆分爲可點擊區域

似乎有可能嗎?

+0

你已經知道如何劃分大的圖像了變成精靈(每個獨立的形狀)?如果是這樣,它應該相當容易。只需使用子圖像作爲切換按鈕的「GridLayout」的圖標即可。所以答案是'是的,這似乎是可能的'。 –

回答

8

嗯看看我所提出:

這是我用於測試的圖像:

Original Image

圖像後已經分裂:

After Image splitting

這裏是源碼:

import java.awt.Graphics2D; 
import java.awt.GridLayout; 
import java.awt.Toolkit; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 

public class Test { 

    private JFrame frame; 
    private JLabel[] labels; 
    private static String imagePath = "c:/test.jpg"; 
    private final int rows = 3; //You should decide the values for rows and cols variables 
    private final int cols = 3; 
    private final int chunks = rows * cols; 
    private final int SPACING = 10;//spacing between split images 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Test().createAndShowUI(); 
      } 
     }); 
    } 

    private void createAndShowUI() { 
     frame = new JFrame("Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     initComponents(); 
     frame.setResizable(false); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    private void initComponents() { 

     BufferedImage[] imgs = getImages(); 

     //set contentpane layout for grid 
     frame.getContentPane().setLayout(new GridLayout(rows, cols, SPACING, SPACING)); 

     labels = new JLabel[imgs.length]; 

     //create JLabels with split images and add to frame contentPane 
     for (int i = 0; i < imgs.length; i++) { 
      labels[i] = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().createImage(imgs[i].getSource()))); 
      frame.getContentPane().add(labels[i]); 
     } 
    } 

    private BufferedImage[] getImages() { 
     File file = new File(imagePath); // I have bear.jpg in my working directory 
     FileInputStream fis = null; 
     try { 
      fis = new FileInputStream(file); 
     } catch (FileNotFoundException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     BufferedImage image = null; 
     try { 
      image = ImageIO.read(fis); //reading the image file 
     } catch (IOException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     int chunkWidth = image.getWidth()/cols; // determines the chunk width and height 
     int chunkHeight = image.getHeight()/rows; 
     int count = 0; 
     BufferedImage imgs[] = new BufferedImage[chunks]; //Image array to hold image chunks 
     for (int x = 0; x < rows; x++) { 
      for (int y = 0; y < cols; y++) { 
       //Initialize the image array with image chunks 
       imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType()); 

       // draws the image chunk 
       Graphics2D gr = imgs[count++].createGraphics(); 
       gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null); 
       gr.dispose(); 
      } 
     } 
     return imgs; 
    } 
} 

唯一的缺陷是,如果圖像是大這樣就可能導致問題的屏幕,這將通過使用圖像上getScaledInstance(int x,int y, int width, in height)一個簡單的圖像尺寸分離成塊來解決我還沒有檢查。

編輯/ UPDATE:

對不起,我錯過了一部分,如果在形狀的問題,看一看的Graphics2D/Graphicsdraw(Shape s)方法。

我閱讀此:

任何形狀對象可以被用來作爲限制將要呈現的繪圖區域的 部分剪切路徑。剪切路徑 是Graphics2D上下文的一部分;要設置剪輯屬性,請調用 Graphics2D.setClip並傳入定義要使用的剪輯 剪輯的形狀。

在這裏看到裁剪一條u]圖像的形狀:Clipping the Drawing Region

參考文獻:

+1

使用'drawImage()'+1不錯的選擇。 – trashgod

+0

我應該在哪裏改變它,所以這些圖像之間沒有差距?,對不起noob問題 –

4

您可以使用getSubImage()方法BufferedImage,說明herehere。該示例還使用JLabel,但您可以將Icon添加到可單擊的JButton。按鈕有幾種方法可以記住它的圖標的詳細信息:

  • 子類JButton並添加合適的字段。
  • 將一個客戶端屬性添加到父JComponent
  • 使用父Componentname財產。