2013-11-23 79 views
6

我不知道這是否是正確的地方問,但我想知道是否有類似於Android上的9個補丁圖像,但對於Java,如Oracle,PC Java。我所有的谷歌搜索都只顯示了我的android,因爲人們稱之爲Java,但它並不完全相同。針對Java的9補丁類圖像縮放?

我已經找到了LibGdx,但是對於我所需要的單一九段補丁功能來說它是相當高的。

九塊圖像是一個具有'9'區域的圖像,通常在側壁和中心區域被拉伸以適應屏幕時,角部「未縮放」。 來自Android的示例:http://developer.android.com/tools/help/draw9patch.html

有人知道任何可以縮放的東西嗎?我需要一些可以支持PNG的東西。

+2

谷歌'類似9補丁擺動buttons' weblookandfeel'似乎是前幾個鏈接'成爲你正在尋找的東西。 http://weblookandfeel.com/nine-patch-editor/ –

+0

使用java2d/imageio。您可以使用正確的平移和縮放(AffineTransform)在BufferedImage上繪製所有內容,並使用圖像將其作爲PNG寫出 –

回答

2

如果您正在尋找一種在Java組件上使用9修補圖像的方法,我在這裏問了同樣的問題:How use a 9-patch image as background on a JPanel?並且簡短答案是否定的,您不能。

長的一個是:如果您在9張圖像(邊框,角落和中心)中分割圖像並創建一個重繪時重新移動和調整圖像大小的組件。

遵循適於我的情況下的例子:

  • 的組件是一個JPanel。
  • 面板的中心必須是透明的,所以我需要少一個圖像。
  • 該組件不會小於給定的圖像。
  • 圖像具有透明度,這解釋了代碼中的setOpaque(false)調用。
  • 該代碼是一個粗略的草案。

下面的代碼:

public class NinePatchLikePanel extends JPanel{ 
private JPanel corner_top_l; 
private JPanel corner_top_r; 
private JPanel corner_bot_l; 
private JPanel corner_bot_r; 

private JPanel border_ver_l; 
private JPanel border_ver_r; 
private JPanel border_hoz_t; 
private JPanel border_hoz_b; 

private int min_width, min_height; 

private int corners_width; 
private int corners_height; 
private int borders_width; 
private int borders_height; 


public NinePatchLikePanel (String[] urls) { 

    if(urls.length != 8) { 
     throw new UnsupportedOperationException("Exception to be managed!"); 
    } else { 
     corner_top_l = new JPanelWithBackground (urls [0]); 
     corner_top_r = new JPanelWithBackground (urls [1]); 
     corner_bot_r = new JPanelWithBackground (urls [2]); 
     corner_bot_l = new JPanelWithBackground (urls [3]); 

     border_hoz_t = new JPanelWithBackground (urls [4]); 
     border_ver_r = new JPanelWithBackground (urls [5]); 
     border_hoz_b = new JPanelWithBackground (urls [6]); 
     border_ver_l = new JPanelWithBackground (urls [7]); 

     corners_width = corner_top_l.getWidth(); 
     corners_height = corner_top_l.getHeight(); 

     borders_width = border_hoz_t.getWidth(); 
     borders_height = border_ver_l.getHeight(); 

     min_width = 2 * corners_width + borders_width; 
     min_height = 2 * corners_height + borders_height; 

     this.setSize (min_width, min_height); 
     this.setMinimumSize (new Dimension (min_width, min_height)); 
     this.setOpaque(false); 
     this.setLayout(null); 
     this.add(corner_top_l); 
     this.add(corner_top_r); 
     this.add(corner_bot_l); 
     this.add(corner_bot_r); 

     this.add(border_hoz_t); 
     this.add(border_ver_r); 
     this.add(border_hoz_b); 
     this.add(border_ver_l); 
    } 
} 

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 

    int actual_width = this.getWidth(); 
    int actual_height = this.getHeight(); 

    int _x = actual_width - corners_width; 
    int _y = actual_height - corners_height; 

    corner_top_l.setLocation(0, 0); 
    corner_top_r.setLocation(_x, 0); 
    corner_bot_l.setLocation(0, _y); 
    corner_bot_r.setLocation(_x, _y); 

    int new_borders_width = _x - corners_width; 
    int new_borders_height = _y - corners_height; 


    border_hoz_t.setLocation(corners_width, 0); 
    border_hoz_t.setSize(new_borders_width, border_hoz_t.getHeight()); 

    border_ver_r.setLocation(_x, corners_height); 
    border_ver_r.setSize(border_ver_r.getWidth(), new_borders_height); 

    border_hoz_b.setLocation(corners_width, _y); 
    border_hoz_b.setSize(new_borders_width, border_hoz_b.getHeight()); 

    border_ver_l.setLocation(0, corners_height); 
    border_ver_l.setSize(border_ver_l.getWidth(), new_borders_height); 
} 

} 

這裏的JPanelWithBackground類的代碼:

public class JPanelWithBackground extends JPanel { 
Image bg = null; 

public JPanelWithBackground(String url) { 
    try{ 
     bg = ImageIO.read(getClass().getResourceAsStream(url)); 
     int height = bg.getHeight(null); 
     int width = bg.getWidth(null); 
     Dimension d = new Dimension(width,height); 
     this.setSize (width, height); 
     this.setMinimumSize (d); 
     this.setOpaque(false); 
    } catch (IOException ex) { 
     //TODO: Manage this exception in a better way 
      System.err.println(ex); 
      System.exit(1); 
    } 
} 

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    if (bg != null) 
     g.drawImage(bg, 0, 0, this.getWidth(), this.getHeight(), null); 
} 
}