有没有更好的方法来添加大量带有附加到数组的独特图像的 JPanel?
Is there a better way to add a ton of JPanels with unique images attached to an array?
我有一整副纸牌作为图像,我需要:
a) 加载为 JPanels 和
b) 在 JFrame 中显示。
有没有更好的方法让它们进入我的程序,然后一遍又一遍地 (52x) 编写类似
的东西
final JPanel panelName = draw(new ImageIcon("spritesheet.gif"));
每张图片 (spritesheet.gif) 都有一个唯一的名称。这是一副纸牌。
这里是draw
public static JPanel draw(final ImageIcon img)
{
JPanel panel = new JPanel()
{
private static final long serialVersionUID = 1L;
//paintComponent is called automatically by the JRE whenever
//the panel needs to be drawn or redrawn
public void paintComponent(Graphics g) {
super.paintComponent(g);
img.paintIcon(this, g, 10, 10);
}
};
panel.setOpaque(false);
return panel;
}
each image has a unique name..
只要他们有某种模式就可以很容易。例如。 "spades-queen.gif"
可以由一系列 String[]
套装、一系列级别、一个 -
来分隔它们以及最后一个 .gif
组成。
They're formatted like aceSpades
and so on.
这是一个实现:
public class CardNames {
public final static String[] SUITS = {
"Spades", "Hearts", "Diamonds", "Clubs"
};
public final static String[] LEVELS = {
"ace", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "jack", "queen", "king"
};
public final static String SEP = "";
public final static String XTN = ".gif";
public static void main(String[] args) {
for (String suit : SUITS) {
for (String level : LEVELS) {
System.out.println(level + SEP + suit + XTN);
}
}
}
}
输出
aceSpades.gif
twoSpades.gif
threeSpades.gif
fourSpades.gif
fiveSpades.gif
sixSpades.gif
sevenSpades.gif
eightSpades.gif
nineSpades.gif
tenSpades.gif
jackSpades.gif
queenSpades.gif
kingSpades.gif
aceHearts.gif
// ...
aceClubs.gif
twoClubs.gif
threeClubs.gif
fourClubs.gif
fiveClubs.gif
sixClubs.gif
sevenClubs.gif
eightClubs.gif
nineClubs.gif
tenClubs.gif
jackClubs.gif
queenClubs.gif
kingClubs.gif
我有一整副纸牌作为图像,我需要: a) 加载为 JPanels 和 b) 在 JFrame 中显示。
有没有更好的方法让它们进入我的程序,然后一遍又一遍地 (52x) 编写类似
的东西final JPanel panelName = draw(new ImageIcon("spritesheet.gif"));
每张图片 (spritesheet.gif) 都有一个唯一的名称。这是一副纸牌。
这里是draw
public static JPanel draw(final ImageIcon img)
{
JPanel panel = new JPanel()
{
private static final long serialVersionUID = 1L;
//paintComponent is called automatically by the JRE whenever
//the panel needs to be drawn or redrawn
public void paintComponent(Graphics g) {
super.paintComponent(g);
img.paintIcon(this, g, 10, 10);
}
};
panel.setOpaque(false);
return panel;
}
each image has a unique name..
只要他们有某种模式就可以很容易。例如。 "spades-queen.gif"
可以由一系列 String[]
套装、一系列级别、一个 -
来分隔它们以及最后一个 .gif
组成。
They're formatted like
aceSpades
and so on.
这是一个实现:
public class CardNames {
public final static String[] SUITS = {
"Spades", "Hearts", "Diamonds", "Clubs"
};
public final static String[] LEVELS = {
"ace", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "jack", "queen", "king"
};
public final static String SEP = "";
public final static String XTN = ".gif";
public static void main(String[] args) {
for (String suit : SUITS) {
for (String level : LEVELS) {
System.out.println(level + SEP + suit + XTN);
}
}
}
}
输出
aceSpades.gif
twoSpades.gif
threeSpades.gif
fourSpades.gif
fiveSpades.gif
sixSpades.gif
sevenSpades.gif
eightSpades.gif
nineSpades.gif
tenSpades.gif
jackSpades.gif
queenSpades.gif
kingSpades.gif
aceHearts.gif
// ...
aceClubs.gif
twoClubs.gif
threeClubs.gif
fourClubs.gif
fiveClubs.gif
sixClubs.gif
sevenClubs.gif
eightClubs.gif
nineClubs.gif
tenClubs.gif
jackClubs.gif
queenClubs.gif
kingClubs.gif