使用 Icon 将 window 的长度设置为正确的长度
Setting the length of the window to the correct length using Icon
使用这个 class 和这个 main 方法,我试图使 运行 main class 时创建的 window 是正确的大小来保存传递给它的所有图标,而不必调整 window 的大小,并且无需将值硬编码为我在初始化时想要的 window 大小。
现在,当它运行时,window 开始时非常小,当我调整它的大小时,它上面绘制的所有图标的布局都乱七八糟。
我知道如何确定合适的尺寸,但我不确定我是如何知道使用坐标 ArrayList 来确定尺寸的,但我不确定如何更改尺寸window 已经初始化后
public class CompositeIcon implements Icon
{
ArrayList<Icon> iList;
static int width;
static int height;
ArrayList<Point> coordinates;
public CompositeIcon()
{
iList = new ArrayList<Icon>();
coordinates = new ArrayList<Point>();
}
public int getIconHeight()
{
return height;
}
public void addIcon(Icon icon, int x, int y)
{
iList.add(icon);
coordinates.add(new Point(x, y));
}
public int getIconWidth()
{
return width;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
int i = 0;
for (Icon s : iList)
{
Point offset = coordinates.get(i++);
s.paintIcon(c, g, x + offset.x, y + offset.y);
}
}
这个主要是测试一下
public static void main (String args[]) {
JFrame frame = new JFrame();
Container panel = frame.getContentPane();
panel.setLayout(new BorderLayout());
CompositeIcon icon = new CompositeIcon();
try {
icon.addIcon(new ImageIcon(new URL("http://th02.deviantart.net/fs71/150/f/2013/103/2/7/java_dock_icon_by_excurse-d61mi0t.png")), 10, 10);
icon.addIcon(new ImageIcon(new URL("http://www.bravegnu.org/blog/icons/java.png")), 5, 370);
icon.addIcon(new ImageIcon(new URL("http://fc03.deviantart.net/fs20/f/2007/274/9/8/3D_Java_icon_by_BrightKnight.png")), 200, 200);
}
catch (MalformedURLException e) {
System.err.println("Apparently, somebody cannot type a URL");
}
panel.add(new JLabel(icon));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
基本上,CompositeIcon
的 width
和 height
应该代表您添加的图标的组合宽度和高度(允许 x/y 偏移量)
类似...
public void addIcon(Icon icon, int x, int y) {
iList.add(icon);
width = Math.max(width, x + icon.getIconWidth());
height = Math.max(height, y + icon.getIconHeight());
coordinates.add(new Point(x, y));
}
您需要删除 width
和 height
的 static
声明,因为 CompositeIcon
的每个实例都应该有自己的 width
和 height
您的方法允许随机定位图标,但这意味着您有责任了解每个图标的大小并定位它们,以免彼此重叠。
有关更结构化的方法,您可以查看 Compound Icon。 class 支持垂直、水平和堆叠图标对齐,并为您完成所有 location/size 计算。
使用这个 class 和这个 main 方法,我试图使 运行 main class 时创建的 window 是正确的大小来保存传递给它的所有图标,而不必调整 window 的大小,并且无需将值硬编码为我在初始化时想要的 window 大小。
现在,当它运行时,window 开始时非常小,当我调整它的大小时,它上面绘制的所有图标的布局都乱七八糟。
我知道如何确定合适的尺寸,但我不确定我是如何知道使用坐标 ArrayList 来确定尺寸的,但我不确定如何更改尺寸window 已经初始化后
public class CompositeIcon implements Icon
{
ArrayList<Icon> iList;
static int width;
static int height;
ArrayList<Point> coordinates;
public CompositeIcon()
{
iList = new ArrayList<Icon>();
coordinates = new ArrayList<Point>();
}
public int getIconHeight()
{
return height;
}
public void addIcon(Icon icon, int x, int y)
{
iList.add(icon);
coordinates.add(new Point(x, y));
}
public int getIconWidth()
{
return width;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
int i = 0;
for (Icon s : iList)
{
Point offset = coordinates.get(i++);
s.paintIcon(c, g, x + offset.x, y + offset.y);
}
}
这个主要是测试一下
public static void main (String args[]) {
JFrame frame = new JFrame();
Container panel = frame.getContentPane();
panel.setLayout(new BorderLayout());
CompositeIcon icon = new CompositeIcon();
try {
icon.addIcon(new ImageIcon(new URL("http://th02.deviantart.net/fs71/150/f/2013/103/2/7/java_dock_icon_by_excurse-d61mi0t.png")), 10, 10);
icon.addIcon(new ImageIcon(new URL("http://www.bravegnu.org/blog/icons/java.png")), 5, 370);
icon.addIcon(new ImageIcon(new URL("http://fc03.deviantart.net/fs20/f/2007/274/9/8/3D_Java_icon_by_BrightKnight.png")), 200, 200);
}
catch (MalformedURLException e) {
System.err.println("Apparently, somebody cannot type a URL");
}
panel.add(new JLabel(icon));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
基本上,CompositeIcon
的 width
和 height
应该代表您添加的图标的组合宽度和高度(允许 x/y 偏移量)
类似...
public void addIcon(Icon icon, int x, int y) {
iList.add(icon);
width = Math.max(width, x + icon.getIconWidth());
height = Math.max(height, y + icon.getIconHeight());
coordinates.add(new Point(x, y));
}
您需要删除 width
和 height
的 static
声明,因为 CompositeIcon
的每个实例都应该有自己的 width
和 height
您的方法允许随机定位图标,但这意味着您有责任了解每个图标的大小并定位它们,以免彼此重叠。
有关更结构化的方法,您可以查看 Compound Icon。 class 支持垂直、水平和堆叠图标对齐,并为您完成所有 location/size 计算。