gpt4 book ai didi

java - 有没有更好的方法来添加大量带有附加到数组的独特图像的 JPanel?

转载 作者:行者123 更新时间:2023-11-30 07:03:21 24 4
gpt4 key购买 nike

我有一整副纸牌作为图像,我需要: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

关于java - 有没有更好的方法来添加大量带有附加到数组的独特图像的 JPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28467763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com