我不确定这是否是正确的问题,但我想知道在Android上是否有类似9个补丁图像,但对于Java,如Oracle,PC java。我所有的谷歌搜索只显示android,因为人们称之为Java,但它并不是真的相同。
我找到了LibGdx,但对于我正在寻找的单一九补丁能力而言,这是非常沉重的。
九个补丁图像是具有“9”区域的图像,通常在侧壁和中心区域拉伸以适合屏幕时“不缩放”角落。 来自Android的示例:http://developer.android.com/tools/help/draw9patch.html
有谁知道任何可以像这样扩展的东西?我需要能够支持PNG的东西。
答案 0 :(得分:2)
如果您正在搜索在Java组件上使用9补丁图像的方法,我在这里问了同样的问题:How use a 9-patch image as background on a JPanel?而简短的回答是否定的,你不能。
较长的一个是:如果您在9个图像(边框,角落和中心)中分割图像并创建一个重新绘制时移动并调整图像大小的组件,则可以。
以下示例适用于我的情况:
这里是代码:
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);
}
}