[英]Is there a better way to add a ton of JPanels with unique images attached to an array?
I have an entire deck of cards as images I need to: a) load in as JPanels and b) display in a JFrame. 我需要一整套纸牌作为图像:a)作为JPanels加载和b)在JFrame中显示。
Is there a better way to get them into my program then over and over (52x) writing something like 有没有更好的办法让它们进入我的程序,然后反复(52x)编写类似
final JPanel panelName = draw(new ImageIcon("spritesheet.gif"));
Each image (spritesheet.gif) has a unique name. 每个图像(spritesheet.gif)都有一个唯一的名称。 It's a deck of cards.
这是一副纸牌。
Here is draw
这是
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..
每个图像都有一个唯一的名称。
As long as there is some sort of pattern to them it can be easy. 只要他们有某种模式就可以很容易。 EG
"spades-queen.gif"
can be made from an array String[]
of suits, an array of levels, a -
to separate them & a .gif
on the end.. EG
"spades-queen.gif"
可以由西装的String[]
数组,关卡数组, -
分隔它们和最后一个.gif
。
They're formatted like
aceSpades
and so on.它们的格式类似于
aceSpades
等。
Here is an implementation: 这是一个实现:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.