2013-11-23 6 views
6

मुझे यकीन नहीं है कि यह पूछने का सही स्थान है, लेकिन मुझे आश्चर्य है कि एंड्रॉइड पर 9 पैच छवियों की तरह कुछ है, लेकिन जावा के लिए, ओरेकल, पीसी जावा जैसे। मेरी सभी Google खोज केवल मुझे एंड्रॉइड दिखाती हैं क्योंकि लोग जावा को कॉल करते हैं, लेकिन यह वास्तव में वही नहीं है।9-पैच जावा के लिए छवि स्केलिंग की तरह?

मुझे लीबीजीडीएक्स मिला है लेकिन यह एक नौ-पैच क्षमता के लिए काफी भारी है जिसे मैं ढूंढ रहा हूं।

एक नौ पैच छवि वह है जिसमें '9' क्षेत्र हैं, कोनों को आम तौर पर 'स्केल नहीं किया जाता है' जबकि पक्ष की दीवारें और केंद्र क्षेत्र स्क्रीन फिट करने के लिए फैला हुआ है। एंड्रॉइड से एक उदाहरण: http://developer.android.com/tools/help/draw9patch.html

क्या किसी को ऐसा कुछ भी पता है जो इस तरह से स्केल कर सकता है? मुझे कुछ ऐसा चाहिए जो पीएनजी का समर्थन कर सके।

+2

गूगल 'समान स्विंग के लिए पहले कुछ लिंक करने के लिए' weblookandfeel' लगते buttons' 9-पैच जो आप खोज रहे हैं उसके लिए। http://weblookandfeel.com/nine-patch-editor/ –

+0

java2d/imageio का उपयोग करें। आप सही अनुवाद और स्केलिंग (AffineTransform) का उपयोग करके BufferedImage पर सबकुछ खींच सकते हैं और इसे छवियों का उपयोग करके पीएनजी के रूप में लिख सकते हैं –

उत्तर

2

यदि आप जावा घटक पर 9-पैच छवि का उपयोग करने का कोई तरीका खोज रहे हैं तो मैंने यहां एक ही प्रश्न पूछा: How use a 9-patch image as background on a JPanel? और संक्षिप्त उत्तर नहीं है, आप नहीं कर सकते।

लंबा एक है: यदि आप 9 छवियों (सीमाओं, कोनों और केंद्र) में छवि को विभाजित कर सकते हैं और एक घटक बना सकते हैं कि जब चित्रों को पुनर्निर्मित किया जाता है और छवियों का आकार बदलता है।

उदाहरण इस प्रकार है कि मेरे मामले जहां के लिए अनुकूलित है:

  • घटक एक JPanel है।
  • पैनल का केंद्र पारदर्शी होना चाहिए, इसलिए मुझे एक छवि कम की आवश्यकता है।
  • घटक दिए गए चित्रों से छोटा नहीं होगा।
  • छवियों में पारदर्शिताएं हैं, यह कोड में setOpaque (झूठी) कॉल बताती है।
  • कोड एक मोटा ड्राफ्ट है।

यहाँ कोड:

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); 
} 
} 
संबंधित मुद्दे